Adding an array to move a group of images to a for loop in Swift: SpriteKit

How can I add mine starsSqArray

to a for loop in my update function that grabs everything SKSpriteNodesfor

_starsSq1

so that all the stars move together rather than separately?

Right now my Swift class keeps returning an error saying it _starsSqArray

has no position (my code is disabled). My goal is to capture the abandoned stars and immediately move them down.

import SpriteKit

class Stars:SKNode {

//Images
var _starsSq1:SKSpriteNode?

//Variables
var starSqArray = Array<SKSpriteNode>()
var _starSpeed1:Float = 5;

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init() {
    super.init()
    println("stars plotted")
    createStarPlot()
}

/*-------------------------------------
    ## MARK: Update
-------------------------------------*/
func update() {

    for (var i = 0; i < starSqArray.count; i++) {
        _starsSq1!.position = CGPoint(x: self.position.x , y: self.position.y + CGFloat(_starSpeed1))
    }
}

/*-------------------------------------
    ## MARK: Create Star Plot
-------------------------------------*/

func createStarPlot() {
    let screenSize: CGRect = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    for (var i = 0; i < 150 ; i++) {
        _starsSq1 = SKSpriteNode(imageNamed: "starSq1")
        addChild(_starsSq1!)
        //starSqArray.addChild(_starsSq1)
        var x = arc4random_uniform(UInt32(screenSize.width + 400))
        var y = arc4random_uniform(UInt32(screenSize.height + 400))
        _starsSq1!.position = CGPoint(x: CGFloat(x), y: CGFloat(y))
    }

}

}

      

+3


source to share


2 answers


A few suggestions in terms of design:

  • You already have all of your stars (I think so) grouped into a common parent node (which you named Stars correctly ). Then you just need to move the Star type node and all of its children node will move automatically.
  • Manually changing the coordinates of a node inside the update method works, but (imho) it's not the best way to move it. SKAction should be used instead .

So, if you want to move stars permanently with general speed and direction, you can add this method to stars



func moveForever() {
    let distance = 500 // change this value as you prefer
    let seconds : NSTimeInterval = 5 // change this value as you prefer

    let direction = CGVector(dx: 0, dy: distance)
    let move = SKAction.moveBy(direction, duration: seconds)
    let moveForever = SKAction.repeatActionForever(move)
    self.runAction(moveForever)
}

      

Now you can remove the code inside the update method and call moveForever when you want the stars to move.

Finally, at some point, the stars will leave the screen. I don't know what effect you want to achieve, but you will need to deal with it.

+1


source


for (SKSpriteNode *spriteNode in starSqArray) {
    spriteNode.position = CGPoint(x: spriteNode.position.x , y: spriteNode.position.y + GFloat(_starSpeed1))
}

      

Use the above code in the update () function.



Hope it helps ...

+1


source







All Articles