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 04-26-2010, 05:04 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default How can I improve these if statements?

Hi. I recently created a game where you tilt the device to avoid falling rocks. The problem is, when the rocks fall, I have it set to when they are off the screen, they will fall randomly from back at the top. The only thing is that when it does this, it flashes really fast on the screen where it is going to fall, then it falls. Here is the code:

Code:
        if(rock1.center.y > 480) {
		score_value++;
		rock1.center = CGPointMake(random() % 360,1);
		scoreInGame.text = [NSString stringWithFormat:@"%d",score_value];
		yourScore.text = [NSString stringWithFormat:@"%d",score_value];
	}
	if(rock2.center.y > 480) {
		score_value++;
		rock2.center = CGPointMake(random() % 360,1);
		scoreInGame.text = [NSString stringWithFormat:@"%d",score_value];
		yourScore.text = [NSString stringWithFormat:@"%d",score_value];
	}
	if(rock3.center.y > 480) {
		score_value++;
		rock3.center = CGPointMake(random() % 360,1);
		scoreInGame.text = [NSString stringWithFormat:@"%d",score_value];
		yourScore.text = [NSString stringWithFormat:@"%d",score_value];
	}
	if(rock4.center.y > 480) {
		score_value++;
		rock4.center = CGPointMake(random() % 360,1);
		scoreInGame.text = [NSString stringWithFormat:@"%d",score_value];
		yourScore.text = [NSString stringWithFormat:@"%d",score_value];
	}
I can't think of why it would be flashing on the screen then falling where it flashed (only at the top). Help would be appreciated. Thanks!
Whitehk is offline   Reply With Quote
Old 04-26-2010, 09:55 PM   #2 (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

I would expect your code to pop the rock back to the top of the screen; half off the top actually.

What is it really doing? I don't understand "it flashes really fast on the screen where it is going to fall, then it falls." Can you explain it another way? Slow down your framerate so you can see what's really happening if you need it.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-27-2010, 01:56 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
I would expect your code to pop the rock back to the top of the screen; half off the top actually.

What is it really doing? I don't understand "it flashes really fast on the screen where it is going to fall, then it falls." Can you explain it another way? Slow down your framerate so you can see what's really happening if you need it.
It flashes in the middle of the screen of where it is going to fall on the y axis. So basically, you can see it for a split second in the middle of the screen then it falls from the top of the screen directly above from where it flashed. Sorry. It's kind of hard to explain. If you still don't get what I'm trying to say, I can record the screen then post it to youtube.
Whitehk is offline   Reply With Quote
Old 04-27-2010, 02:08 PM   #4 (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

How are you moving your items? Are you using an NSTimer? Is it possible that this code is inside a beginAnimations/commitAnimations block? It sounds like it might be "animating" to the new position instead of moving instantly.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-27-2010, 07:01 PM   #5 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
How are you moving your items? Are you using an NSTimer? Is it possible that this code is inside a beginAnimations/commitAnimations block? It sounds like it might be "animating" to the new position instead of moving instantly.
I am using both an NSTimer and a beginAnimations/commitAnimations block. Here's the code:

inside the viewDidLoad Method:
Code:
makeRock1Fall = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fallDownRock1) userInfo:nil repeats:YES];
	makeRock2Fall = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fallDownRock2) userInfo:nil repeats:YES];
	makeRock3Fall = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fallDownRock3) userInfo:nil repeats:YES];
	makeRock4Fall = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fallDownRock4) userInfo:nil repeats:YES];
