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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2010, 09:25 AM   #1 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Exclamation removing objects in view in an array

Hi, I am trying to get this game to work, but it gets messed up heres the code:
Code:
-(void)moveMeteors{
	
	for (int i = [bigbombArray count] - 1; i >=0; i--){
		UIView *bigbomb = [bigbombArray objectAtIndex:i];
		
		CGPoint newCenter = bigbomb.center;
		newCenter.y = newCenter.y +5;
		bigbomb.center = newCenter;
	
		if (bigbomb.frame.origin.y > 390){ // if off the bottom
			self.view = gameOver;
			[timer1 invalidate];
			timer1 = nil;
			score = 0;
			
			newCenter.y = newCenter.y - 5;
			[bigbomb removeFromSuperview];
			
			[bigbombArray removeObjectAtIndex:i];
			
			
			continue;
		}
Ok so everything works for moving the images in the array but when one of them hits the bottom and goes to the game over screen, if you play again the other images that didn't hit the bottom are still there and you almost immediately lose. Then if you go to even the next level after one touching the bottom it makes about 100 images when i don't want it to.
__________________


Counting Time Now on the appstore - a very addicting strategic time game!!
http://itunes.apple.com/us/app/count...377631631?mt=8
Mjsdabeast is offline   Reply With Quote
Old 07-13-2010, 01:12 PM   #2 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Question ?

Quote:
Originally Posted by Mjsdabeast View Post
Hi, I am trying to get this game to work, but it gets messed up heres the code:
Code:
-(void)moveMeteors{
	
	for (int i = [bigbombArray count] - 1; i >=0; i--){
		UIView *bigbomb = [bigbombArray objectAtIndex:i];
		
		CGPoint newCenter = bigbomb.center;
		newCenter.y = newCenter.y +5;
		bigbomb.center = newCenter;
	
		if (bigbomb.frame.origin.y > 390){ // if off the bottom
			self.view = gameOver;
			[timer1 invalidate];
			timer1 = nil;
			score = 0;
			
			newCenter.y = newCenter.y - 5;
			[bigbomb removeFromSuperview];
			
			[bigbombArray removeObjectAtIndex:i];
			
			
			continue;
		}
Ok so everything works for moving the images in the array but when one of them hits the bottom and goes to the game over screen, if you play again the other images that didn't hit the bottom are still there and you almost immediately lose. Then if you go to even the next level after one touching the bottom it makes about 100 images when i don't want it to.
Does anyone know?
__________________


Counting Time Now on the appstore - a very addicting strategic time game!!
http://itunes.apple.com/us/app/count...377631631?mt=8
Mjsdabeast is offline   Reply With Quote
Old 07-13-2010, 01:47 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Sounds like the bug is in your "game over" code. You need to remove all of the bombs from the array an the screen; it looks like you just remove one bomb now.

I'd move all of the gameOver stuff to another method for clarity's sake, and do this:

Code:
if (bigbomb.frame.origin.y > 390){ // if off the bottom
	[self gameOver];
	break; //note break, not continue!
	}
I changed the "continue" to a "break" because you don't want to keep checking the other bombs if the game is over. A "continue" skips to the next cycle of the loop, but "break" will break you out of the loop entirely.

Now in gameOver you can loop through the array and remove all the bombs from the screen and array.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-13-2010, 04:02 PM   #4 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Sounds like the bug is in your "game over" code. You need to remove all of the bombs from the array an the screen; it looks like you just remove one bomb now.

I'd move all of the gameOver stuff to another method for clarity's sake, and do this:

Code:
if (bigbomb.frame.origin.y > 390){ // if off the bottom
	[self gameOver];
	break; //note break, not continue!
	}
I changed the "continue" to a "break" because you don't want to keep checking the other bombs if the game is over. A "continue" skips to the next cycle of the loop, but "break" will break you out of the loop entirely.

Now in gameOver you can loop through the array and remove all the bombs from the screen and array.
Thank you, but the one main problem is I don't know exactly how to loop through the array and remove the images from the screen. I have tried: bigbombArray = nil; but it doesn't work
[bigbombArray removeObjectsAtIndexes:i]; (didn't really think it would work)
__________________


Counting Time Now on the appstore - a very addicting strategic time game!!
http://itunes.apple.com/us/app/count...377631631?mt=8
Mjsdabeast is offline   Reply With Quote
Old 07-13-2010, 08:15 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 55
Applesson is on a distinguished road
Default

in your game over code:

Code:
// loop through all entries in the array and remove them
for (UIView *bomb in bigbombArray)
 [bomb removeFromSuperview];

// remove all objects from the array
[bigbombArray removeAllObjects];
Applesson is offline   Reply With Quote
Old 07-13-2010, 11:40 PM   #6 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by Applesson View Post
in your game over code:

Code:
// loop through all entries in the array and remove them
for (UIView *bomb in bigbombArray)
 [bomb removeFromSuperview];

// remove all objects from the array
[bigbombArray removeAllObjects];
ok I put this code and fixed it(it's bigbomb not bomb). But when one of the images hits the bottom it crashes and the debugger says it's because of

[self removeimages];
and
for (UIView *bigbomb in bigbombArray)
__________________


Counting Time Now on the appstore - a very addicting strategic time game!!
http://itunes.apple.com/us/app/count...377631631?mt=8
Mjsdabeast is offline   Reply With Quote
Old 07-14-2010, 12:41 AM   #7 (permalink)
Elegance is Infinite
iPhone Dev SDK Supporter
 
TapTouchClick's Avatar
 
Join Date: Jan 2010
Location: Bay Area, CA
Posts: 677
TapTouchClick is on a distinguished road
Send a message via AIM to TapTouchClick Send a message via Skype™ to TapTouchClick
Default

sigh. no, its *bomb. you dont want to name it something already declared...
__________________
Our website
TapTouchClick is offline   Reply With Quote
Old 07-14-2010, 07:43 AM   #8 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by TapTouchClick View Post
sigh. no, its *bomb. you dont want to name it something already declared...
ok my bad, but it still crashes and it's saying it's because of the [self removeimages]; and for (UIView *bomb in bigbombArray)
__________________


Counting Time Now on the appstore - a very addicting strategic time game!!
http://itunes.apple.com/us/app/count...377631631?mt=8
Mjsdabeast is offline   Reply With Quote
Old 07-15-2010, 02:38 PM   #9 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 22
Tinyted is on a distinguished road
Default

I recall doing such a thing with my test app. I had several balls falling down, and it would explode after a certain situation(which isn't the point)
When the user has filled the entire screen with balls or failed. I clear out the ballarrays

Here's the code that removes the array. BallArray contains UIViews.
Code:
for (int a=0; a<[BallArray count]; a++)
		{
			NSString *testtext2 = [[NSString alloc] initWithFormat:@"Failed~"];
			test2.text=testtext2;
			score=0;
			[[BallArray objectAtIndex:a] removeFromSuperview];
			[BallArray removeObjectAtIndex:a];
			a--;
				
		}
You have to do a loop because each UIView needs to remove from super view first.
By the way I don't think.
Code:
for (UIView *bomb in bigbombArray)
 [bomb removeFromSuperview];
Is a legit statement.
Another method not sure if it saves a bit of time is this.
Code:
for (int a=0; a<[BallArray count]; a++)
		{
			NSString *testtext2 = [[NSString alloc] initWithFormat:@"Failed~"];
			test2.text=testtext2;
			score=0;
			[[BallArray objectAtIndex:a] removeFromSuperview];			
			a--;				
		}[BallArray removeAllOBjects];
Just remember to replace BallArray with your array.
And just in case this info is needed, my BallArray is an NSMutableArray
Tinyted is offline   Reply With Quote
Old 07-15-2010, 03:55 PM   #10 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by Tinyted View Post
I recall doing such a thing with my test app. I had several balls falling down, and it would explode after a certain situation(which isn't the point)
When the user has filled the entire screen with balls or failed. I clear out the ballarrays

Here's the code that removes the array. BallArray contains UIViews.
Code:
for (int a=0; a<[BallArray count]; a++)
		{
			NSString *testtext2 = [[NSString alloc] initWithFormat:@"Failed~"];
			test2.text=testtext2;
			score=0;
			[[BallArray objectAtIndex:a] removeFromSuperview];
			[BallArray removeObjectAtIndex:a];
			a--;
				
		}
You have to do a loop because each UIView needs to remove from super view first.
By the way I don't think.
Code:
for (UIView *bomb in bigbombArray)
 [bomb removeFromSuperview];
Is a legit statement.
Another method not sure if it saves a bit of time is this.
Code:
for (int a=0; a<[BallArray count]; a++)
		{
			NSString *testtext2 = [[NSString alloc] initWithFormat:@"Failed~"];
			test2.text=testtext2;
			score=0;
			[[BallArray objectAtIndex:a] removeFromSuperview];			
			a--;				
		}[BallArray removeAllOBjects];
Just remember to replace BallArray with your array.
And just in case this info is needed, my BallArray is an NSMutableArray
Ok, finally it works. I used the code that you put but in a couple different places and also I had to set the array to nil and it's working perfectly now. Thank you so much. Here is the working code:
Code:
-(void)createmissiles{
	
    if (bigbombArray==nil){
		bigbombArray = [[NSMutableArray alloc] init];
    }
	
    UIImage *bigbombImage = [UIImage imageNamed:@"bigbomb.png"];
    
	
    for (int i = 0; i< 30; i++){  
        bigbomb = [[UIImageView alloc] initWithImage:bigbombImage];
		
	
        int x = arc4random()%320; 
		int y = -70;
        y = y - arc4random()%500; 
        bigbomb.center = CGPointMake(x,y);
		
        [self.view addSubview:bigbomb]; 
        [bigbombArray addObject:bigbomb]; 
       [bigbomb release];
    }
	
}	
-(void)moveMeteors{
	
	for (int i = [bigbombArray count] - 1; i >=0; i--){
		UIView *bigbomb = [bigbombArray objectAtIndex:i];
		
		CGPoint newCenter = bigbomb.center;
		newCenter.y = newCenter.y +5;
		bigbomb.center = newCenter;
	
		if (bigbomb.frame.origin.y > 390){ // if off the bottom
			
			self.view = gameOver;
			[timer1 invalidate];
			timer1 = nil;
			score = 0;
			
			newCenter.y = newCenter.y - 5;

			[self performSelector:@selector(removeimages)];
			[[bigbombArray objectAtIndex:i] removeFromSuperview];
			[bigbombArray removeObjectAtIndex:i];
			break;
		}
}
-(void)removeimages {
	for (UIView *bomb in bigbombArray) {
		[bomb removeFromSuperview];
                 bigbombArray = nil;
	
	}
	
}
__________________


Counting Time Now on the appstore - a very addicting strategic time game!!
http://itunes.apple.com/us/app/count...377631631?mt=8

Last edited by Mjsdabeast; 07-15-2010 at 04:01 PM.
Mjsdabeast is offline   Reply With Quote
Reply

Bookmarks

Tags
array, falling, images, problem

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: 400
11 members and 389 guests
7twenty7, ChrisYates, djohnson, Duncan C, gmarro, hussain1982, Kirkout, Retouchable, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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