views:

49

answers:

1

Hello,

I am having trouble with my game and was wondering if anyone can help?

I am loading an overlay image on the screen when a user clicks on an answer in my quiz game, the image is of either a tick or cross to indicate if the answer was correct. The image is being loaded in a new thread.

The problem is that the image does not appear every-time the user clicks on an answer, it only appears about 20% of the time in one round.

Whats even more weird though is that i had the game a few months ago ready on iOS3 and this problem didn't occur, it only seemed to start when updating to iOS4.

Here is the code:

The images are created in the loadView method:

 tick = [UIImageView alloc];
 [tick initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
 [tick setImage:[UIImage imageNamed:@"correct.png"]];

 cross = [UIImageView alloc];
 [cross initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
 [cross setImage:[UIImage imageNamed:@"wrong.png"]];

The threads are called here:

-(void)answerAttempt:(UIButton *)b{
     ansQues++;
     if([b.currentTitle isEqualToString:ans]){
      [NSThread detachNewThreadSelector: @selector(showTick) toTarget:self withObject:nil];
      [appDelegate playSound:1]; 
      score++;
                ...
                ...
                ...
 }else{
  [NSThread detachNewThreadSelector: @selector(showCross) toTarget:self withObject:nil];
  [appDelegate playSound:0];
 }

Here is the function the two threads call:

-(void)showTick{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 [tick setHidden:NO];
 [levelPage addSubview:tick];
 [levelPage bringSubviewToFront: tick];

 usleep(500000);

 [tick removeFromSuperview];
 [pool release];
}

-(void)showCross{
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 [cross setHidden:NO];
 [levelPage addSubview:cross];
 [levelPage bringSubviewToFront: cross];

 usleep(500000); 

 [cross removeFromSuperview];
 [pool release];
}

As you might be able to tell from the code, ive tried various ways to try and get it to display everytime, but have not found any solution for some time now. I have checked to make sure that the correct thread is being called each time, and it does - just UIImage for tick and cross does not display.

Please feel free to ask any further questions.

Thanks.

+3  A: 

All UI updates should be done on the Main Thread. Also, if you are sleeping in your code, the design is probably not right, but if you are stuck on doing it this way, then a simple fix is to do this:

[levelPage performSelectorOnMainThread:@selector(addSubview:) withObject:cross waitUntilFinished:NO];
coneybeare
+1 looks good to me
invariant
thanks for the help and advice mate but im trying to use that line of code in my showTick method, just before the sleep and im getting an error message: 'UIImageView' may not respond to '-performSelectorOnMainThread:withObject:waitUntilFinished:' Any ideas about a fix to that?
I wrote that from memory. Have you ever looked at the docs? They are an excellent resource. I probably have a typo somewhere.
coneybeare
http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone:
coneybeare
thanks alot mate, it worked like a charm! Sorry that was my fault for not rechecking the method code, although i did look over it and did search online aswell for the error but didnt find out anything until i wrote it all again manually and realised that the last parameter waituntilfinished should be waituntildone! thanks again