2013-05-27 44 views
0

我正在写一个使用cocos2D的二十一点游戏。 我使用CCSequence很多,并在我的代码中的许多地方有问题。 我遇到的问题是一个动作在前一个动作完成之前发射。 例如:CCS序列不等待动作完成,然后开始下一个动作

-(void)standButtonPressed:(id)sender 
{ 
    [self removeChildByTag:333]; 

    if ((splitNumber<3)&&(numberSplitHits>0)) 
    { 
     [self removeChildByTag:333]; 
     splitNumber++; 
     if ([[hands objectAtIndex:splitNumber]handTotal]==0) 
      goto end; 
     [self afterSpliting]; 
     [self addArrow]; 
     return; 
    } 

    end: 
    [self removeChildByTag:333]; 
    [[BackgroundLayer sharedBackground]menuSetup:hand gamePhase:3]; 
    BJDrawnCard *holeCard = [dealerHand getFlippedCard]; 
    [holeCard flipCard]; 
    [[SimpleAudioEngine sharedEngine] playEffect:SND_DEAL_CARD]; 
    [self generateDealerHandDisplay]; 
    [self updateDealerHandScoreDisplay]; 
    id myCallFun1 = [CCCallFunc actionWithTarget:self     selector:@selector(finishDrawingDealer)]; 
    id myCallFun2 = [CCCallFunc actionWithTarget:self selector:@selector(checkWhoWonHand)]; 
    id myCallFun3 = [CCCallFuncND actionWithTarget:[BackgroundLayer sharedBackground] selector:@selector(menuSetup:gamePhase:)data:(void*)6]; 
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2]; 
    [self runAction:[CCSequence actions:myCallFun1,delay,myCallFun2,myCallFun3 ,nil]]; 
} 

所以myCallFunc2将开始运行myCallFun1完成之前。 当我使用CCSequence时,在我的代码的其他部分中遇到同样的问题,操作将按顺序启动,但不会等待某个操作在下一个启动之前完成。

有没有更好的方法来排序动作,或者甚至可以替代CCSequence?

这里是myCallFun1调用方法:

-(void)finishDrawingDealer 
{ 
if (dealerHand.handTotal<17) 
{ 
drawnCard=[havila drawFromDeck]; 
[drawnCard setDisplayFrame:[[CCSpriteFrameCache  sharedSpriteFrameCache]spriteFrameByName:drawnCard.imageFileName]]; 
CCMoveTo *move =[self animateDealerCards:drawnCard andPosition:[self dealerCardPosition]]; 
CCDelayTime *delay = [CCDelayTime 
         actionWithDuration:0.5]; 
[dealerHand getCard:drawnCard]; 

//Run the action 

numDealerHits++; 
[self performSelector:@selector(updateDealerHandScoreDisplay) withObject:nil afterDelay:1.0]; 


if (dealerHand.handTotal<17) { 
    id more = [CCCallFunc actionWithTarget:self selector:@selector(finishDrawingDealer)]; 


    [drawnCard runAction:[CCSequence actions:delay,move,delay,more,nil]]; 
} else { 
    [drawnCard runAction:[CCSequence actions:delay,move,delay,nil]]; 
} 
if (dealerHand.handTotal>21) 

    [self dealerBusted]; 
} 
} 
+0

你应该检查你的缩进代码的方式。 –

+0

您如何注意myClassFun2在myClassFun1完成之前调用?我想你会记录它或使用调试器。也请张贴代码或任何使你得出这个结论的东西。 –

+0

myClassFun1的功能是什么?你可以发布实施吗? – giorashc

回答

2

的问题来自于这样CCCallFunc动作是瞬间完成的。他们不关心给定选择器运行多长时间,他们关心的是调用该选择器。在他们调用选择器之后,他们已经有效地完成了他们的工作,然后顺序进入下一个动作。

取而代之,您可以在每个方法的末尾调用下一个方法,如下所示。我也注意到你想要一个延迟,你可以用一个序列和CCCallFunc来做。

- (void)finishDrawingDealer 
{ 
    //Do your work that takes time 

    //Once it has finished 
    id callNextMethod = [CCCallFunc actionWithTarget:self selector:@selector(checkWhoWonHand)]; 
    id sequence = [CCSequence actions:[CCDelayTime actionWithDuration:2.0f], callNextMethod, nil]; 
    [self runAction:sequence]; 
} 
+0

这工作!谢谢一堆 –

+0

为什么不直接使用'CCScheduler'调度选择器呢?无需运行操作。另外,假设你使用Cocos2D 2.x,你应该使用'CCCallBlock'而不是'CCCallFunc'。 –

+0

是的,那也可以。如果你已经有了方法,CCCallFunc可以正常工作。唯一一个我不喜欢的是CCCallFuncND,因为这对ARC不起作用。 –

相关问题