0

I'm trying to unhide this node, so that it appears within the rect location but then somehow making that node hidden again so when it unhides it is in a different location within the rect. But I'm trying to add a random timer to it unhides at random intervals (but under 5 seconds)?

let x12 = rect4.origin.x + CGFloat(arc4random()) % rect4.size.width
let y12 = rect4.origin.y + CGFloat(arc4random()) % rect4.size.height
    let randomPoint12 = CGPointMake(x12, y12)
    self.yellowcircle3.position = randomPoint12
    self.addChild(yellowcircle3)


    yellowcircle3.hidden = true
MattB
  • 13
  • 4

1 Answers1

0

It sounds like you want to make an NSTimer and use arc4random to get a time interval.

The first thing you need to do is declare a NSTimer and a random number by adding the code:

var myTimer = NSTimer()
var secondsPassed : Int = 0
var randomTimeInterval = arc4Random % 4 + 1

(The 4 + 1 will return a value between 1 and 5)

Next, you need a function to call upon when you want to move your node.

 func moveNode(){
            myTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerRan:"), userInfo: nil, repeats: true)

    }

Once you call moveNode(), your timer will start to run. But now we need the timer's function, or it's Selector so it can run properly.

func timerRan(sender: NSTimer!){
     secondsPassed++
     if secondsPassed == randomTimeInterval (
        //unhide your rect and reset the timer
     )
}

Hope this helps :)

Nick Pacini
  • 195
  • 2
  • 12
  • I suggest you use an `SKAction` instead of an `NSTimer`. Here's why http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer – 0x141E Aug 21 '15 at 17:13
  • Could I do this with multiple nodes? – MattB Aug 21 '15 at 18:19
  • @MattB The formatting would be the same with multiple nodes. You would just need multiple timers for each node, such as myTimer1 and myTimer2, with separate functions respectively – Nick Pacini Aug 21 '15 at 22:35