1

I'm making a game in Xcode 6 using spriteKit and swift. I have a plane on scene, and to make it look like its moving, I'm create clouds off the scene to the left of the screen. I then us an SKAction to move the cloud to the right side of the screen. This works great. You then click to jump off the plane, and the plane moves up off the scene. I then have it start making the clouds on the bottom of the scene, then they move up off the top of the scene, but the problem is, the already existing clouds still have to move to the right side of the screen. My question is, how do I make it so all of the existing clouds stop their action that moves them to the right, then begin to move up exactly where they are? How do I access the group of existing clouds all at the same time once they have been created? I also want the clouds to slow down after you have jumped when you tap the screen to open your parachute, but this should be able to be done by ending the SKAction that moves the clouds, then using another SKAction on them that moves them up slower, but I don't know how to access a group of SKSpriteNodes.

Here is the code that I have to make the clouds:

//This is how the cloud is first declared at the top of the .swift file
var cloud = SKSpriteNode()

//This is the function that runs every certain interval through an NSTimer
func createCloud()
{
    cloud = SKSpriteNode(imageNamed: "cloud")
    cloud.xScale = 1.25

    cloud.yScale = cloud.xScale/2
    cloud.zPosition = -1
    self.addChild(cloud)

    if personDidJump == false
    {
        let moveRight = SKAction.moveToX(CGRectGetWidth(self.frame) + CGRectGetWidth(cloud.frame), duration: cloudSpeed)
        var apple = CGRectGetWidth(self.frame)
        var randomNumber:CGFloat = CGFloat(arc4random_uniform(UInt32(apple)))
        cloud.position = CGPointMake(-CGRectGetWidth(cloud.frame), randomNumber)
        cloud.runAction(moveRight)
    }
    if personDidJump == true
    {
        let moveUp = SKAction.moveToY(CGRectGetHeight(self.frame) + CGRectGetWidth(cloud.frame), duration: cloudSpeed)
        var apple = CGRectGetWidth(self.frame)
        var randomNumber:CGFloat = CGFloat(arc4random_uniform(UInt32(apple)))
        cloud.position = CGPointMake(randomNumber, -CGRectGetHeight(cloud.frame))
        cloud.runAction(moveUp)
    }
}

Also, should I be worried about deleting the clouds when they move off the scene? Or can I just leave them there because you can't see them, and from what I've seen, they don't lag you. Any help would be greatly appreciated. Thank you.

-Callum-

sangony
  • 11,636
  • 4
  • 39
  • 55
Callum Ferguson
  • 190
  • 2
  • 12
  • You can apply same action to nodes with same name using this method : http://stackoverflow.com/a/24213529/3402095 About removing nodes when they are offscreen...I think nodes still consuming resources even they are offscreen because they are still in node graph. But you can check this further more...Also note that you must figure out how to detect when nodes are offscreen. Checking for nodes position in update method sometimes may not be the most performant way. The solution for this would be having a border with same contactTestBitMask and on contact detection to do you stuff. – Whirlwind Mar 23 '15 at 22:46
  • Thank you. I should have looked harder to find that page, but that is worded weird, so I'm not surprised I didn't find that page while searching for solutions. – Callum Ferguson Mar 23 '15 at 23:03

2 Answers2

0

Put your clouds in an array. When your player jumps (or whenever you need to move them) run through the array and run your action on each cloud.

meisenman
  • 1,818
  • 1
  • 15
  • 25
  • Thanks for the help, but the solution "whirlwind" showed me: stackoverflow.com/a/24213529/3402095 worked well. With this I can also easily change the properties of the Nodes, like physics body's properties, but thanks for trying to help. – Callum Ferguson Mar 25 '15 at 03:52
0

stackoverflow.com/a/24213529/3402095

^ That is where I found my answere ^

When you make a node give it a name:

myNode.name = "A Node Name"
self.addChild(myNode)

If there are a lot of these nodes, later you can change properties or preform SKAcions on these nodes or do whatever you want to do by using enumerateChildNodesWithName:

self.enumerateChildNodesWithName("A Node Name")
{
    myNode, stop in 

    myNode.runAction(SKAction.moveToY(CGRectGetHeight(self.Frame), duration: 1)
}

I hope this is useful to whoever may need it.

Callum Ferguson
  • 190
  • 2
  • 12