To add sprites from spritesheet and animating the frames, you can use the following code:
in .h:
CCSprite *sprite_1;
NSMutableArray *spriteAnimArray;
in .m:
//In init method, add sprite frames from plist file
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"plistName.plist"];
sprite_1 =[CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
sprite_1.position = ccp(240,240);
[self addChild:sprite_1 z:2];
[self spriteAnimation]; // call sprite animation function
//SpriteAnimation function
-(void)spriteAnimation{
spriteAnimArray = [NSMutableArray new];
// call sprite1 to sprite4 from spritesheet //
for(int i=1; i<=4; s++)
{
[spriteAnimArray addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:[NSString stringWithFormat:@"sprite%d.png",i]]];
}
CCAnimation *animate = [CCAnimation animationWithFrames:spriteAnimArray delay:0.3f];
CCRepeat *sprite_Action = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:animate
restoreOriginalFrame:YES]];
[sprite_1 runAction:sprite_Action];
[spriteAnimArray release];
}
in .h:
CCSprite *sprite_1;
NSMutableArray *spriteAnimArray;
in .m:
//In init method, add sprite frames from plist file
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"plistName.plist"];
sprite_1 =[CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
sprite_1.position = ccp(240,240);
[self addChild:sprite_1 z:2];
[self spriteAnimation]; // call sprite animation function
//SpriteAnimation function
-(void)spriteAnimation{
spriteAnimArray = [NSMutableArray new];
// call sprite1 to sprite4 from spritesheet //
for(int i=1; i<=4; s++)
{
[spriteAnimArray addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:[NSString stringWithFormat:@"sprite%d.png",i]]];
}
CCAnimation *animate = [CCAnimation animationWithFrames:spriteAnimArray delay:0.3f];
CCRepeat *sprite_Action = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:animate
restoreOriginalFrame:YES]];
[sprite_1 runAction:sprite_Action];
[spriteAnimArray release];
}
No comments:
Post a Comment