0

I have a project in Sprite Kit and I am trying to make it so that a timer gradually decreases. For example if the timer float variable is set at 3.0, this would gradually decrease and be at 0, 3 seconds later. With the way that updates work in sprite kit its a horrible mess trying to get an integer to gradually decrease.

For example:

time+=1;

If I was to put this in an update void, it would increment extremely quickly and differently depending on frames and so forth. Is there a way I can Increment or Decrement a value at a steady rate no despite the fps in Sprite Kit?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

You'd be better off getting the current time with each update and comparing it to some initial time to determine when 3 seconds pass.

Declare an ivar in your SKScene subclass:

@implementation MyScene {
    NSDate* _timestamp;
}

When your timer starts:

_timestamp = [NSDate timeIntervalSinceReferenceDate];

Check your timer in your update pass:

- (void)update:(NSTimeInterval)currentTime {
    if(_timestamp != nil && currentTime - _timestamp.timeIntervalSinceReferenceDate >= 3.0) {
        // Perform your timer event
    }

    // Other updates
}
Charles A.
  • 10,685
  • 1
  • 42
  • 39
  • Note that update() in SpriteKit actually passes in the time as a parameter, so you don't even need to go out to NSDate.timeIntervalSinceReferenceDate. – cc. Oct 02 '15 at 22:24
  • I have a variable called timestamp which is a float but I can't assign it to NSDate.timeIntervalSinceReferenceDate()? –  Oct 02 '15 at 23:05
  • You want an NSTimeInterval type, not a float. – Charles A. Oct 02 '15 at 23:06
  • NSTimeInterval still gives me an error bud :) error: "Called object type 'NSTimeInterval' (aka 'double') is not a function or function pointer –  Oct 02 '15 at 23:46
  • What exactly is the line of code that's causing the error? – Charles A. Oct 02 '15 at 23:47
  • timestamp = NSDate.timeIntervalSinceReferenceDate() –  Oct 02 '15 at 23:48
  • I don't think timeIntervalSinceReferenceDate() is a function in NSDate?? –  Oct 03 '15 at 00:06
  • It's a function on the `NSDate` type and a property on an `NSDate` instance. It's in the [docs for NSDate](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/#//apple_ref/occ/clm/NSDate/timeIntervalSinceReferenceDate). – Charles A. Oct 03 '15 at 00:10
  • Well how would you go about creating it because right now, I have "NSTimeInterval timestamp;" at the top of my code and then "timestamp = NSDate.timeIntervalSinceReferenceDate()" in an if statement in the update and it gives me a red error and won't let me run it. –  Oct 03 '15 at 00:16
  • Are you writing swift code? If so, I would expect you to need a line like `var timestamp: NSTimeInterval!`. I will update my answer. – Charles A. Oct 03 '15 at 00:21
  • no I am not at all :) Im in sprite kit as I said in my question. I declare "NSTimeInterval timestamp;" at the very top and "timestamp = NSDate.timeIntervalSinceReferenceDate()" in the update and this is the line that comes up red before I run it :) –  Oct 03 '15 at 00:25
  • Take a look at the update, that should do the job in Objective-C – Charles A. Oct 03 '15 at 00:31
  • haha there something going on in the update, its not liking the reference to the _timestamp variable now :/ if your not sure what this is don't worry, I might be able ti fix it but I will let you know If I can't –  Oct 03 '15 at 00:38
  • As in it doesn't compile? What line and what's the error? – Charles A. Oct 03 '15 at 00:40
  • no its an error shown in the editor before I run it, its to do with the time stamp variable "Member reference bas type is not a union or structure" on one of them and the other timestamp variable in the same line says "Invalid operands to binary expression and 'void" Im not sure if that will help you but thats what its telling me. btw I really appreciate you helping me –  Oct 03 '15 at 00:45
  • So this error is on the if statement in the update method? What if you change `_timestamp.timeIntervalSinceReferenceDate` in the if statement to `[_timestamp timeIntervalSinceReferenceDate]`? – Charles A. Oct 03 '15 at 00:48
  • It just doesn't like the variable referencing or something because even If I just shorten it to "if(timeStamp != nil)" it still gives me an error –  Oct 03 '15 at 12:05
  • I'd probably have to see the whole file to help. Hard for me to speculate. Also, _timestamp and timeStamp are quite different variable names, so it might just be inconsistency between your declaration and use? – Charles A. Oct 03 '15 at 15:51
  • Oh no sorry its just I changed them to make it easy, everything has remained consistent though. Here is a link to an image of the error I get: https://www.dropbox.com/s/rk56rbq5ih694x4/Screen%20Shot%202015-10-03%20at%2017.22.49.png?dl=0 –  Oct 03 '15 at 16:27
  • @CharlesA. any ideas? –  Oct 04 '15 at 20:21
  • The type of timestamp in that screenshot is `NSTimeInterval`. When I changed the code to Objective-C, one of the things I meant to change was the type to `NSDate*` instead of `NSTimeInterval`. I have updated the code to be correct, sorry for the trouble. – Charles A. Oct 05 '15 at 19:33