0
votes

I would like to spawn an enemy (an SKSpriteNode) every ten seconds and increase that time as time goes on. Does anyone know how I can accomplish this?

P.S. I have tried using a NSTimer but I keep getting an error saying: Cannot invoke 'scheduledTimerWithTimeInterval' with an argument list of type '(Double, target: GameScene -> () -> GameScene, selector: String, userInfo: nil, repeats: Bool)'

1
Please try and see where you get stuck and we'll help youMyGGaN
@MyGGaN The thing is I don't really know where to start... ._.vinny_711
Are you really talking about using timers? What if you read up on developer.apple.com/library/mac/documentation/Cocoa/Reference/… and then try to ask a more specific question? Or are you asking about how to create a Sprite and add it to a scene?BaseZen
@BaseZen Well I've tried to use NSTimers but they do not work with SpriteKit or at least it didn't work with SpriteKit with me. :/vinny_711

1 Answers

0
votes

You're simply calling the parameters incorrectly to the scheduledTimerWithTimeInterval method.

This API works in a very "old-fashioned" way: they don't take a closure but rather a target object (on which to call the selector), and String-typed method selector.

The target is usually just self, e.g. the controller that you're working within that started the timer should also receive the timer events.

The selector should be simply a hard-coded string: "handleTimer:"

so you have:

class MyController {
    override func viewDidLoad() {
        super.viewDidLoad()
        /* Repeats every 10 seconds. handleTimer() can feel free to invalidate
           and re-schedule this timer at a different interval */
        NSTimer.scheduledTimerWithTimeInterval(10.0, self, "handleTimer:", nil, true)
    }

    func handleTimer(timer: NSTimer) {
        /* Do my awesome GameKit stuff here, using stored properties to refer to
           all my scene objects. (Since I don't get any params passed in) */
        /* Now play with the timer any way I like, e.g.: */
        let newInterval = timer.timeInterval + 1.0
        timer.invalidate()
        NSTimer.scheduledTimerWithTimeInterval(newInterval, self, "handleTimer:", nil, true)
    }
}