2014-02-17 4 views
4

나는 애니메이션 스프라이트를 만들려고 노력 해왔다. 그것들은 많은 튜토리얼이지만 Cocos2d 2.x를위한 것이다. 내 스프라이트 시트 flappbird.png 이름과 .plist는 flappbird.plist 이름스프라이트 프레임 애니메이션 Cocos2d 3.0

나는이 내 초기화에 방법이다,이 코드를하지만 난 그냥 충돌 장면을 시작할 때마다이

// ----------------------------------------------------------------------- 

_player = [CCSprite spriteWithImageNamed:@"monster1.png"]; // comes from your .plist file 
_player.position = ccp(self.contentSize.width/28,self.contentSize.height/2); 
_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1 
_player.physicsBody.collisionGroup = @"playerGroup"; 
_player.physicsBody.type = CCPhysicsBodyTypeStatic; 
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"monster1.png"]; 
[batchNode addChild:_player]; 
[self addChild:batchNode]; 

NSMutableArray *animFrames = [NSMutableArray array]; 
for(int i = 1; i < 5; i++) 
{ 
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"flapbird%d.png",i]]; 
    [animFrames addObject:frame]; 
} 

CCAnimation *animation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.2f]; 
[_player runAction:[CCActionRepeatForever actionWithAction:[CCActionAnimate actionWithAnimation:animation]]]; 

[_physicsWorld addChild:_player]; 

// ----------------------------------------------------------------------- 
+2

중복 가능성을 [어떻게적인 Cocos2D 3.x의에서 CCSprite 애니메이션 없습니다? ] (http://stackoverflow.com/questions/21645953/how-to-animate-ccsprite-in-cocos2d-3-x) –

+0

나는 분에 그 질문의 대답을 구현하려했지만 충돌이 일어나고 아무 일도 일어나지 않을 것이다. – Crazycriss

+0

그것이 충돌 할 때 당신에게 무엇을 말합니까? – connor

답변

8

적인 Cocos2D 3.0 spritesheet와

애니메이션 스프라이트 =

는 또한 self.userInteractionEnabled 후 스프라이트 시트를 추가 코드의 시작 부분에 #import "CCAnimation.h"를 추가해야합니다 예; 스프라이트이 누군가하는 데 도움이

//The sprite animation 
NSMutableArray *walkAnimFrames = [NSMutableArray array]; 
for(int i = 1; i <= 7; ++i) 
{ 
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"monster%d.png", i]]]; 
} 
CCAnimation *walkAnim = [CCAnimation 
         animationWithSpriteFrames:walkAnimFrames delay:0.1f]; //Speed in which the frames will go at 

//Adding png to sprite 
monstertest = [CCSprite spriteWithImageNamed:@"monster1.png"]; 

//Positioning the sprite 
monstertest.position = ccp(self.contentSize.width/2,self.contentSize.height/2); 

//Repeating the sprite animation 
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim]; 
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction]; 

//Animation continuously repeating 
[monstertest runAction:repeatingAnimation]; 

//Adding the Sprite to the Scene 
[self addChild:monstertest]; 

희망이 될 곳은 init에

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"your.plist"]; 

없음이 모든 추가의 D 건배

+0

이미 [CCSpriteBatchNode batchNodeWithFile :] 호출의 일부로 캐시에 모든 프레임을 추가 한 경우 다음 줄이 나타납니다. monstertest = [...]; monstertest = [CCSprite spriteWithSpriteFrameName :]을 읽고 첫 번째 프레임의 이름을 전달할 수 있습니다. 이렇게하면 스프라이트의 초기 이미지가 .png에서 별도로 메모리로로드되는 대신 캐시에서 꺼내집니다. – PKCLsoft

+0

@ PKCLsoft 이전 버전이었습니다. 3.x에서 그들은 둘 다 spriteWithImageNamed를 사용하고 그것이 작동하는 방식은 캐시에 존재하면 캐시가 사용된다는 것입니다. – agro1986

+0

@ agro1986 감사합니다. 중요한 정보를 놓쳤습니다. – PKCLsoft

관련 문제