3

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

}

}
3
  • I do not know about Sprite nodes, but I guess if you will add all the stars in a scene or view and move the scene to downward then the result will be what you are looking for. Commented Jan 5, 2015 at 13:12
  • Could you put on more details, like the error info? Commented Jan 5, 2015 at 13:25
  • have you tried adding them as subnodes to one parent node and move that parent mode? Commented Jan 5, 2015 at 14:35

2 Answers 2

1
+50

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.

Sign up to request clarification or add additional context in comments.

Comments

1
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...

2 Comments

I don't think this will do what he wants. This will make every sprite in starSqArray have the exact same X, Y position. What appzYourLife is more correct in that all sprites in starSqArray are children of Stars, so just moving Star will move all the children in their positions which are randomly distributed. If you don't want to move the Stars node itself, then I'd have one sub-node and have the sub-node contain all of the startSqArray sprites and move the sub-node.
Optionally if you did want to do it that way you would do spriteNode.position = CGPoint(x: spriteNode.position.x, y: spriteNode.position.y + CGFloat(_startSpeed1)) But moving the parent node is far superior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.