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 01-31-2011, 08:44 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default increasing the speed of an image with time

Yello all!, I a working on a project and I wish to increase the speed of an object with time i.e as the game progresses, the objects speed increases and its movement becomes faster.

currently I am moving the object by using this code in my .m file

Code:
  imageMovement = CGPointMake(2,2);
Please can anyone advice on how to do this? Do I need a counter, use of NSTimer...?

Code snippets will be very much appreciated...Thanks in advance
eyinEkpe is offline   Reply With Quote
Old 02-01-2011, 12:09 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 46
NorthCode is on a distinguished road
Default ..

Your code is not moving the image more than once.. if you want a game with an image moving around you need this:

You need to call a "move"-function with a repeating timer:


Code:
//Declare these in .h:
UIImageView *imageBeingMoved;
int speedX, speedY;

NSTimer *timer;

-(void)start;
-(void)move;


@property (nonatomic, retain) NSTimer *timer;


.m
@synthesize timer;


-(void)start {

speedX = 1;

speedY = 1;

timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(move) userInfo:nil repeats:YES];

}

-(void)move {

//Moving the image
imageBeingMoved.center = CGPointMake(imageBeingMoved.x + speedX, imageBeingMoved.center.y + speedY);

//Increasing the speed:
speedX ++;
speedY ++;
//You can have the code for increasing the speed anywhere you want, for example a function called levelUp which is called when something reaches something...

}
You can customize this code so it fits your needs..

Last edited by NorthCode; 02-01-2011 at 12:13 PM.
NorthCode is offline   Reply With Quote
Old 02-02-2011, 09:01 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default @NorthCode

Thanks a million, NorthCode...I can see your implementation working for me already.

However I have no idea what the levelup method will look like or how it can be implemented...

If I am not asking for too much can you give me an example based on your code of how the level up method can be implemented.

Once again thanks for your help. I will start working on customising your code right away.
CHEERS!!
eyinEkpe is offline   Reply With Quote
Old 02-04-2011, 06:11 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 46
NorthCode is on a distinguished road
Default ..

Quote:
Originally Posted by eyinEkpe View Post
Thanks a million, NorthCode...I can see your implementation working for me already.

However I have no idea what the levelup method will look like or how it can be implemented...

If I am not asking for too much can you give me an example based on your code of how the level up method can be implemented.

Once again thanks for your help. I will start working on customising your code right away.
CHEERS!!

I do not know what the objective of your game is, but im guessing you have a score.
So in the move-function you need to increase the score according to things happening in the game..
For example:

Code:
-(void)viewDidLoad {

imageBeingMove.center = CGPointMake(100, 100);

player.center = CGPointMake(300, 300);

score = 0;
speedX = 1;
speedY = 1;

}

