Cocos2d v3.0 - 使用物理编辑器和动画

时间:2014-04-25 19:22:21

标签: ios animation cocos2d-iphone

我正在尝试使用物理编辑器为我的精灵创建自定义物理,这些精灵是动画的。我正在关注https://www.codeandweb.com/blog/2014/04/09/using-physicseditor-with-cocos2d-v3的指南,但它没有说明如何处理动画。

下面是我拥有动画的代码:

//adding the png with all the sprites(run)
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"run-hd.plist"];
CCSpriteBatchNode *runSheet = [CCSpriteBatchNode batchNodeWithFile:@"run-hd.png"];
[self addChild:runSheet];

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

//Adding png to sprite
_character = [CCSprite spriteWithImageNamed:@"Run1-HDN.png"];
_character.position  = ccp(100,80);

//Repeating the sprite animation
CCActionAnimate *runAnimationAction = [CCActionAnimate actionWithAnimation:runAnim];
CCActionRepeatForever *runRepeatingAnimation = [CCActionRepeatForever actionWithAction:runAnimationAction];

//Animation continuously repeating
[_character runAction:runRepeatingAnimation];

//Adding the Sprite to the Scene
_character.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _character.contentSize} cornerRadius:0];
_character.physicsBody.collisionGroup = @"playerGroup";
_character.physicsBody.collisionType  = @"playerCollision";
_character.scale = 2;
[_physicsWorld addChild:_character];

我在Physics Editor中使用自定义物理主体创建了.plist,有没有办法将它添加到我的代码中?或者还有另一种方法可以完成这项工作吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

我明白了。万一有人需要知道,这就是我所做的。首先,我需要将https://github.com/CodeAndWeb/PhysicsEditor-Cocos2D-V3/tree/master/PhysicsEditor-Cocos2D-V3/Classes中的GCCShapeCache类添加到我的项目中。然后我添加了

#import "GCCShapeCache.h" 

到我的HelloWorldScene.m类,并将在Physics Editor中创建的.plist文件添加到我的项目中。然后我添加了

[[GCCShapeCache sharedShapeCache] addShapesWithFile:@"run.plist"];
[[GCCShapeCache sharedShapeCache] setBodyWithName:@"Run1-HDN" onNode:_character];

我的自定义形状已添加到我的项目中。