// Import the interfaces
#import "HelloWorldLayer.h"
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
#import "AnimatedSprite.h"
#pragma mark - HelloWorldLayer
// HelloWorldLayer implementation
@implementation HelloWorldLayer
// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
self.touchEnabled = YES;
// Load the pig spritesheet
[self loadSpritesheet:@"pig"];
// Create the pig instance with a default sprite
pig = [AnimatedSprite spriteWithSpriteFrameName:@"pig_walk.swf/0000"];
// Init a looping animation
[pig addLoopingAnimation:@"walk" frame:@"pig_walk.swf/%04d" delay:0.05f];
// Init an animation
[pig addAnimation:@"dead" frame:@"pig_dead.swf/%04d" delay:0.05f target:self callback:@selector(removePig)];
// Set the position of the sprite
pig.position = ccp(200,200);
// Add the sprite in the scene
[self addChild:pig];
// Start the walk animation
[pig startAnimation:@"walk"];
}
return self;
}
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CCLOG(@"Start dead animation");
[pig startAnimation:@"dead"];
}
- (void) removePig {
CCLOG(@"Remove pig");
[pig removeFromParentAndCleanup:YES];
}
- (void) loadSpritesheet:(NSString *)spritesheetName {
[CCTexture2D PVRImagesHavePremultipliedAlpha:YES];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444];
NSString *pvrccz = [NSString stringWithFormat:@"%@.pvr.ccz", spritesheetName];
CCSpriteBatchNode *loadingBatchNode = [CCSpriteBatchNode batchNodeWithFile:pvrccz];
[self addChild:loadingBatchNode z:2];
NSString *plist = [NSString stringWithFormat:@"%@.plist", spritesheetName];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:plist];
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end