I'm working on an iPhone Game where the player tilts the iPhone to make the character move, but unfortunately all of the timers I'm using to animate the scenario are slowing down my game. I've been told to use NSThreads, but I really don't know anything about them. My question is, What are the differences between using NSThreads and NSTimers? Or what are the advantages of using NSThreads? How are they useful?
2 Answers
Timers are used for asynchronous, not concurrent, execution of code.
A timer waits until a certain time interval has elapsed and then fires, sending a specified message to a target object. For example, you could create an
NSTimer
object that sends a message to a window, telling it to update itself after a certain time interval.
Threads are for concurrent execution of code.
An
NSThread
object controls a thread of execution. Use this class when you want to have an Objective-C method run in its own thread of execution. Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application, which handles user interface and event-related actions. Threads can also be used to divide a large job into several smaller jobs, which can lead to performance increases on multi-core computers.
See also:
-
I see what you mean, I know how to work with NSTimers but I really don't know anything about NSThreads, how can I get started on this? I also don't want to lose the time intervals used in my NSTimers, as this is essential to the animations of my app. – Fernando Cervantes Sep 22 '11 at 23:15
Timers are objects that simply call methods at a later time. Threads are additional "streams" of stuff to be processed, in psuedo-parallel. It's like comparing apples to oranges—they're not really related. The only relation I can see is if you want to do some processing in the background, but you shouldn't be doing UI calls in background threads, Why are you using timers to make your character move? Knowing that, we might be able to supply an alternate solution.

- 16,250
- 7
- 45
- 84
-
Basically what I've done is I created a method called touchesHolded which is just a method that is ran by a timer which is started when you tap the buttons to make my character move. Inside touchesHolded I increase the X Variable and update my character's location. I'm not using [UIView beginAnimation] because that will not provide me with all the control I need. – Fernando Cervantes Sep 22 '11 at 23:12
-
What exactly is the timer keeping track of? You could just directly increment the location of the character… I couldn't help noticing you're using UIKit to draw the game? Maybe a game engine (for simpler games, Cocos2D works quite well) would simplify the process for you. – FeifanZ Sep 22 '11 at 23:20
-
Well the thing is that I want my character to slide across the screen when the user taps on the (Left/Right) button, and the timer just increases the x and also increases inertia so that the character slides out when the user releases their finger from the screen. – Fernando Cervantes Sep 22 '11 at 23:26
-
1How many timers are you using then? An alternative is to have a velocity variable that you increment by the acceleration constant every frame. Then increment the position by the velocity every frame. And repeat. – FeifanZ Sep 23 '11 at 00:32
-
-
How many things are those timers keeping track of? You could definitely do without them. In general though, 6 or 7 timers should be fine. You're probably just trying to do too much in between frames. Use Instruments to profile. The thing is though UIKit was never designed to run a smooth game—†hat could explain the lag. You'd have to use something more robust, like Core Animation at the least for a simple game, or more likely OpenGL ES. Try something like Cocos2D, which wraps OpenGL ES in a nicer API. – FeifanZ Sep 23 '11 at 10:21