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