Game Development Reference
In-Depth Information
Nodes can schedule messages, which is Objective-C lingo for calling a method. In
many cases, you'll want a particular update method to be running on a node in order to
do some processing, such as checking for collisions. The simplest way to schedule the
particular update method to be called every frame is like this, typically found in the
node's init method:
[self scheduleUpdate];
If a node has the update method scheduled, it will send the update message to your
class every frame. You must implement this particular method in your node class:
-(void) update:(ccTime)delta
// this method is called every frame
Dead simple, isn't it? Notice that the update method has a fixed signature, meaning it's
always defined exactly this way. The delta parameter is the elapsed time since the
method was last called. This is the preferred way to schedule updates that should take
place every frame, but there are reasons to use other update methods that give you
more flexibility.
If you want a different method to be run, or if you don't want the method to be called
every frame but every tenth of a second, you should use this method:
[self schedule:@selector(updateTenTimesPerSecond:) interval:0.1f];
This will send the updateTenTimesPerSecond message to your node class ten
times per second:
-(void) updateTenTimesPerSecond:(ccTime)delta
// this method is called according to its interval, ten times per second
Note that if interval is 0, you should use the scheduleUpdate method instead
because it's slightly faster. It's faster because cocos2d has optimizations for the com-
mon update selector if it's scheduled with scheduleUpdate , whereas all other
Search Nedrilad ::

Custom Search