在循环内运行延迟

时间:2013-03-25 11:51:36

标签: iphone ios objective-c cocos2d-iphone

首先,我只是一名航空公司的飞行员,编码我坐在酒店房间的死角时间,所以原谅我的无知。 我正在用Obj写这个二十一点游戏。 C和Cocos2D。对于iOS 我的问题如下: 当我点击支架时,我希望我的代码为经销商绘制牌,直到它达到17.我设法做到了但是只要我在循环中插入延迟就停止工作。 我尝试过多种方法来实现延迟,例如:

[self performSelector:@selector(dealDealerCard:faceUp:) withObject:self afterDelay:2.0];

只是冻结所有按钮。我还尝试了运行动作的CCSequence。我也尝试过调度程序和NSTimer等。

这是我的代码:

// if the stand button was pressed

-(void)standButtonPressed:(id)sender
{
 BJDrawnCard *holeCard = [dealerHand getFlippedCard];
 [holeCard flipCard];
 while ([dealerHand getTotal]<=17){
    [self performSelector:@selector(dealDealerCard:faceUp:)   withObject:self afterDelay:2.0];

}

以下是方法:

// Deal Dealers Card

 -(void)dealDealerCard:(id) dummy faceUp:(BOOL)isFaceUp
{
drawnCard=[havila drawFromDeck];
if (isFaceUp) {
[drawnCard setDisplayFrame:[[CCSpriteFrameCache  sharedSpriteFrameCache]spriteFrameByName:drawnCard.imageFileName]];
}else [drawnCard setDisplayFrame:[[CCSpriteFrameCache   sharedSpriteFrameCache]spriteFrameByName:@"Backside.png"]];
[dealerHand getCard:drawnCard];
[drawnCard setScale:0.5f];
[drawnCard setPosition:[self shoePosition]];
[cardsheet addChild:drawnCard];
// animate the cards
float cardVelocity = (sqrtf((size.width*size.width)+(size.height*size.height)))/0.5; //     set the base speed for the movment

// calculate the time needed to move the card
CGPoint moveDifference = ccpSub([self dealerCardPosition],
                                [self shoePosition]);
float moveDuration = ccpLength(moveDifference) /
cardVelocity;

// define the movement
CCMoveTo *move = [CCMoveTo actionWithDuration:
                  moveDuration position:[self dealerCardPosition]];
CCDelayTime *delay = [CCDelayTime
                      actionWithDuration:0.5];

//Run the action

[drawnCard runAction:[CCSequence actions:move,delay,nil]];

numDealerHits++;

}

总结一下:我正在尝试运行一个条件循环,调用此方法,调用之间的延迟为2.0秒,直到总数为17。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

使用第一个解决方案是冻结UI,因为你延迟了GUI线程。

您可以尝试使用GCD

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    <#code to be executed on the main queue after delay#>
});

答案 1 :(得分:1)

-(void)standButtonPressed:(id)sender
{
   BJDrawnCard *holeCard = [dealerHand getFlippedCard];
   [holeCard flipCard];
   if ([dealerHand getTotal]<=17){
      [self performSelector:@selector(dealDealerCard:faceUp:)   withObject:self afterDelay:2.0];

  }
}

在选择器的最后,确定是否还有更多,

 if ([dealerHand getTotal]<=17) {
     id more = [CCCallBlock actionWithBlock:^{
    [self performSelector:@selector(dealDealerCard:faceUp:)   withObject:self afterDelay:2.0];
}];
    [drawnCard runAction:[CCSequence actions:move,delay,more,nil]];
} else {
    [drawnCard runAction:[CCSequence actions:move,delay,nil]];
}

答案 2 :(得分:0)

尝试此操作不会冻结用户界面,并会延迟启动

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

      //do work
    });