title: running a delay inside a loop
body: <p>first off I'm just an airline pilot coding on my dead hours sitting in hotel rooms so pardon my ignorance.
  I'm writing this Blackjack Game using Obj. C and Cocos2D. for iOS
  My problem is as follows:
  When I hit stand I want my code to draw cards for the dealer until it reaches 17. I managed to do that but as soon as I insert a delay in the loop it stop working.
  I have tried numerous ways to implement the delay such as :</p>
  
  <pre><code>[self performSelector:@selector(dealDealerCard:faceUp:) withObject:self afterDelay:2.0];
  </code></pre>
  
  <p>which just freezes up all the buttons. I also tried the CCSequence with run action. I also tried the scheduler and NSTimer etc.</p>
  
  <p>here is my code:   </p>
  
  <pre><code>// if the stand button was pressed
  
  -(void)standButtonPressed:(id)sender
  {
   BJDrawnCard *holeCard = [dealerHand getFlippedCard];
   [holeCard flipCard];
   while ([dealerHand getTotal]&lt;=17){
      [self performSelector:@selector(dealDealerCard:faceUp:)   withObject:self afterDelay:2.0];
  
  }
  </code></pre>
  
  <p>and here is the method:</p>
  
  <pre><code>// 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++;
  
  }
  </code></pre>
  
  <p>so to sum it up: I'm trying to run a conditional loop that will call this method with a 2.0 sec delay between calls until total is 17.</p>
  
  <p>any help will be appreciated.</p>