Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-27-2011, 03:23 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 41
cvcs1 is on a distinguished road
Exclamation Loading Screen ? (NOT ON STARTUP)

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!
cvcs1 is offline   Reply With Quote
Old 02-27-2011, 06:11 PM   #2 (permalink)
Digital Assertion
 
drewag's Avatar
 
Join Date: Aug 2009
Posts: 268
drewag is on a distinguished road
Default

Quote:
Originally Posted by cvcs1 View Post
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.
__________________
Digital Assertion, LLC
Check out our iPhone apps:
Notecards: Great study tool for anytime anywhere!
Redirect: Addicting puzzle / action game!

Follow Digital Assertion on Twitter: DigAssertion

Follow me on Twitter: Andrew Wagner
drewag is offline   Reply With Quote
Old 03-07-2011, 03:52 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 41
cvcs1 is on a distinguished road
Default

Quote:
Originally Posted by drewag View Post
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"

ViewDidLoad:
Code:
backgroundAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Final.png"]];
	backgroundAnim.frame = CGRectMake(0, 0, 480, 320);
	[self.view addSubview:backgroundAnim];
	[self.view sendSubviewToBack:backgroundAnim];
      
        [self performSelectorInBackground:@selector (initBackground) withObject:nil];
initBack:
Code:
					   [UIImage imageNamed:@"1 126.png"],
					   [UIImage imageNamed:@"1 127.png"],
					   [UIImage imageNamed:@"1 128.png"],
					   [UIImage imageNamed:@"1 129.png"],
					   nil];
	
	
	[self performSelectorOnMainThread:@selector(runBackground) withObject:nil waitUntilDone:TRUE];
runBack:

Code:
-(void)runBackground{
	backgroundAnim.animationImages = backgroundArray;
	backgroundAnim.animationDuration = 6;
	backgroundAnim.animationRepeatCount = 1;
	backgroundAnim.contentMode = UIViewContentModeTopLeft;
	[backgroundAnim startAnimating];
	f1.hidden = FALSE;
	[backgroundAnim removeFromSuperview];


}
cvcs1 is offline   Reply With Quote
Old 03-07-2011, 08:52 PM   #4 (permalink)
Digital Assertion
 
drewag's Avatar
 
Join Date: Aug 2009
Posts: 268
drewag is on a distinguished road
Default

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?
__________________
Digital Assertion, LLC
Check out our iPhone apps:
Notecards: Great study tool for anytime anywhere!
Redirect: Addicting puzzle / action game!

Follow Digital Assertion on Twitter: DigAssertion

Follow me on Twitter: Andrew Wagner

Last edited by drewag; 03-07-2011 at 08:54 PM.
drewag is offline   Reply With Quote
Old 03-07-2011, 09:06 PM   #5 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 41
cvcs1 is on a distinguished road
Default

Quote:
Originally Posted by drewag View Post
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.
cvcs1 is offline   Reply With Quote
Old 03-07-2011, 09:20 PM   #6 (permalink)
Digital Assertion
 
drewag's Avatar
 
Join Date: Aug 2009
Posts: 268
drewag is on a distinguished road
Default

Quote:
Originally Posted by cvcs1 View Post
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:

Code:
NSData* data = [[NSData alloc] initWithContentsOfFile:filePath];
UIImage* image = [UIImage imageWithData:data];
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?
__________________
Digital Assertion, LLC
Check out our iPhone apps:
Notecards: Great study tool for anytime anywhere!
Redirect: Addicting puzzle / action game!

Follow Digital Assertion on Twitter: DigAssertion

Follow me on Twitter: Andrew Wagner
drewag is offline   Reply With Quote
Old 03-07-2011, 10:56 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 41
cvcs1 is on a distinguished road
Default

Quote:
Originally Posted by drewag View Post
Ok, so first of all you should be doing the following to load an image in the backgound:

Code:
NSData* data = [[NSData alloc] initWithContentsOfFile:filePath];
UIImage* image = [UIImage imageWithData:data];
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.
cvcs1 is offline   Reply With Quote
Old 03-07-2011, 11:51 PM   #8 (permalink)
Digital Assertion
 
drewag's Avatar
 
Join Date: Aug 2009
Posts: 268
drewag is on a distinguished road
Default

Quote:
Originally Posted by cvcs1 View Post
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:

Code:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"1 126" ofType:@"png"];
Code:
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];
__________________
Digital Assertion, LLC
Check out our iPhone apps:
Notecards: Great study tool for anytime anywhere!
Redirect: Addicting puzzle / action game!

Follow Digital Assertion on Twitter: DigAssertion

Follow me on Twitter: Andrew Wagner
drewag is offline   Reply With Quote
Old 03-08-2011, 12:14 AM   #9 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 41
cvcs1 is on a distinguished road
Default

Quote:
Originally Posted by drewag View Post
With my code I am assuming that you have already filled the variable "filePath" with the path of the image. You can use:

Code:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"1 126" ofType:@"png"];
Code:
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?
cvcs1 is offline   Reply With Quote
Old 03-08-2011, 08:39 AM   #10 (permalink)
Digital Assertion
 
drewag's Avatar
 
Join Date: Aug 2009
Posts: 268
drewag is on a distinguished road
Default

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)
__________________
Digital Assertion, LLC
Check out our iPhone apps:
Notecards: Great study tool for anytime anywhere!
Redirect: Addicting puzzle / action game!

Follow Digital Assertion on Twitter: DigAssertion

Follow me on Twitter: Andrew Wagner
drewag is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, loading

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 367
18 members and 349 guests
Absentia, akphyo, apatsufas, BinHex, cpsclicker, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, Music Man, PavelMik, teebee74, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0