HTML and CSS Reference
In-Depth Information
Listing 24-2.
Move a Sprite 50 Pixels to the Right and 10 Pixels to the Top over a Period of 2 Seconds
var animation = cc.Animation.create ( ) ;
for ( var i = 1 ; i < 15 ; i ++ ) {
var frameName = "res/Images/grossini_dance_" + ( ( i < 10 ) ? ( "0" + i ) : i ) + ".png" ;
animation.addSpriteFrameWithFile ( frameName ) ;
}
animation.setDelayPerUnit ( 2.8 / 14 ) ;
animation.setRestoreOriginalFrame ( true ) ;
var action = cc.Animate.create ( animation ) ;
sprite.runAction ( cc.Sequence.create ( action, action.reverse ( ) ) ) ;
Scheduler
The
Scheduler
is responsible for triggering the scheduled callbacks. There are two different types of callbacks
(selectors) in Cocos2d:
•
Update Selector
: The update selector will be called on every frame. You can customize its
priority: the lower the value, the earlier it is called.
•
Custom Selector
: A custom selector will be called on every frame, or with a customized interval
of time.
Custom selectors should be avoided whenever possible. Update selectors are faster and consume less memory.
Touch Event
Cocos2d supports two different ways of handling touch events, which are described in
CCTouchDelegateProtocol.js
.
There is the
TargetedTouchDelegate
event and the
StandardTouchDelegate
event.
TargetedTouchDelegate
offers two benefits:
•
You don't have to deal with an event set, as the dispatcher does the job of splitting them. You
get exactly one
cc.Touch
per call.
cc.Touch
by returning true in
onTouchBegan
. Updates of claimed touches are
sent only to the delegate(s) that claimed them. So if you get a move/ended/cancelled update,
you can be certain that it's your touch. This frees you up from having to do numbers checks
when using multi-touch.
StandardTouchDelegate
also has two benefits:
•
You can claim a
cc.Touch
from the
•
Splitting an event set is something that you must do. You can get each
event set.
cc.TouchesBegan
. All of your touch callbacks will be
•
You don't need to state true or false in
called when you touch the screen.
The difference between
TargetedTouchDelegate
and
StandardTouchDelegate
is as follows:
TargetedTouchDelegate
processes touch events one by one, while
StandardTouchDelegate
processes touch events in an event set. Thus when you need to process a multi-touch, you
should choose
StandardTouchDelegate
.
•
TargetedTouchDelegate
has a higher priority to get a touch event than
StandardTouchDelegate
.
•