Monstr92
Monstr92

Reputation: 404

Adding Array to move group of images in for loop in Swift : SpriteKit

How can I add my starsSqArray to a for loop in my update function that grabs all the SKSpriteNodesfor _starsSq1 so that all of the stars move together and not separately?

Right now my Swift class keeps returning an error saying that _starsSqArray doesn't have a position (my code is bugged out). My goal is to grab the plotted stars and move them downward all at once.

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))
    }

}

}

Upvotes: 3

Views: 1173

Answers (2)

Henit Nathwani
Henit Nathwani

Reputation: 442

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 this helps...

Upvotes: 1

Luca Angeletti
Luca Angeletti

Reputation: 59496

A couple of suggestions by a design point of view:

  1. You already have all your stars (I guess so) grouped togheter inside a common parent node (that you correctly named Stars). Then you just need to move your node of type Stars and all its child node will move automatically.
  2. Manually changing the coordinates of a node inside an update method does work but (imho) it is not the best way to move it. You should use SKAction instead.

So, if you want to move the stars forever with a common 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 start moving.

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

Upvotes: 1

Related Questions