runAction动画崩溃问题 - 缺少什么

时间:2011-08-26 00:14:59

标签: iphone animation cocos2d-iphone sprite

需要一些Cocos2d问题的帮助。

我正在使用相同的精灵表运行动画,需要精灵才能发挥不同的效果 动画从左到右,向上和向下移动。

我尝试了一些不同的方法来实现这一点,下面的代码应该是最接近的版本,它应该工作。但是,它现在崩溃了(可能是内存泄漏)。

我最初使用的动画代码来自http://www.raywenderlich.com博客网站的精彩教程。

以下是我遇到的代码片段。 谁能给我任何关于缺少什么的指示?

THX Richg

#import "HelloWorldScene.h"

// HelloWorld implementation
@implementation HelloWorld
@synthesize gameMan = _gameMan;
@synthesize moveAction = _moveAction;
@synthesize walkAction = _walkAction;
@synthesize walkAction2 = _walkAction2;


+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}

// on "init" you need to initialize your instance
-(id) init
{

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

    [[CCSpriteFrameCache sharedSpriteFrameCache]    addSpriteFramesWithFile:@"gameMan.plist"];

    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode   batchNodeWithFile:@"gameMan.png"];
    [self addChild:spriteSheet];

    //Loop thru frames and add a cache of the frames
    NSMutableArray *walkAnimFrames =[NSMutableArray array];
    for (int i = 1; i <= 4; ++i) {
        [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
                                   [NSString stringWithFormat:@"Man%d.png", i]]];
    }

        CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];    



    NSMutableArray *walkAnimFrames2 =[NSMutableArray array];
    for (int i = 1; i <= 2; ++i) {
        [walkAnimFrames2 addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
                                   [NSString stringWithFormat:@"Man_up%d.png", i]]];

    }
    CCAnimation *walkAnim2 = [CCAnimation animationWithFrames:walkAnimFrames2 delay:0.1f];  



    //Create the sprite and run the animation                    
    CGSize winSize = [CCDirector sharedDirector].winSize;
    self.gameMan = [CCSprite spriteWithSpriteFrameName:@"Man1.png"];

    _gameMan.position = ccp(winSize.width/2, winSize.height/2);
    self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    self.walkAction2 = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim2 restoreOriginalFrame:NO]];
    //[_Man runAction:_walkAction
    //[self addChild:spriteSheet];
    [spriteSheet addChild:_gameMan];

    self.isTouchEnabled = YES;

}
return self;
}

-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 
                                           swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
//Methods for controlling the animation

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {  
//determine touch location  
CGPoint touchLocation = [touch locationInView: [touch view]];       
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

//set velocity, time it takes for the sprite across the iPhone screen
float manVelocity = 480.0/4.0;
//figure out amount moved in X and Y
CGPoint moveDifference = ccpSub(touchLocation, _gameMan.position);
//Figure out actual length moved
float distanceToMove = ccpLength(moveDifference);
//Figure out how long move will take
float moveDuration = distanceToMove / manVelocity;
//if moving other direction, flip direction of sprite
if (moveDifference.x < 0) {
    _gameMan.flipX = YES;
} else {
    _gameMan.flipX = NO;
}
  

这是重要的部分---在此之间

if (abs(moveDifference.x) > abs(moveDifference.y)) {

    [_gameMan runAction:_walkAction];

else {

    [_gameMan runAction:_walkAction2];  
}

到这里!

[_gameMan stopAction:_moveAction];

if (!_moving) {
    [_gameMan runAction:_walkAction];
}


self.moveAction = [CCSequence actions:                          
                   [CCMoveTo actionWithDuration:moveDuration position:touchLocation],
                   [CCCallFunc actionWithTarget:self selector:@selector(manMoveEnded)],
                   nil];

[_gameMan runAction:_moveAction];   
_moving = TRUE;

}

-(void)manMoveEnded {
[_gameMan stopAction:_walkAction];
_moving = FALSE;

}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
self.gameMan = nil;
self.walkAction = nil;
self.walkAction2 = nil;

// don't forget to call "super dealloc"
[super dealloc];
}
@end

1 个答案:

答案 0 :(得分:0)

你必须先停止_walkAction和_walkAction2才能运行它们。

  

由于未捕获的异常而终止应用   'NSInternalInconsistencyException',原因:'runAction:已经是Action   运行

此行表示操作已在运行,cocos2d无法再次运行。因此,在运行每个动作之前,您必须确保它已经完成或停止它。 修复应该如下所示:

if (abs(moveDifference.x) > abs(moveDifference.y)) {
    [_gameMan stopAction:_walkAction];
    [_gameMan runAction:_walkAction];
else {
    [_gameMan stopAction:_walkAction2];
    [_gameMan runAction:_walkAction2];
}