2010-06-06 82 views
2

我想在cocos2D中製作sprite animate。我相信我已經設置了動畫,但是如何在屏幕上繪製animating sprite?以下是我有:提前Cocos2d中的動畫。

id anim = [[[CCAnimation alloc] initWithName:@"char_walking" delay:1/12.0] autorelease]; 

[anim addFrame:@"run2.png"]; 
[anim addFrame:@"run1.png"]; 
[anim addFrame:@"run3.png"]; 
[anim addFrame:@"run4.png"]; 
[anim addFrame:@"run3.png"]; 
[anim addFrame:@"run1.png"]; 

id myAction = [CCAnimate actionWithAnimation:anim]; 
id repeating = [CCRepeatForever actionWithAction:myAction]; 

[character do:repeating]; 

character = [CCSprite spriteWithSpriteFrame:anim]; 
character.position = ccp(160, 240); 
[self addChild:character]; 

感謝, 約翰

回答

1

也許這只是一個剪切和粘貼錯誤,但它看起來像你說的是精靈之前重複動畫你創建它,所以你添加到節點的字符精靈永遠不會獲得發送給它的CCAnimate動作。

+0

這就是我所做的,我覺得很愚蠢。 – tallen11 2011-02-13 23:37:55

0

由於addFrame方法需要,您並未添加spriteFrame。

與此行:

[字符做:重複];

也許你正在尋找[character runAction:repeating];

字符= [CCSprite spriteWithSpriteFrame:動畫]。

在這裏,anim不是spriteFrame,它是一個CCanimation。

基本上,你有幾個問題。

你可以嘗試這樣的事情用zwoptex創建您的.plist文件:

CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache]; 

[cache addSpriteFramesWithFile:@"runImages.plist"]; 

CCSprite *startingImage = [CCSprite spriteWithSpriteFrameName:@"run1.png"]; 

[self addChild:startingImage]; 

//創建你的精靈幀

NSArray *animFrames = [[NSArray alloc] initWithCapacity:6]; 

[animFrames addFrame:[cache spriteFrameByName:@"run2.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run1.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run3.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run4.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run3.png"]]; 
[animFrames addFrame:[cache spriteFrameByName:@"run1.png"]]; 

//運行動畫

CCAnimation *animation = [CCAnimation animationWithName:@"char_walking" delay:1/12.0 frames:animFrames]; 
id anim = [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO];  
[startingImage runAction:anim];