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
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..
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!
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
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!!
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!
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..
}
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.
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.
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:
// 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.
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
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.
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.
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.