Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-14-2010, 06:22 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 8
iabaksardna is on a distinguished road
Smile Simple Question on Variables

Hey guys,
I'm trying to make a game in which the following statement is usd:

Code:
[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
Instead of the 0.001, I want to have a variable that can be modified by the user. Can anyone help?
Thanks, Andy
iabaksardna is offline   Reply With Quote
Old 06-14-2010, 06:59 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iabaksardna View Post
Hey guys,
I'm trying to make a game in which the following statement is usd:

Code:
[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
Instead of the 0.001, I want to have a variable that can be modified by the user. Can anyone help?
Thanks, Andy

A couple of things:

NSTimers are fairly coarse. To quote from the XCode docs:

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

You won't get called anywhere near every .001 seconds. More like every .05 seconds, and don't expect the calls to be exact. There is quite a bit of variability in the time between calls, and the system may miss a timer interval completely sometimes.

The other thing is that you can't change the timer interval for a running repeating timer, at least not that I know of.

You are probably better off creating a one-shot timer, and when it goes off, create a new one. Then you can pick up a user-specified interval for the next timer you create.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-14-2010, 07:29 PM   #3 (permalink)
Eager to Learn Member
iPhone Dev SDK Supporter
 
mkenney's Avatar
 
Join Date: Sep 2009
Posts: 76
mkenney is on a distinguished road
Default

Follow-up question:

In Cocos2d the timers actually passed a delta variable that one could use to see how long it actually was between timer calls. Is there something similar to that for NSTimers??

Mark

P.S. If wrong place to ask this, please say so and I wasn't sure...

This should work I think other then the .001 being to short of a time as Duncan stated.:
Code:
float timerTime=.001;

[NSTimer scheduledTimerWithTimeInterval:timerTime target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
mkenney is offline   Reply With Quote
Old 06-14-2010, 07:42 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 8
iabaksardna is on a distinguished road
Default Thanks

Thanks Mark, helped a lot.
iabaksardna is offline   Reply With Quote
Old 06-14-2010, 08:10 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by mkenney View Post
Follow-up question:
In Cocos2d the timers actually passed a delta variable that one could use to see how long it actually was between timer calls. Is there something similar to that for NSTimers??
[/code]
I don't think there is any delta parameter.

It would be quite easy to generate one yourself however.

Just record the result from NSDate timeIntervalSinceReferenceDate when you start the timer and each time the timer fires, and compare the new value with the old value.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 06-14-2010, 08:49 PM   #6 (permalink)
Eager to Learn Member
iPhone Dev SDK Supporter
 
mkenney's Avatar
 
Join Date: Sep 2009
Posts: 76
mkenney is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
I don't think there is any delta parameter.

It would be quite easy to generate one yourself however.

Just record the result from NSDate timeIntervalSinceReferenceDate when you start the timer and each time the timer fires, and compare the new value with the old value.
Well that just makes too much sense

Thanks!!
mkenney is offline   Reply With Quote
Reply

Bookmarks

Tags
variable, variables

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 336
9 members and 327 guests
Absentia, Domele, fiftysixty, givensur, heshiming, linkmx, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0