-1

I've got this set of code that spawns a SKSpriteNode within a set location (using rect), but I'm trying to make the Node disappear but then reappear after a set amount of time in a different location. I've been looking a NSTimer but not sure how to implement that, is there a good way of doing this?

let rect = CGRectMake(8, 293, 165, 218)    
let x = rect.origin.x + CGFloat(arc4random()) % rect.size.width
let y = rect.origin.y + CGFloat(arc4random()) % rect.size.height
let randomPoint = CGPointMake(x, y)
self.redcircle.position = randomPoint
self.addChild(redcircle)
Pimgd
  • 5,983
  • 1
  • 30
  • 45
MattB
  • 13
  • 4

1 Answers1

0

Instead of using an NSTimer, use a SpriteKit SKAction. What you need is the action waitForDuration:

someNode.hidden = YES

waitAction = SKAction.waitForDuration(someDuration)

self.runAction(wait, completion: {() -> Void in

    // Sprite Kit waits for a certain amount of time and runs this completion closure
    // Inside here you can get the node to reappear again

    someNode.hidden = NO
}))
MaxKargin
  • 1,560
  • 11
  • 13
  • Thank you, is there a a way to repeat this, so it disappears again and reappears in a different location within the rect? – MattB Aug 20 '15 at 15:11
  • Yeah what you could do is use a custom `SKAction` (called `runBlock`) which would make the node unhidden or hidden. Then you would put that action inside of a `sequence` action and have another action repeat that sequence for either a certain amount of iterations or forever. You should check out the SKAction class reference: https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKAction_Ref/ – MaxKargin Aug 20 '15 at 15:15
  • Do you know how I would implement the `node.hidden` into the SKAction so it can keep on disappearing and then become unhidden? – MattB Aug 21 '15 at 14:44
  • You would need to create a custom SKAction, which would either be a runBlock or performSelector. runBlock might be the easiest since it creates a closure inside which you just write either unhidden or hidden – MaxKargin Aug 21 '15 at 16:05