Game Development Reference
In-Depth Information
-(void) didLoadFromCCB
{
_triggerFrame = [CCSpriteFrame frameWithImageNamed:
@"SpriteSheets/Anims/facepalm42.png"];
}
The frameWithImageNamed: method will return the cached image if it has already
been loaded. This will be the case if the image is in the same Sprite Sheet as the sprite
frame that the sprite initially uses. That would be the sprite frame that's assigned to the
sprite in SpriteBuilder.
The frameWithImageNamed: method and similar “look up by name” methods per-
form at least a string comparison. More likely, they'll also run some name-mangling code
in order to locate the image resource, which could theoretically be found in a number of
different locations.
Therefore, it's recommended to avoid running frameWithImageNamed: and similar
methods while the gameplay is underway. Loading resources at runtime is a common
source of short, intermittent drops in frame rate (appropriately nicknamed hiccups ).
With a strong reference to the sprite frame in question assigned to _triggerFrame ,
you can now override setSpriteFrame: and compare the incoming spriteFrame
parameter with the one referenced by _triggerFrame .
If both are equal, that's when you can fire a method to run any code that's supposed to be
synchronized with specific sprite frames. In Listing 12-5 , that would play a sound effect
specific to the animation.
Listing 12-5 . Running a method when the new spriteFrame matches a specific sprite
frame
-(void) setSpriteFrame:(CCSpriteFrame *)spriteFrame
{
[super setSpriteFrame:spriteFrame];
if ([_triggerFrame isEqual:spriteFrame])
{
[self playFacepalmSoundEffect];
}
}
Search WWH ::




Custom Search