Game Development Reference
In-Depth Information
}
The _didLoad flag prevents the updateTextureCoordinates method from being
run before didLoadFromCCB . This is important because the setup method called in
Listing 12-2 needs to run before any call to updateTextureCoordinates ; other-
wise, the _vertices pointer's memory would not have been allocated, resulting in a
crash.
Of course, setSpriteFrame: does get called at least once before
didLoadFromCCB , by CCBReader . At some point, CCBReader has to assign the
sprite's initial spriteFrame as set up in SpriteBuilder, and any assignments issued by
CCBReader itself need not update the texture coordinates.
Now you can build and run the game, and you'll see the player blink its eyes about every
three seconds, or whatever duration you set for the default Timeline in SoftBodyPlay-
er.ccb .
Comparing Sprite Frames
You may also want to override the setSpriteFrame: method in other CCSprite
subclasses if you want to run code when specific frames are being displayed but you don't
want to (or can't) use the Callbacks keyframes to do so.
You can get and store sprite frames in ivars (or arrays or dictionaries) which, when passed
into the setSpriteFrame: method, should run specific selectors. For instance, this
can be used to synchronize sound effects or speech to start with a specific sprite frame.
Note This is a theoretical example. You needn't add the following code to the
project.
Assuming that _triggerFrame is a CCSpriteFrame* ivar (it must be declared
without the __weak keyword), you can assign it in didLoadFromCCB of the CCS-
prite subclass in which you want to monitor an animation to play a sound effect when a
specific sprite frame is set. Listing 12-4 shows you how it's done. Take note that, as with
all resources, you have to specify the relative path to the file.
Listing 12-4 . Obtaining a sprite frame reference for a given animation image
Search WWH ::




Custom Search