0

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)

}
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • post more code, timer looks local, but you may be using it globally – Knight0fDragon Dec 26 '15 at 22:50
  • 3
    @ddramond Aside from a main problem, in SpriteKit it is a better idea to use SKAction for time related actions. Read more [here](http://stackoverflow.com/questions/23978209/spritekit-creating-a-timer) – Whirlwind Dec 26 '15 at 23:53
  • @Whirlwind, his issue is technically not a sprite kit issue, and SKActions are not really needed in this case – Knight0fDragon Dec 27 '15 at 01:05
  • 4
    Shouldn't be using nstimer in SpriteKit. Either use skaction or get delta time from the update loop to determine an interval of time – hamobi Dec 27 '15 at 01:15
  • there are times when you need to use NSTimers in conjunction with SpriteKit, especially if you are mixing SpriteKit with UIKit. Plus, sometimes you do not want a timer that is on par with the game world. As long as you add a pause feature to NSTimer, NSTimer has its purpose and can even be more efficient then SKActions, if you have multiple actions that are on the same timer that is. – Knight0fDragon Dec 27 '15 at 01:22
  • @Knight0fDragon I am afraid that we will go off-topic even more if continue talking about NSTimer vs SKAction, or SpriteKit mixed with UIKit...But, can you give an example about when in SpriteKit we need a timer which shouldn't be paired with a game world (I guess that "game world" refers to a game loop) ? – Whirlwind Dec 28 '15 at 10:54
  • How about the free 2 play model when you want things to refresh in exactly 10 minutes to get a refresh in something, which interacts with all sorts of objects on your screen, and for some reason is also running in the background of your app, you wouldn't want to do this inside the game scene code – Knight0fDragon Dec 28 '15 at 15:50
  • @Knight0fDragon "and for some reason is also running in the background of your app" Can you please be more specific about this part ? Probably not, but did you meant that NSTimer should be running while app is in the background or you thought about something else ? – Whirlwind Dec 29 '15 at 18:04
  • @whirlwind. I am speaking very generic here. I am saying that not always does the spritekit timer meet the needs of the developer. There are just times when a scene is in a pause state, and you still need a timer. That is all – Knight0fDragon Dec 29 '15 at 18:08
  • @Knight0fDragon Ah,okay, I meant you were talking about running NSTimer while app is in the background which wouldn't be reliable/safe at all because an app might be killed by iOS easily. Probably this is not even possible for more than just few minutes... Anyways, I am pretty much positive that refreshing you were talking about can be done by using just update: method and by tracking the elapsed time. While game is paused, it is not meant for anything to happens actually..And when game is unpaused, you can refresh what is needed based on the elapsed time. – Whirlwind Dec 29 '15 at 19:36
  • @Whirlwind, elapse time checking is not the same as `NSTimer`, there are things that may happen on a continual beat, pausing a scene is not the same as pausing an app, you can be doing things outside of a scene that need to be communicated with the scene even in its paused state on a regular interval, it all depends on how the app is designed. This has carried on to long, so my final point is that you may need `NSTimer`, so to say do not use them is flawed, if you can get by with the built in SK timing, then yes do that first. – Knight0fDragon Dec 29 '15 at 19:37
  • @Knight0fDragon Yeah, I know what means when scene is paused, and what is the difference when the view is paused, or when a game is paused. Your examples are very abstract so I just said, that I can't see a real world example where you are going to pause the game and then in the "background" while game is paused to do "some" operations ... Everything was very abstract, so I asked for real world example. Sorry about confusion though :) – Whirlwind Dec 29 '15 at 19:45
  • @Whirlwind, best case I can think of off hand would be if you want events to carry out at certain intervals that are triggered via UIKit style game, with Sprite Kit being used as a graphics renderer only., and events inside SpriteKit are missed if the Scene is in a paused state (Like say some shiny gfx that appears).You want the triggers to continue firing because UIKit needs it, – Knight0fDragon Dec 29 '15 at 20:06

1 Answers1

1

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)

Dieblitzen
  • 544
  • 4
  • 22