fallDownRock 1,2,3, and 4:
Code:
- (void)fallDownRock1 {
	if(gameState == kGameStateRunning) {
		if(timerCount > 1){
			rock1.hidden = NO;
		
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.1];
			[UIView setAnimationBeginsFromCurrentState:YES];
			rock1.frame = CGRectMake(rock1.frame.origin.x, (rock1.frame.origin.y + 20.0), rock1.frame.size.width, rock1.frame.size.height);
			[UIView commitAnimations];
		}
		if(timerCount > 3) {
			rock1.frame = CGRectMake(rock1.frame.origin.x, (rock1.frame.origin.y + 3.0), rock1.frame.size.width, rock1.frame.size.height);
		}
		if(timerCount > 6) {
			rock1.frame = CGRectMake(rock1.frame.origin.x, (rock1.frame.origin.y + 3.0), rock1.frame.size.width, rock1.frame.size.height);
		}
		if(timerCount > 9) {
			rock1.frame = CGRectMake(rock1.frame.origin.x, (rock1.frame.origin.y + 3.0), rock1.frame.size.width, rock1.frame.size.height);
		}
	}
}
- (void)fallDownRock2 {
	if(gameState == kGameStateRunning) {
		rock2.hidden = NO;
		
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDelegate:self];
		[UIView setAnimationDuration:0.1];
		[UIView setAnimationBeginsFromCurrentState:YES];
		rock2.frame = CGRectMake(rock2.frame.origin.x, (rock2.frame.origin.y + 17.0), rock2.frame.size.width, rock2.frame.size.height);
		[UIView commitAnimations];
	if(timerCount > 3) {
		rock2.frame = CGRectMake(rock2.frame.origin.x, (rock2.frame.origin.y + 3.0), rock2.frame.size.width, rock2.frame.size.height);
	}
	if(timerCount > 6) {
		rock2.frame = CGRectMake(rock2.frame.origin.x, (rock2.frame.origin.y + 3.0), rock2.frame.size.width, rock2.frame.size.height);
	}
	if(timerCount > 9) {
		rock2.frame = CGRectMake(rock2.frame.origin.x, (rock2.frame.origin.y + 3.0), rock2.frame.size.width, rock2.frame.size.height);
		}
	}
}
- (void)fallDownRock3 {
	if(gameState == kGameStateRunning) {
		if (timerCount > 2) {	
			rock3.hidden = NO;
	
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.1];
			[UIView setAnimationBeginsFromCurrentState:YES];
			rock3.frame = CGRectMake(rock3.frame.origin.x, (rock3.frame.origin.y + 15.0), rock3.frame.size.width, rock3.frame.size.height);
			[UIView commitAnimations];
		}
		if(timerCount > 3) {
			rock3.frame = CGRectMake(rock3.frame.origin.x, (rock3.frame.origin.y + 3.0), rock3.frame.size.width, rock3.frame.size.height);
		}
		if(timerCount > 6) {
			rock3.frame = CGRectMake(rock3.frame.origin.x, (rock3.frame.origin.y + 3.0), rock3.frame.size.width, rock3.frame.size.height);
		}
		if(timerCount > 9) {
			rock3.frame = CGRectMake(rock3.frame.origin.x, (rock3.frame.origin.y + 3.0), rock3.frame.size.width, rock3.frame.size.height);
		}
	}
}
- (void)fallDownRock4 {
	if(gameState == kGameStateRunning) {
		if (timerCount > 3) {	
			rock4.hidden = NO;
			
			[UIView beginAnimations:nil context:NULL];
			[UIView setAnimationDelegate:self];
			[UIView setAnimationDuration:0.1];
			[UIView setAnimationBeginsFromCurrentState:YES];
			rock4.frame = CGRectMake(rock4.frame.origin.x, (rock4.frame.origin.y + 13.0), rock4.frame.size.width, rock4.frame.size.height);
			[UIView commitAnimations];
		}
		if(timerCount > 3) {
			rock4.frame = CGRectMake(rock4.frame.origin.x, (rock4.frame.origin.y + 3.0), rock4.frame.size.width, rock4.frame.size.height);
		}
		if(timerCount > 6) {
			rock4.frame = CGRectMake(rock4.frame.origin.x, (rock4.frame.origin.y + 3.0), rock4.frame.size.width, rock4.frame.size.height);
		}
		if(timerCount > 9) {
			rock4.frame = CGRectMake(rock4.frame.origin.x, (rock4.frame.origin.y + 3.0), rock4.frame.size.width, rock4.frame.size.height);
		}
	}
}
Whitehk is offline   Reply With Quote
Old 04-27-2010, 07:52 PM   #6 (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

If you comment out the beginAnimations/commitAnimations, does the problem go away? I haven't mixed UIView animations with NSTimers, but I suspect that's the issue.

It's not related to the problem, but you might also consider using *one* timer that calls a gameUpdate() method, and an array of rocks instead of separate rock variables and methods. Your code will be shorter and easier to update.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 04-27-2010, 09:25 PM   #7 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
If you comment out the beginAnimations/commitAnimations, does the problem go away? I haven't mixed UIView animations with NSTimers, but I suspect that's the issue.

It's not related to the problem, but you might also consider using *one* timer that calls a gameUpdate() method, and an array of rocks instead of separate rock variables and methods. Your code will be shorter and easier to update.
The problem does go away! The only thing is now one of the rocks stops at the top and then falls about 2 seconds later. I'll figure it out from here though. And thanks for the suggestion. I will do that as well! Again, thanks!
Whitehk is offline   Reply With Quote
Old 04-27-2010, 09:28 PM   #8 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
The problem does go away! The only thing is now one of the rocks stops at the top and then falls about 2 seconds later. I'll figure it out from here though. And thanks for the suggestion. I will do that as well! Again, thanks!
Actually, I figured out that I only commented out the one rock that was stalling and then falling. now all of them stall then they all fall at once.
Whitehk is offline   Reply With Quote
Old 04-27-2010, 09:32 PM   #9 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by Whitehk View Post
Actually, I figured out that I only commented out the one rock that was stalling and then falling. now all of them stall then they all fall at once.
You're gonna kill me, but I figured out what was wrong. I cut out the part that wasn't part of the animation. I'm so sorry. You did have it right to begin with. Once again, thanks! Sorry i'm so noobish. lol
Whitehk is offline   Reply With Quote
Reply

Bookmarks

Tags
cgpointmake, falling, glitch, if statement, random

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: 407
10 members and 397 guests
7twenty7, ChrisYates, djohnson, Duncan C, gmarro, 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:04 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0