0

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

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

播放器.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;
    }

游戏场景.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 (code=2, address=0xf

我能做些什么?提前致谢

4

1 回答 1

0

第一件事 - 你尝试使用自动释放的对象。animationOllie将在您的 init 方法之后释放。

第二个错误是你不能重用动作。当您需要运行操作时,您必须重新创建它。

于 2012-09-24T08:48:22.390 回答