Objective-c [Xcode] CCAnimation不工作,cocos2d

时间:2012-09-23 14:59:00

标签: objective-c xcode cocos2d-iphone

我在播放器类中声明了两个动画,我想从另一个类运行,但我不能在这里是我的代码(如果你不能在这里阅读它是在pastebin:http://pastebin.com/iy0eFMWL): player.h

    @interface Player : CCSprite {
    CCAnimate *animationOllie;
    CCRepeatForever *repeatNormal;
    }

player.m:

    @implementation Player

    -(id)initWithFile:(NSString *)filename
    {
if (self = [super initWithFile:filename]) {
    self.velocity = ccp(0.0, 0.0);


    CCAnimation *ollie = [CCAnimation animation];
    [ollie setDelayPerUnit:0.05f];
    [ollie addSpriteFrameWithFilename:@"ollie1.png"];
    [ollie addSpriteFrameWithFilename:@"ollie2.png"];

    animationOllie = [CCAnimate actionWithAnimation:ollie];


    CCAnimation *normal = [CCAnimation animation];
    [normal setDelayPerUnit:0.05f];
    [normal addSpriteFrameWithFilename:@"normal1.png"];
    [normal addSpriteFrameWithFilename:@"normal2.png"];
    CCAnimate *animationNormal = [CCAnimate actionWithAnimation:normal];

    repeatNormal = [CCRepeatForever actionWithAction:animationNormal];

    [self runAction:repeatNormal];

        }
        return self;
    }
    -(void)animateThePlayer {
        [self stopAction:repeatNormal];
        [self runAction:animationOllie];
    }

在GameScene课程中: GameScene.h:

    @interface GamePlayLayer : CCLayerColor {
        float yVel;
    }

GameScene.m:

    #import "Player.h"

    @interface GamePlayLayer()
    {
        Player *player;
     }

    @end

    @implementation GamePlayLayer

    -(id) init
    {
        if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {

    player = [[Player alloc] initWithFile:@"normal1.png"];

    [self addChild:player];

    self.isTouchEnabled = YES;

    player.position = ccp(85,70);


    [self schedule:@selector(update:)];

        }
        return self;
    }

    -(void)update:(ccTime)dt {

        if (player.position.y > 70) {
            yVel -= 0.1;
        }
else {
    if (yVel != 5.5) {
        yVel = 0;
        player.position = ccp(player.position.x, 70);
    }
}
player.position = ccp(player.position.x, player.position.y + yVel);

    }


    - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    yVel = 5.5;
        [player animateThePlayer];
    }

就是这样,它构建得很好,一切正常但是当我点击它崩溃时我得到了这个消息:

  

0x1df609b:movl 8(%edx),%edi线程1:EXC_BAD_ACCESS(代码= 2,   地址= 0xf

我该怎么办?提前致谢

1 个答案:

答案 0 :(得分:0)

首先 - 你尝试使用自动释放的对象。 {init}方法将释放animationOllie

第二个错误是您无法重复操作。当您需要运行操作时,您必须重新创建它。

相关问题