I just started to learn cocos2d-iphone 2.x.
When try to make an animation of a CCSprite, error accured.
My code as below:
CCTexture2D *texture=[[CCTextureCache sharedTextureCache] addImage:@”dragon.png”];
CCSpriteBatchNode *spriteSheet=[CCSpriteBatchNode batchNodeWithTexture:texture];
[self addChild:spriteSheet];
NSMutableArray *animFrames=[[NSMutableArray alloc]init];
for (int i=1; i<8; i++) {
[animFrames addObject:[CCSprite spriteWithTexture:spriteSheet.texture rect:CGRectMake(i*75, 0, 75, 70)]];
}
CCAnimation *flyAnim=[CCAnimation animationWithSpriteFrames:animFrames delay:0.1f];
CGSize winSize=[CCDirector sharedDirector].winSize;
CCSpriteFrame *frame3=[CCSpriteFrame frameWithTexture:texture rectInPixels:CGRectMake(0, 0, 75, 70) rotated:NO offset:CGPointZero originalSize:CGSizeMake(75, 70)];
self.dragon=[CCSprite spriteWithSpriteFrame:frame3];
_dragon.position=ccp(winSize.width/2,winSize.height/2);
self.flyAction=[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:flyAnim]];
[_dragon runAction:_flyAction];
[spriteSheet addChild:_dragon];
Is there anything wrong?
in your loop, the line:
you should be adding CCSpriteFrame objects instead of CCSprite. When the action is run, the action manager thinks these are spriteFrame objects and sends them messages for that class.
It is customary to have the sprite frames declared in a in a .plist file, lets say ‘dragon.plist’. The plist can be created with zwoptex, or texturePacker or a similar tool (vi works fine). Then you can do this: