Hey guys!
I'm making a game where when you hit play (or start) the first thing that happens is it animates a 129 image animation, which lags it for well over 10 seconds... Is their a way to make it not lag? or to put a loading screen in?
THANKS!
Hey guys!
I'm making a game where when you hit play (or start) the first thing that happens is it animates a 129 image animation, which lags it for well over 10 seconds... Is their a way to make it not lag? or to put a loading screen in?
THANKS!
You will have to use multithreading. You would setup a function that will load all of the images and put them in memory. You then call that function on that object using performSelectorInBackground:withObject:. Directly after calling this you can put a loading screen and that will run separately on the main thread while the images load in the background. At the end of the background thread function you can call a function using performSelectorOnMainThread:withObject: that will take the loading screen off and start the animation. If you want a progress bar you will have to setup a function that the background thread can call on the main thread updating the progress as it goes. Even better, possibly you can load these images before hand while the user is on a previous menu screen or something.
You will have to use multithreading. You would setup a function that will load all of the images and put them in memory. You then call that function on that object using performSelectorInBackground:withObject:. Directly after calling this you can put a loading screen and that will run separately on the main thread while the images load in the background. At the end of the background thread function you can call a function using performSelectorOnMainThread:withObject: that will take the loading screen off and start the animation. If you want a progress bar you will have to setup a function that the background thread can call on the main thread updating the progress as it goes. Even better, possibly you can load these images before hand while the user is on a previous menu screen or something.
Thank you, I tried this, and it loads the view, allows you to play UNTIL the animation is started. The anim is not displayed and it freezes while it is "playing"
Sorry, I am confused about what the problem is exactly. It is my understanding that you are on some kind of menu page, then the user presses start, then an animation plays, and then the game starts.
So that would be:
Menu -> Animation -> Gameplay
But since loading the animation takes a long time, you are going to display a loading screen so it becomes:
Main Thread: Menu -> Loading Screen -> Animation -> Gameplay
When the user presses play you transition to the loading screen, and also start loading the images on a background thread. When that finishes, you start the animation and then go into the gameplay. You would also have the option of starting to load the images when entering the menu or starting the app.
If I am mistaken, please let me know the flow that you are trying to achieve. If not, where in this process is it freezing?
Sorry, I am confused about what the problem is exactly. It is my understanding that you are on some kind of menu page, then the user presses start, then an animation plays, and then the game starts.
So that would be:
Menu -> Animation -> Gameplay
But since loading the animation takes a long time, you are going to display a loading screen so it becomes:
Main Thread: Menu -> Loading Screen -> Animation -> Gameplay
When the user presses play you transition to the loading screen, and also start loading the images on a background thread. When that finishes, you start the animation and then go into the gameplay. You would also have the option of starting to load the images when entering the menu or starting the app.
If I am mistaken, please let me know the flow that you are trying to achieve. If not, where in this process is it freezing?
Ah. Sorry, Ok so the menu is in ViewController.m. When a button is pressed it calls an ibaction that loads Game.m.
In Game.m (viewDidLoad), I call [performSelectorInBackground:@selector(initBackgrou nd) withObject:nil];
This DOES call the initBackground and it DOESN'T lag, but when the runBackground loads using [self [performSelectorOnMainThread:@selector(runBackgroun d) withObject:nil waitUntilDone:TRUE]; it lags, does not show the animation, and then continues on as normal after about 10 seconds.
Ah. Sorry, Ok so the menu is in ViewController.m. When a button is pressed it calls an ibaction that loads Game.m.
In Game.m (viewDidLoad), I call [performSelectorInBackground:@selector(initBackgrou nd) withObject:nil];
This DOES call the initBackground and it DOESN'T lag, but when the runBackground loads using [self [performSelectorOnMainThread:@selector(runBackgroun d) withObject:nil waitUntilDone:TRUE]; it lags, does not show the animation, and then continues on as normal after about 10 seconds.
Ok, so first of all you should be doing the following to load an image in the backgound:
If you just use imageNamed: only a reference is stored to the image and it isn't loaded until it is attempted to be displayed. That is why you are still lagging when returning to the main thread.
Also, are you putting up a loading screen? Or you are just letting them play the game, and when the animation is ready, you are showing it?
If you just use imageNamed: only a reference is stored to the image and it isn't loaded until it is attempted to be displayed. That is why you are still lagging when returning to the main thread.
Also, are you putting up a loading screen? Or you are just letting them play the game, and when the animation is ready, you are showing it?
I'm not sure what you mean by the code snippet.. can you explain more? and I'm letting them play it.. I might add a loading screen.. depending on how the game's direction goes.
I'm not sure what you mean by the code snippet.. can you explain more? and I'm letting them play it.. I might add a loading screen.. depending on how the game's direction goes.
With my code I am assuming that you have already filled the variable "filePath" with the path of the image. You can use:
NSData* data = [[NSData alloc] initWithContentsOfFile:filePath];
This line reads in the image into the data variable in binary format (the actual bytes (1s and 0s) of the image file). That means that it is loading it all into memory directly.
[code]
UIImage* image = [UIImage imageWithData:data];
[code]
This line then turns the binary data into a UIImage. This way you can add it to the view later in the main thread.
You should also add the following line to release the data object:
NSData* data = [[NSData alloc] initWithContentsOfFile:filePath];
This line reads in the image into the data variable in binary format (the actual bytes (1s and 0s) of the image file). That means that it is loading it all into memory directly.
[code]
UIImage* image = [UIImage imageWithData:data];
[code]
This line then turns the binary data into a UIImage. This way you can add it to the view later in the main thread.
You should also add the following line to release the data object:
Code:
[data release];
So basically each image in the array is 3 lines now rather then 1?
Well you should load the images in a loop. Each time through the loop you do that code and then add the UIImage to an array that you will use later. So yes, you will have to run that code for each image, but you should only have to write that code once (inside the loop)