在CCSprite子类中实现动画的正确方法是什么?

时间:2012-08-10 13:00:28

标签: objective-c cocos2d-iphone

在我的游戏中,我为我的敌人制作了CCSprite(cocos2d)的子类。 因为我想控制动画,所以在这个子类的每个实例中,我必须在我的主类中翻译我的动画代码, 敌人,这个子类。

我觉得这很难,但是......经过一段时间它神奇地开始工作了。

不幸的是,在这个子类中创建和设置属性后,我开始发生奇怪的崩溃。因为他们在cocosDenshion和其他与我无关的地方 在我的代码和网络上深入研究之后,我确信它有某种数据损坏,我几乎完全肯定它,因为我完全用错误的方式完成了他的动画代码。< / p>

说实话,我甚至无法将我的想法包围在这里,以及这实际上是如何运作的: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

------------------- EDIT --------------------

我在Ray Wenderlich的教程的帮助下改变了我的代码:http://www.raywenderlich.com/3888/how-to-create-a-game-like-tiny-wings-part-1

这是我认为它应该更接近它应该是什么。不幸的是,它仍然在我的iphone(而不是模拟器)上在SimpleAudioEngine(我实现正确)上崩溃,所以我仍然做错了。

在Npc类之上:

@synthesize batchNode = _batchNode;

Npc类的初始化:

-(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;
}

2 个答案:

答案 0 :(得分:2)

你在loadAnimation中重新分配自己:

 self = [Npc spriteWithTexture:sheet.texture];

那时我停止阅读代码。由于self已经是Npc类的一个实例,你必须问自己为什么要在像loadAnimation这样的Npc实例方法中这样做。

答案 1 :(得分:0)

所以,我明白了......这是代码:

<强> 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"];

在Npc类中,但对我来说这是太高级了。如果有人知道这一点,请告诉我......实际上要了解更好,更好地了解oop。 但这并不是绝对必要的......