2011-10-12 102 views
1

我的問題是動畫在精靈的移動過程中無法正常工作。 下面是我使用動畫無法正常工作(Cocos2d)

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {  

    [selSprite resumeSchedulerAndActions]; 
    CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
    [self selectSpriteForTouch:touchLocation]; 
    return TRUE;  
} 

- (void)selectSpriteForTouch:(CGPoint)touchLocation 
{ 
CCSprite * newSprite = nil; 

    for (CCSprite *sprite in movableSprite) { 
     if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {    
      newSprite = sprite; 
      break; 
     } 
    }  
    if (newSprite != selSprite) { 
     [selSprite stopAllActions]; 

     selSprite = newSprite; 

     _MoveableSpritetouch = TRUE; 
    } 

    if(_MoveableSpritetouch==TRUE) 
    { 
     movement=0; 
CGRect selRect=CGRectMake((SpriteX)-20.0,(SpriteY)-20.0,40.0,40.0); 
     if(CGRectContainsPoint(selRect, touchLocation)) 
     { 
      [selSprite stopAllActions]; 


     } 
if((selSprite==MarshallCar)&& (!(CGRectContainsPoint(selRect, touchLocation)))) 
     { 
      movement=1; 
      [self reorderChild:selSprite z:5]; 


      NSMutableArray *MarshallCarWalkAnimFrames = [NSMutableArray array]; 
      for(int i = MarshallCarTouchStartFrameIndex; i <= MarshallCarTouchEndFrameIndex; ++i) { 
       [MarshallCarWalkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"mcar_move_%d.png", i]]]; 
      } 
      MarshallCarWalkAnim = [CCAnimation animationWithFrames:MarshallCarWalkAnimFrames delay:MarshallCarTouchFrameDelay]; 
      walkMarshallCar = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:MarshallCarWalkAnim restoreOriginalFrame:NO]]; 


      [selSprite runAction:walkMarshallCar]; 
     } 
} 
} 

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {  


    if(gameState == TRUE){ 


     CGPoint point = [touch locationInView:[touch view]]; 
     point = [[CCDirector sharedDirector] convertToGL:point]; 
     if (moveDifference.x>0) 
        { 
         selSprite.flipX = YES; 
        } 

        else { 
         selSprite.flipX = NO; 
        } 
     [selSprite setPosition:point]; 


    } 
    } 

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    movement=0; 
if(selSprite==MarshallCar) 
    { 
     [selSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mcar_idle.png"]]; 

    } 
[selSprite pauseSchedulerAndActions]; 
} 

的動畫運動幀運動過程中不打,每次有時它起着或有時不是代碼。當您第一次觸摸並移動精靈時,它會正常播放,但如果觸摸另一個精靈,然後再移動上一個精靈,移動的動畫將無法播放。

有沒有人知道爲什麼會發生這種情況? 請告訴我刪除此錯誤的正確代碼。 謝謝!

回答

0
if((selSprite==MarshallCar)&& (!(CGRectContainsPoint(selRect, touchLocation)))) 
     { 
      movement=1; 
      [self reorderChild:selSprite z:5]; 


      NSMutableArray *MarshallCarWalkAnimFrames = [NSMutableArray array]; 
      for(int i = MarshallCarTouchStartFrameIndex; i <= MarshallCarTouchEndFrameIndex; ++i) { 
       [MarshallCarWalkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"mcar_move_%d.png", i]]]; 
      } 
      MarshallCarWalkAnim = [CCAnimation animationWithFrames:MarshallCarWalkAnimFrames delay:MarshallCarTouchFrameDelay]; 
      walkMarshallCar = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:MarshallCarWalkAnim restoreOriginalFrame:NO]]; 


      [selSprite runAction:walkMarshallCar]; 
     } 

我已添加[selSprite stopAllActions]; ,它開始正常工作,因爲在觸摸結束的方法中,我暫停了動作 但沒有恢復它們,所以當我第二次觸摸精靈時,它不是在播放動畫,因爲動作已暫停。

0

我相信你的問題是,如果/否則,如果結構:

if (_MoveableSpritetouch==TRUE) 
{ 
     CGRect selRect = CGRectMake(SpriteX - 20, SpriteY - 20, 40, 40); 
     if(CGRectContainsPoint(selRect, touchLocation)) 
     { 
      [selSprite stopAllActions]; 
     } 
     else if(...) 
     { 
      ... 
      [selSprite runAction:walkMarshallCar]; 
     } 
} 

如果你沒有看到它的時候了:如果觸摸位置是selRect裏面,你叫stopAllActions選定(新)精靈,並沒有別的。只有當觸摸位置不在該矩形內時,纔會運行動畫操作。

我認爲「在矩形中」檢查是多餘的,因爲您已經在上面幾行中調用了stopAllActions。

,請允許我對你的代碼的一些一般性發言:

的方法「selectSpriteForTouch」告訴我,你是選擇一個新的精靈。該功能做到了。但它不播放播放動畫。您可能需要將此重構爲單獨的「playAnimationOnSelectedSprite」方法。

你寫了20.0和40.0幾次。這意味着你實際上將一個雙精度(8字節浮點數據類型)傳遞給一個帶浮點數的CGPoint(4個浮點數)。嚴格地說,使用帶後綴「f」的20.0f表示它爲浮點數據類型,或者使用整數,因爲不使用浮點數部分。

爲什麼你把(SpriteX)放在括號裏對我來說不是很清楚,如果你想提高可讀性,你可以通過在逗號和操作數之後加空格來實現更多。

在Objective-C中,使用YES和NO宏而不是TRUE和FALSE。

bool變量_MoveableSpritetouch似乎是多餘的,除非需要其他地方。在任何情況下,如果(_MoveableSpritetouch == TRUE)塊應該移動到您將_MoveableSpritetouch變量設置爲TRUE的位置,因爲它會讓您的代碼難以通過設置變量來讀取,從而留下您所在的代碼塊(if(selSprite != newSprite))只是跳轉到另一個代碼塊(if(_MoveableSpritetouch == TRUE)),你已經知道你將要運行。

+0

我已經編輯了代碼[if((selSprite == MarshallCar)&&(!(CGRectContainsPoint(selRect,touchLocation)]現在告訴我什麼是問題。 – Neha