Game Development Reference
In-Depth Information
Since playing an animation when activated by a trigger will be a common use case, it
makes sense to write a generic class
PlayTimelineWhenTriggered
. But before you
write this class, open
Saw1_noautoplay.ccb
in SpriteBuilder, and then select the
root node. Do
not
select the saw sprite. Switch to the
Item Code Connections
tab,
and enter
PlayTimelineWhenTriggered
in the
Custom class
field.
Caution
The
PlayTimelineWhenTriggered
class inherits from
CCNode
. This is why it won't work as a custom class of a sprite node that in-
herits from
CCSprite
. Custom classes for sprite nodes need to inherit from
the
CCSprite
class. The same is true for other node types—for instance, the
custom class of a button needs to inherit from
CCButton
.
You should return to Xcode now to add the
PlayTimelineWhenTriggered
class.
Right-click the
Source
group and select
New File
. Once again, you'll go through the
dialogs in
Figure 2-9
and
Figure 2-10
. Name the class
PlayTimelineWhenTriggered
, and
make it a
Subclass
of
CCNode
before clicking
Next
to create the files. Open the
PlayTimelineWhenTriggered.h
file, and enhance it with the code highlighted in
Listing 5-15
.
Extending the PlayTimelineWhenTriggered class to become a TriggerDeleg-
ate
#import "CCNode.h"
#import "Trigger.h"
@interface PlayTimelineWhenTriggered : CCNode
<TriggerDelegate>
@property NSString* timelineName;
@end
Adopting the
TriggerDelegate
protocol is necessary to allow instances of this class
to be used as a target by the
Trigger
class. The
timelineName
property is just to al-
low more flexibility for this class.
If
timelineName
is
nil
or an empty string, the class will play the
Default
Timeline
when activated; otherwise, it will attempt to play the Timeline whose name is
given by
timelineName
. In any node where you set the
PlayTimelineWhenTriggered
class as its
Custom class,
you can then add a
Search WWH ::
Custom Search