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.
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];
Related
I have a very strange error that I have never seen before. I have used this code hundreds of times before, but for some reason, the afterDelay is being completely disregarded.
My Code:
- (void)runIntro
{
if([self introDone])return;
UIImageView* back = [[UIImageView alloc] initWithFrame:[[AppDelegate shared] frame]];
[back setImage:[[AppDelegate shared] bgImage]];
[back setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:back];
UIImageView* target = [[UIImageView alloc] initWithFrame:CGRectMake(([[AppDelegate shared] width]-250)/2.0, ([[AppDelegate shared] height]-350)/2.0, 250, 72)];
[target setImage:[UIImage imageNamed:#"bpside.png"]];
[self.view addSubview:target];
Parabolic* wally = [[Parabolic alloc] initWithFrame:CGRectMake(([[AppDelegate shared] width]-200)/2.0, ([[AppDelegate shared] height]-314)/2.0+250, 200, 200)];
[wally setImage:[UIImage imageNamed:#"wallyside1.png"]];
[self.view addSubview:wally];
[self performSelector:#selector(moveWallyIntro:) withObject:wally afterDelay:1];
[self performSelector:#selector(moveWallyIntro:) withObject:wally afterDelay:2];
}
- (void)moveWallyIntro:(Parabolic*)wally
{
[wally runPBM:50 withDY:300 withDuration:0.5];
}
I know that all of the other code is working properly, but for whatever reason, the selectors are being performed immediately, which causes a major problem. This code is being run at the end of the viewWillAppear method. I have programmed this way many times before and this is the first time I've seen this happen.
UPDATE:
If I remove the code that adds the back UIImageView, this no longer is a problem. How can I add a giant image (3000x2000) in aspect fill format like this so it doesn't cause the program to hang? I'm using the same background image for iphone, ipad, etc., so it needs to be large for the retina iPads...
Goal is to "launch a spinner graphic at start of viewWillAppear that loads data before showing the tableview" so the user doesn't wonder why there's a delay before seeing the table. I.e. a UIActivityIndicatorView has been added to the window, and I just want to set the alpha to hide/show it.
I get this strange error when starting a thread to make sure the "spinning gears" imageview (tag=333) gets shown, before moving on to load/calculate stuff in viewWillAppear.
I don't get it on every call to [appdel addGearz] and [appdel removeGearz], it happens for both these, and it's random. It can happen after 2 viewWillAppears, or after 15. If I comment out the line that sets the alpha, everything works.
A typical viewWillAppear looks something like this,
[super viewWillappear];
self.title=#"Products listing"; //and other simple things
[appdel addGearz];
[self getProducts];
[self getThumbnails];
[myTableView reloadData]; //in case view already loaded and coming back from subview and data changed
And here is the code that crashes if the lines with .alpha are not commented out
-(void)addGearz {
[NSThread detachNewThreadSelector:#selector(gearzOn) toTarget:self withObject:nil];
}
-(void)removeGearz {
[NSThread detachNewThreadSelector:#selector(gearzOff) toTarget:self withObject:nil];
}
- (void)gearzOn {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[window viewWithTag:333].alpha=1.0;
//
// [[window viewWithTag:333] setNeedsDisplay];
[pool drain];
}
- (void) gearzOff {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[window viewWithTag:333].alpha=0.0;
//
// [[window viewWithTag:333] setNeedsDisplay];
[pool drain];
}
I've used someone else's code, so... anything obvious you can see? Surely I must be able to change alpha of UIViews in a thread? Do I need to "embed" the alpha-change in some "stop enumerating while I change this"-code?
I made it not crash by moving that alpha-change-line to above the pool alloc or below the [pool drain], but then I get a lot of "autoreleased with no pool in place - just leaking"-messages.
Apparently, there's something I don't understand about this thread code.
You must not try to modify the UI on a separate thread. UI should only be manipulated on the main thread.
Instead of detaching a new thread, you should use performSelectorOnMainThread:withObject:waitUntilDone:. This will ensure that the method will be called on the proper thread.
I know this must be something simple that I am overlooking, but why is this leaking:
//add a pool for this since it is on its own thread
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//add the image
NSURL * imageURL = [NSURL URLWithString:[recipeData objectForKey:#"imagePath"]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
//resize to make it fit the screen space
CGRect frame = myImageView.frame;
frame.size.width = 320;
frame.size.height = 357;
myImageView.frame = frame;
[self.view addSubview:myImageView];
[activity stopAnimating];
[pool drain];
[self placeItems];
I get the error:
_NSAutoreleaseNoPool(): Object 0x4e2fdf0 of class NSPathStore2 autoreleased with no pool in place - just leaking
I tried moving the placement of [pool drain] but that did nothing. I see a lot of code that looks just like this while searching Google for a cause.
Thanks for your help.
Draining the pool has the effect of releasing and subsequently deallocating it, since autoreleasepools cannot be retained. I suspect there must be some need for an autoreleasepool within placeItems (or some other place called after [pool drain]) since at that point the pool is propably gone already.
So, you might want to try commenting out the drain message to see if that will make the leak go away.
A lot of things to say here :
first, you're leaking myImageView. You have to release it after the -addSubview.
next, since you're on another thread, your [pool drain] must be at the end
last, since you're not on the main thread, you can't perform any UI operation. Try to replace [self.view addSubview:myImageView] by [self.view performSelectorOnMainThread:#selector(addSubview:) withObject:myImageView waitUntilDone:YES]. Same with [activity stopAnimating].
And like Brian said, the -drain message must be at the end of your thread.
I'm trying to create an activity indicator in iPhone app. The problem is that I cannot get it to appear before the actual task i want it to diplay during is already done. Is there something funky about the order in which the iPhone does stuff?
Here is my problematic code (in my app delegate):
-(BOOL)showProgressView: (NSString *) message {
self.progress = [[UIView alloc] initWithFrame:window.frame];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"squircle.png"]];
[img setAlpha:0.5];
[img setFrame:CGRectMake(94, 173, 133, 133)];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(51.5, 51.5, 30, 30)];
spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[img addSubview:spinner];
[self.progress addSubview:img];
[spinner startAnimating];
[img release];
[spinner release];
[window addSubview:self.progress];
return YES;
}
I then call this code like this:
if ([appDelegate showProgressView:#"Loading..:"])
{
//My actual code loads data and stuff here but that is not important
//drawCtrl is a UIViewController subclass that is instantiated here
UINavigationController *navController = [appDelegate navigationController];
[navController pushViewController:drawCtrl animated:YES];
[drawCtrl release];
}
The problem is that my activity indicator does not appear until the new view controller is pushed onto the navController's stack. Can I control this in some way?
Thanks in advance!
-Mats
You need to call your loading method periodically via a timer. UI changes require a trip through the event loop to be seen. I have done this usually in a timer I set up in the AppDelegate, it calls the "methodThatTakesSomeTime" every n seconds, the "methodThatTakesSomeTime" method does some work for a specified time slice and then exits which is usually about the same as the timer trigger time. This gives the event loop time to refresh the UI and also give your method time to do its stuff.
Takes some fiddling with keeping the state of the "methodThatTakesSomeTime" so it can continue on its way, but that is what it takes.
I'm trying to add a progress meter, or other "I'm busy right now" notification to my view hierarchy right before doing some intense computation that will block the UI. My code looks some thing like:
//create view
[currentTopView addSubView:imBusyView];
//some initialization for the intense computation
[computation startComputing];
Unfortunately, my progress meter doesn't display until after the computation completes. It appears like the views aren't re-drawn until the run loop completes. I'm pretty sure that setNeedsDisplay and setNeedsLayout will also wait until the run loop completes.
How do I get the view to display immediately?
Redrawing only occurs when your code returns control to the run loop. So the easiest way would be for you to schedule the startComputing call with a zero delay. That way, it will be executed during the next run loop iteration (right after redrawing):
[computation performSelector:#selector(startComputing) withObject:nil afterDelay:0.0];
Be aware, though, that unless you do your computation in another thread you will not be able to update the UI during the computation.
If you are doing heavy calculations maybe spawning a new thread is a good idea.
Here I have an activityIndicator displayed and starts a large XMLParse operation in a background thread:
- (void) setSearchParser {
activityIndicator = [[ActivityIndicatorView alloc] initWithActivity];
[self.view addSubview:activityIndicator];
[NSThread detachNewThreadSelector:#selector(getSearchResults:) toTarget:self withObject:[searchParser retain]];
}
then the getSearchResults method:
- (void) getSearchResults: (SearchResultParser *) parser {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[parser startParser];
[self performSelectorOnMainThread:#selector(searchResultsReady:) withObject:[parser data] waitUntilDone:NO];
[pool release];
}
So firstly make a new thread:
[NSThread detachNewThreadSelector:#selector(getSearchResults:) toTarget:self withObject:[searchParser retain]];
this means that all code inside the getSearchResults will be executed on a different thread. getSearchResults also get's passed a parameter of "searchParser" thats a large object that just needs startParse called on it to begin.
This is done in getSearchResults. When the [parser startParser] is done, the results is passed back to the main thread method called "searchResultsReady" and the threads autorelease pool is released.
All the time it took from my parser began to it had finished, a gray view covered the screen an an activityIndicator ran.
You can have the small activityIndicator class I wrote:
-(id) initWithActivity {
[self initWithFrame:[self bounds]];
[self setBackgroundColor:[UIColor blackColor]];
[self setAlpha:0.8];
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.center = CGPointMake(160, 240);
[self addSubview:activityView ];
[activityView startAnimating];
return self;
}
- (void) dealloc {
[activityView release];
[super dealloc];
}
Hope it helps you out, even though threads seems a bit confusing, they can help to make the UI not freeze up, which is especially important on the iPhone.