• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

loop confusion

D

Dan Moore

Guest
Hey guys,

I have a object that speeds upon at certain intervals, and I am wanting cap it at a certain value. I figured a while or a do until loop wrapped around this code would work, but now when I run my game, the object hits the goal which should speed it up a bit and increase the score by one, but instead the score jumps to 28 and the object speeds jumps too. I am not sure how I have gone wrong. Code is below, so any help is appreciated, and yes I am aware how 'inelegant' my code is! lol

do
{
if ((sprite_index == spr_ball && obr_goabar.sprite_index == spr_goalbar) || (sprite_index == spr_ball_red && obr_goabar.sprite_index == spr_goalbar_red))
{
global.ballspeed += .25;
with obj_ball {
speed += .25;
}
score += 1;
}
else if ((sprite_index == spr_ball_green && obr_goabar.sprite_index == spr_goalbar_green) || (sprite_index == spr_ball_yellow && obr_goabar.sprite_index == spr_goalbar_yellow))
{
global.ballspeed += .25;
with obj_ball {
speed += .25;
}
score += 1;
}
else if ((sprite_index != spr_ball && obr_goabar.sprite_index != spr_goalbar) || (sprite_index != spr_ball_red && obr_goabar.sprite_index != spr_goalbar_red))
{
global.ballspeed -= 1;
with obj_ball {
speed -= 1;
}
score -= 1;
}
else if ((sprite_index != spr_ball_green && obr_goabar.sprite_index != spr_goalbar_green) || (sprite_index != spr_ball_yellow && obr_goabar.sprite_index != spr_goalbar_yellow))
{
global.ballspeed -= 1;
with obj_ball {
speed -= 1;
}
score -= 1;
}
}
until (global.speed == 10)


Thanks guys!
 
D

Docker

Guest
get rid of do and until and instead use if (global.speed < 10) and put all of the code inside it.
A do/until and while will just keep running until the condition is met, its not a one and done thing like I believe you're trying to use it
 
G

Gillen82

Guest
You don't need a loop for capping off the speed. All you have to do is use the following:

Code:
if ( global.speed >= 10 ) {
    
    global.speed  = 10;

}
 
D

Dan Moore

Guest
ah ok, guess I was thinking about it wrong, thought one of those loops would be better!
 
D

Docker

Guest
You don't need a loop for capping off the speed. All you have to do is use the following:

Code:
if ( global.speed >= 10 ) {
   
    global.speed  = 10;

}
That way would mean it would complete all the computation and then just revert speed back to 10 which is unneccessary
 
G

Gillen82

Guest
get rid of do and until and instead use if (global.speed < 10) and put all of the code inside it.
A do/until and while will just keep running until the condition is met, its not a one and done thing like I believe you're trying to use it
This might not work as expected. If he wraps this around his code, the speed will not stop at the value of 10. This will result in the rest of the code not functioning as the condition will no longer be true when the speed goes above 10.
 
D

Dan Moore

Guest
i think it was more the if statement around it, won't be setting it to the value I am checking for, as you say it would revert it and defeat the purpose of the check...
 
G

Gillen82

Guest
He wan
That way would mean it would complete all the computation and then just revert speed back to 10 which is unneccessary
He wants to cap the speed off at the value of 10, so this method ensures that will happen :)
 
D

Docker

Guest
Just realised what you mean, there is a decrement to speed as well which will never run once the condition is met
I just took his code literal and wrote it in a way that done the exact same thing without constant running until condition is met
 
Last edited:
G

Gillen82

Guest
Another wee tip that might help....I notice that there are referring to two different speeds in each statement. Instead, go to the Step Event of the ball and add:

Code:
speed = global.ballspeed;
This way, you won't have to keep modifying both speeds. After that, you could change how you write your "if" statements.

Code:
if ((sprite_index == spr_ball && obr_goabar.sprite_index == spr_goalbar) || (sprite_index == spr_ball_red && obr_goabar.sprite_index == spr_goalbar_red))
{
global.ballspeed = min ( global.ballspeed + .25, 10 );
}
 
D

Dan Moore

Guest
Hey guys, thanks for the help, but I have just realized something. If i put a loop or an if statement around my code to check if the speed is 10 or more, once it gets to 10, the game will stop increasing the score. I want players to be able to keep gaining score but the ball object to not get faster as eventually it gets so fast it breaks the collision detection.

I realized this after I tried your suggestions, but nothing worked. I don't currently use a step event in my ball object. This is my first game and I couldn't quite get my head around how to use the step events. I will have to have a think and come up with some other way to cap the speed of the ball.

Thank you so much for you help, wouldn't have got there otherwise!
 
Top