运行操作动画崩溃问题 - 缺少什么



需要一些帮助来解决Cocos2d问题

我正在运行来自相同精灵表的动画,需要精灵播放不同的动画动画,当它向左向右,向上和向下移动。

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

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

这是我崩溃的代码片段。有人能给我点提示吗?

谢谢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

您必须先停止_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];
}