在CCSprite子类中实现动画的正确方法是什么?
在我的游戏中,我为我的敌人制作了一个CCSprite(cocos2d)的子类。 因为我想控制动画,所以从这个子类的每个实例中,我必须将我的主类中的动画代码翻译成这个子类,并将敌人加载到这个子类中。在CCSprite子类中实现动画的正确方法是什么?
我发现这相当困难,但......一段时间后,它神奇地开始工作。
不幸的是,在创建和设置这个子类的属性后,我开始出现奇怪的崩溃。因为他们在cocosDenshion和其他与我的敌人课程无关的地方,经过我的代码和网络的深入研究,我确信它的某种数据损坏,我几乎完全肯定它的因为我完全用错误的方式完成了他的动画代码。
说实话,我甚至无法将我的想法放在这里,以及这个实际上如何工作:S ...我完全卡住了。任何帮助深表感谢!
所以我的主要问题是:什么是在CCSprite子类中实现动画的正确方法? /我在这里做错了什么?
简化我的代码在这里:(它触发动画每2秒,以显示我多么想 使用它)
#import "cocos2d.h"
@interface Npc : CCSprite
{
CCAction *_runAnimation;
NSString* name;
int strength;
}
@property (nonatomic, retain) CCAction *runAnimation;
@property int strength;
@property (nonatomic, retain) NSString* name;
- (Npc*)loadAnimation;
- (void)animate;
@end
#import "Npc.h"
@implementation Npc
@synthesize runAnimation = _runAnimation;
@synthesize name;
@synthesize strength;
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
if((self=[super initWithTexture:texture rect:rect]))
{
}
return self;
}
- (Npc*)loadAnimation
{
int lastFrame = 11;
NSString *creatureFile = @"vis 1";
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
[NSString stringWithFormat:@"%@.plist", creatureFile]];
CCSpriteBatchNode* sheet = [CCSpriteBatchNode batchNodeWithFile:
[NSString stringWithFormat:@"%@.png", creatureFile]];
self = [Npc spriteWithTexture:sheet.texture];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
for (int x = 0; x < lastFrame; x++)
{
[animFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"%@ %d.png", creatureFile, x]]];
}
CCAnimation* anim = [CCAnimation animationWithFrames: animFrames delay: 0.1];
self.runAnimation = [CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO];
[self runAction:_runAnimation];
return self;
}
- (void)animate
{
[self runAction:self.runAnimation];
}
- (void)dealloc
{
[super dealloc];
[name release];
}
@end
#import "HelloWorldLayer.h"
#import "Npc.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if((self=[super init]))
{
timer = 0;
creatureTemp = [Npc spriteWithFile:@"Icon.png"];
creature = [creatureTemp loadAnimation];
creature.position = ccp(100,100);
[self addChild:creature];
[self schedule:@selector(nextFrame:)];
}
return self;
}
- (void)nextFrame:(ccTime)dt
{
timer += dt;
if (timer > 2.)
{
[creature animate];
timer = 0.;
}
}
- (void) dealloc
{
[super dealloc];
}
@end
- -----------------编辑--------------------
我改变了我的代码有教程的帮助雷Wenderlich:http://www.raywenderlich.com/3888/how-to-create-a-game-like-tiny-wings-part-1
这是我认为更接近它应该是什么。不幸的是,它仍然在我的iPhone(不是模拟器)上在SimpleAudioEngine上崩溃(我正确实施),所以我仍然做错了什么。在NPC类的顶部
:
@synthesize batchNode = _batchNode;
全国人大类的初始化:
-(id) initNpc
{
if((self=[super initWithSpriteFrameName:@"vis 1 0.png"]))
{
_normalAnim = [[CCAnimation alloc] init];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
int lastFrame = 11;
for (int x = 0; x < lastFrame; x++)
{
[animFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"vis 1 %d.png", x]]];
}
_normalAnim = [CCAnimation animationWithFrames: animFrames delay: 0.1];
self.runAnimation = [CCAnimate actionWithAnimation:_normalAnim restoreOriginalFrame:NO];
}
return self;
}
和的HelloWorldLayer
-(id) init
{
if((self=[super init]))
{
timer = 0;
_batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"];
[self addChild:_batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"];
creature = [[[Npc alloc] initNpc] autorelease];
creature.position = ccp(200,200);
[_batchNode addChild:creature];
[self schedule:@selector(nextFrame:)];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"super1.mp3"];
}
return self;
}
于是,我知道了....这里是代码:
NPC。H:
#import "cocos2d.h"
@interface Npc : CCSprite
{
CCAction *_runAnimation;
CCAnimation *_normalAnim;
CCAnimate *_normalAnimate;
}
@property (nonatomic, retain) CCAction *runAnimation;
- (void)animate;
- (id)initNpc;
@end
Npc.m
@synthesize runAnimation = _runAnimation;
@synthesize batchNode = _batchNode;
-(id) initNpc
{
if((self=[super initWithSpriteFrameName:@"vis 1 0.png"]))
{
_normalAnim = [[CCAnimation alloc] init];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
int lastFrame = 7;
for (int x = 0; x < lastFrame; x++)
{
[animFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"vis 1 %d.png", x]]];
}
_normalAnim = [CCAnimation animationWithFrames: animFrames delay: 0.1];
self.runAnimation = [CCAnimate actionWithAnimation:_normalAnim restoreOriginalFrame:NO];
}
return self;
}
- (void)animate
{
[self runAction:_runAnimation];
}
在HelloWorldLayer.m
-(id) init
{
if((self=[super init]))
{
_batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"];
[self addChild:_batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"];
timer = 0;
creature = [[[Npc alloc] initNpc] autorelease];
creature.position = ccp(200,200);
[_batchNode addChild:creature];
[self schedule:@selector(nextFrame:)];
}
return self;
}
- (void)nextFrame:(ccTime)dt
{
timer += dt;
if (timer > 2.)
{
[creature animate];
timer = 0.;
}
}
以及有关cocosDenshion怪异的崩溃。这也解决了......它原来是SimpleAudioEngine中的一个已知错误,只有当我有一个异常断点处于活动状态时它才会抛出异常。解决方法:做我的声音,如果我需要一个异常断点,我注释掉的声音一类...
- 不得不说,我宁愿:
_batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"];
[self addChild:_batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"];
人大内上课,但这对我来说太过先进了。如果有人知道,让我知道...真的很了解,更好地理解oop。 但它不是绝对必要的...
你重新分配自己在init in loadAnimation:
self = [Npc spriteWithTexture:sheet.texture];
那时我停止阅读代码。由于self已经是Npc类的一个实例,所以你不得不问自己为什么要使用像loadAnimation这样的Npc实例方法。
感谢您的回复!我实际上已经意识到这一点,这是不好的......但我不知道如何以不同的方式做...你可能会指出我在正确的方向吗? – yurki 2012-08-11 12:59:58