-(void)move {

//Moving the image
imageBeingMoved.center = CGPointMake(imageBeingMoved.x + speedX, imageBeingMoved.center.y + speedY);


//Check for collision between your image and another image i have called player..
 if (CGRectIntersects(imageBeingMoved.frame, player.frame) {

score = score + 10;

//Reset the image to start
imageBeingMove.center = CGPointMake(100, 100);

}
//Leveling up method:
if (score > 90)

speedX = 5;
speedY = 5;

if (score > 190) {

speedX = 10;
speedY = 10;

}

}
Warning.. You need to declare the new functions and objects in .h

This code now is making your image move down to the left, so if you put the player image in the direct path of your image it will hit and the score will increase and so will the speed...
This can quickly become a "dodge" game.. you just need to make the player follow your finger with the touchesMoved method!

Last edited by NorthCode; 02-04-2011 at 06:14 AM.
NorthCode is offline   Reply With Quote
Old 02-04-2011, 08:00 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default @NorthCode

Thank you very much...I actually tried your first suggestion and what I did was to include speedX++ and speedY++ in the animate method. The ball actually increased in speed but its movement became erratic and at some point disappeared from the screen. I also noticed that it did not travel completely in the reverse direction (i.e speedX = - speedX) as the speed decreases about half way through the travel and then increased in the opposite direction...
this is the code I am using
Code:
- (void)startGame {
	if (!lives) {
		lives = 3;
		score = 0;
	
			
			
	}
	scoreLabel.text = [NSString stringWithFormat:@"%05d", score];
	livesLabel.text = [NSString stringWithFormat:@"%d", lives];
		
		
		
	ball.center = CGPointMake(149,50);		
	
		ballMove = CGPointMake(2,2);
	
		
		// choose whether the ball moves left to right or right to left
		if (arc4random() % 310 < 50)
	 ballMove.x = -ballMove.x;
		
	
		
	
	
		[self initializeTimer];
}




- (void)initializeTimer {
		if (theTimer == nil) {
			theTimer = [CADisplayLink displayLinkWithTarget:self 
												   selector:@selector(animate)];
			theTimer.frameInterval = 0.1f;
			[theTimer addToRunLoop: [NSRunLoop currentRunLoop] 
						   forMode: NSDefaultRunLoopMode];
			
			
		}
		

	}

	- (void)animate {
	
		ball.center = CGPointMake(ball.center.x+ballMove.x,
			ball.center.y+ballMove.y);
	
	
			
				
		BOOL playerCollision = ball.center.y >= player.center.y - 32 &&
		ball.center.y <= player.center.y +32 &&
	ball.center.x >player.center.x - 48 &&
		ball.center.x < player.center.x + 48;
	
		
		if(playerCollision){				
			ballMove.y = -ballMove.y;
		if (ball.center.y >= player.center.y - 32 && ballMove.y < 0) {
				ball.center = CGPointMake(ball.center.x, player.center.y - 32);
			} else if (ball.center.y <= player.center.y + 32 && ballMove.y > 0) {
				ball.center = CGPointMake(ball.center.x, player.center.y + 32);
			} else if (ball.center.x >= player.center.x - 48 && ballMove.x < 0) {
				ball.center = CGPointMake(player.center.x -48, ball.center.y);
			} else if (ball.center.x <= player.center.x + 48 && ballMove.x > 0) {
				ball.center = CGPointMake(player.center.x + 48, ball.center.y);
			} 
		}
I will like to have a timer determine how long a particular live will last and the score from the collisions between the player and the image.

So looking at my code above where will you advise that I fit in the speed component and the increase in the score taking into consideration the timer element for stopping the game.

thanks once again meanwhile i will work on this new suggestion you have given and compare with your response to see what I will be doing wrong.
Thanks once again . I appreciate your time, help and effort.
Cheers!!
eyinEkpe is offline   Reply With Quote
Old 02-04-2011, 09:07 AM   #6 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 46
NorthCode is on a distinguished road
Default

Quote:
Originally Posted by eyinEkpe View Post
Thank you very much...I actually tried your first suggestion and what I did was to include speedX++ and speedY++ in the animate method. The ball actually increased in speed but its movement became erratic and at some point disappeared from the screen. I also noticed that it did not travel completely in the reverse direction (i.e speedX = - speedX) as the speed decreases about half way through the travel and then increased in the opposite direction...
I understand.. If the image is moving and the x value is increasing, when you make the x negative it will still increase upwards with time..
For example:
Image traveling by 2 coordinates to the left
Every 3 seconds or so.. the speed increases by 1
The image gets up to 10 coordinates and hits the edge of the screen
Your code tells the speedX to become -speedX, which in this case is -10
Every three seconds speedX increases.. so -10 becomes -9... -2 -1 until it becomes positive again
Thats why it slows down and changes direction..

What you need is a BOOL indicating what direction the image is traveling!

Code:
if (arc4random() % 310 < 50) {
	directionBool = YES;

}
else {

directionBool = NO;
}
Then you put this where you increase the speed!!:

Code:
if (directionBool == YES) {

speedX = speedX + 1;

//Or the code you use..
//ballMove = CGPointMake(ballMove.x + 1, ballMove.y);

}

else {

speedX = speedX - 1;
//Or the code you use..
//ballMove = CGPointMake(ballMove.x - 1, ballMove.y);

//You might need to swap these two, I dont know what is which direction in you game..

}
Hope that cleared it up
NorthCode is offline   Reply With Quote
Old 02-06-2011, 05:48 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default @ NorthCode

Hello,
I am still not getting the desired result..the ball still moves in an erratic way.
eyinEkpe is offline   Reply With Quote
Old 02-06-2011, 08:57 PM   #8 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

set an if statement that will only change the direction of the object upon hitting "self.view.bounds.size.width" and "0" for x (and "self.view.bounds.size.height" and "0" for y). Kinda like...

if (sub.center.x < 0) {
jetVelocity.x = -jetVelocity.x;
}

if (sub.center.x > self.view.bounds.size.width) {
jetVelocity.x = -jetVelocity.x;
}

so it reverses the polarity when bounds are hit. pretty simple but ingenious when you think about it.

and a better way to implement "boost speed upon score increase" would be to say

if(score%5 == 0){
//increase speed here
}

this code increases the speed every 5th point.

North Code pretty much gave you everything on a silver platter just do some trial and error to figure it out man.

Last edited by missing_no; 02-06-2011 at 09:01 PM.
missing_no is offline   Reply With Quote
Old 02-07-2011, 05:22 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default @missing_no

Thanks for your advice and suggestions. Yeah I agree with you that NorthCode was very,very helpful. I will try implementing your suggestion but may I ask, where in the code, is the best place to put or implement "boost speed upon score increase" you suggested?


Thanks once again for your time and effort. I appreciate it.
eyinEkpe is offline   Reply With Quote
Old 02-07-2011, 01:59 PM   #10 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

Quote:
Originally Posted by eyinEkpe View Post
Thanks for your advice and suggestions. Yeah I agree with you that NorthCode was very,very helpful. I will try implementing your suggestion but may I ask, where in the code, is the best place to put or implement "boost speed upon score increase" you suggested?


Thanks once again for your time and effort. I appreciate it.
You need to set up a function or something that will cycle constantly. i.e:

[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];

// this will go inside the -(void)viewDidLoad {} function

place the "inc. speed as score rises" anywhere within this gameLoop function. it doesn't matter if it's at the very beggining or end because it will loop at 60 times a second (of course you can modify this by changing the "NSTimer scheduledTimerWithTimeInterval" variable) and you won't be able to tell the difference.
missing_no is offline   Reply With Quote
Old 02-07-2011, 03:59 PM   #11 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 46
NorthCode is on a distinguished road
Default ..

And again, inside the gameLoop function you check for collisions (or whatever you need to check for) and if a collision happens you increase the score. Within that same method you can check if the score has increased with a if, if else statement. Thats what you need!
The gameLoop is called by the code missing_no provided.
Code:

-(void)gameLoop {
//Check for collision
if ("object collides with player") {
//Increase score
score = score + 10;

//Check score to see if the game should increase the speed of something..
if (score >= 30 && score < 60) { //Check if the score is between 30 and 60
//Increase the speed
speed = 3;
}

else if (score >= 60 && score < 90){ //Check if the score is between 60 and 90

speed = 5;

}

}


}

That should do it.. Just keep on customizing the code

Last edited by NorthCode; 02-07-2011 at 04:04 PM.
NorthCode is offline   Reply With Quote
Old 02-08-2011, 10:11 AM   #12 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default @NorthCode, @missing_no

Thanks guys for your effort and help. I have tried all your suggestions and still trying. However, there is significant progess in the sense that the image gains speed but there is a problem now and it has to do with maintaining a constant maximum speed.
In the code attached below, you will see that I tried to track the values of speedX and speedY by using NSLog but I cannot really determine the values at which should be the maximum speed.

Please try running the code below(don't forget to add an image for the ball object via IB) notice the movement of the ball and you will see that after a few bounces, the ball movement from start is not regular and after a few seconds it just bounces in the y direction only(i.e from NSLog the values of x and y alternates simultaneously between 0 and 1 and then gets off the screen.

.h
Code:
@interface ViewController : UIViewController 

{
	
	UIImageView *ball;
	int speedX;
	int speedY;
	CGPoint ballMovement;
}


@property (nonatomic, retain) IBOutlet UIImageView *ball;

- (void)initializeTimer;

- (void)animateBall:(NSTimer *)theTimer;


@end
.m
Code:
@implementation ViewController


@synthesize ball;


- (void)dealloc {
   
    [ball release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];

	
	
	ball.center = CGPointMake(50,50);
	
	speedX = 1;
	speedY = 1;
	
	ballMovement = CGPointMake(speedX,speedY);

	[self initializeTimer];
}

- (void)initializeTimer {
	float theInterval = 0.1f;
    [NSTimer scheduledTimerWithTimeInterval:theInterval target:self 
								   selector:@selector(animateBall:) userInfo:nil repeats:YES];
}

- (void)animateBall:(NSTimer *)theTimer {
	ball.center = CGPointMake(ball.center.x+speedX,
		ball.center.y+speedY);
	
       speedX += 1;
	speedY += 1;
	
	NSLog(@"%d,%d", speedX, speedY);
	
	if (speedX <= 1) 	
		speedX ==1;
	if (speedY <= 1)
		speedY ==1;
	
	
	if(ball.center.x > 300 || ball.center.x < 20)
		speedX = -abs(speedX);
	if(ball.center.y > 440 || ball.center.y < 40)
		speedY = -abs(speedY);
	
	
}
eyinEkpe is offline   Reply With Quote
Old 02-08-2011, 01:38 PM   #13 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 46
NorthCode is on a distinguished road
Default

Just reading trough your code Im finding some errors..
For example:
Code:
if (speedX <= 1) 	
		speedX ==1;
	if (speedY <= 1)
		speedY ==1;
You cant use "==" when setting a value. You need to use "="
So:

Code:
if (speedX <= 1) 	
		speedX =1;
	if (speedY <= 1)
		speedY =1;
And you are checking if the speed is less than 1, that means that whenever your speed is negative it will become one! So instead of if(speedX <=1) you need to check if it is 0. We will also need use of the directionBool again.
Code:
//Checking speedX
if (speedX == 0) {
if (directionBoolX == NO) {
speedX = 1;
}
else {
speedX = -1;
}
} 	
//Checking speedY
if (speedY == 0) {
if (directionBoolY == NO) {
speedY = 1;
}
else {
speedY = -1;
}
}
Declare the directionBoolX and directionBoolY, and switch the directions when you hit the "walls":


Code:
if(ball.center.x > 300) {
		speedX = -abs(speedX);
directionBoolX = YES;

}

if(ball.center.x < 20) {
speedX = -abs(speedX)
directionBoolX = NO;
}
	if(ball.center.y > 440) {
speedY = -abs(speedY);
directionBoolX = YES;
}
  if(ball.center.y < 40){
		speedY = -abs(speedY);
directionBoolX = NO;
}
If you want the ball to bounce correctly you need to use the directionBool code when accelerating (increasing/decreasing the speed, as showed earlier), unless thats not what you want. With your current code, the ball is leaning towards the top-right corner.

Last edited by NorthCode; 02-08-2011 at 01:46 PM.
NorthCode is offline   Reply With Quote
Old 02-08-2011, 01:50 PM   #14 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

The absolute value thing is completely unnecessary in fact it's messing up the direction the ball should be going

either use

speedX = -speedX

speedX *= -1;

you want to toggle the direction not always make it negative
missing_no is offline   Reply With Quote
Old 02-08-2011, 02:29 PM   #15 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 46
NorthCode is on a distinguished road
Default

Quote:
Originally Posted by missing_no View Post
The absolute value thing is completely unnecessary in fact it's messing up the direction the ball should be going

either use

speedX = -speedX

speedX *= -1;

you want to toggle the direction not always make it negative
That is very true.. I overlooked that in his code.. Thanks.
NorthCode is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, movement, object, speed, timer

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: 418
10 members and 408 guests
7twenty7, chemistry, ChrisYates, gmarro, hussain1982, Retouchable, skrew88, 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:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0