user5708587
user5708587

Reputation:

SpriteKit - NSTimer showing error

I'm building a simple game and I want a "Square" to shoot from the left and side to the middle of the screen, where my "MainSquare" is. I'm using an NSTimer and making this square shoot from the left to the center every second. Even though this NSTimer has a purpose, running my "LeftShot" function, it's still coming up with the famous and annoying

"initialisation of variable 'timer' was never used".

Help? :(

in my did move to view:

   var Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("LeftShot"), userInfo: nil, repeats: true)

the function the timer runs every second:

func LeftShot(){

    let EnemySquareLeft = SKSpriteNode(imageNamed: "Square2")
    EnemySquareLeft.size = CGSize(width: 30, height: 30)
    EnemySquareLeft.position = CGPoint(x: frame.width / 2 - 231, y: frame.height / 2)

    let action = SKAction.moveTo(MainSquare.position, duration: 1)

    EnemySquareLeft.runAction(SKAction.repeatActionForever(action))

    self.addChild(EnemySquareLeft)

}

Upvotes: 0

Views: 86

Answers (1)

Dieblitzen
Dieblitzen

Reputation: 544

I think this error is just a yellow coloured error? If so, you can ignore it, or in the didMoveToView just change the declaration of timer to

_ = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("LeftShot"), userInfo: nil, repeats: true)

Xcode encourages you to do this when you are not using the name of the variable (or constant) anywhere else.

However, if you will use some property of your timer within the didMoveToView, change the declaration to

let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("LeftShot"), userInfo: nil, repeats: true)

Upvotes: 1

Related Questions