4

首先,我只是一名航空公司的飞行员,在我坐在酒店房间里的死时间编码,所以请原谅我的无知。我正在使用 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。

任何帮助将不胜感激。

4

3 回答 3

2
-(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]];
}
于 2013-03-25T12:02:16.090 回答
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#>
});
于 2013-03-25T11:59:38.880 回答
0

试试这个不会冻结 UI 并且会延迟启动

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

      //do work
    });
于 2013-03-25T12:05:44.987 回答