# [GameMaker:Studio] How to skip a frame after a code repeat

This topic is 916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, im new at this so i dont have a lot of experience but im developing a 2d game and i dont want to use the Game Maker movement controls, i want to make my own code for it, and i was doing it well until i noticed the player stops quickly and i want him to continue walking a few moments after i stop pressing the direction button, then i did this code here:

///Stop slowly
repeat(10)
{
while(walking_speed > 0)
{
while(walking_direction == 1)
{
x = x-walking_speed;
walking_speed -= 1;
if(walking_speed = 0)
{
walking_direction = 0;
}
}
while(walking_direction == 2)
{
x = x+walking_speed;
walking_speed -= 1;
if(walking_speed = 0)
{
walking_direction = 0;
}
}
}
}

Thats Game Maker code. Then, what it does is repeat that code 10 times (as the maximum walking_speed is 10, then it would be subtracting 1 to the walking_speed every time the code runs) and the code works, my problem is this one: The code runs the 10 times at the same time. I need it to run once every frame, and i actually dont have idea of how to do it.

This should be a really easy code, but i repeat, im new at this. Also i want to make a good game.

##### Share on other sites

Yeah, you are going about it the wrong way.  You will need to take the code in the repeat statement, and only run it once per frame in the step event.  You have the walking speed variable.  Since you didn't create it with the "var" keyword, it will stay around between frames.  So each frame, if walking speed is greater than zero, and you are not currently pressing the direction buttons to make it walk, then you can lower that speed.

Alternatively, there is a built-in friction variable.  If you use the built in movement, with vspeed, hspeed, etc... then you can simply make friction some value if you are not pressing a movement key, and it will automatically decrease your movement speed based on that.

##### Share on other sites

Yeah, you are going about it the wrong way.  You will need to take the code in the repeat statement, and only run it once per frame in the step event.  You have the walking speed variable.  Since you didn't create it with the "var" keyword, it will stay around between frames.  So each frame, if walking speed is greater than zero, and you are not currently pressing the direction buttons to make it walk, then you can lower that speed.

Alternatively, there is a built-in friction variable.  If you use the built in movement, with vspeed, hspeed, etc... then you can simply make friction some value if you are not pressing a movement key, and it will automatically decrease your movement speed based on that.

I tried to use that code in the step event but even when im pressing the movement button this code is still running each frame, and that makes my character move slower because of this code (its decreasing the movement speed every frame). I will try to use vspeed and hspeed and see what i get. Step event runs even when you're pressing the movement keys.

Edited by Zoehawks

##### Share on other sites

Yeah, you are going about it the wrong way.  You will need to take the code in the repeat statement, and only run it once per frame in the step event.  You have the walking speed variable.  Since you didn't create it with the "var" keyword, it will stay around between frames.  So each frame, if walking speed is greater than zero, and you are not currently pressing the direction buttons to make it walk, then you can lower that speed.

Alternatively, there is a built-in friction variable.  If you use the built in movement, with vspeed, hspeed, etc... then you can simply make friction some value if you are not pressing a movement key, and it will automatically decrease your movement speed based on that.

Also, if i use the "var" keyword for the walking_speed variable, i will not be able to access it from another code, and i need to.

##### Share on other sites

About stopping the slow-down in the step event, you need another variable if you are using the keyup/down events to start the movement.  This variable at the end of the step event I would set to "false" and only set it to true in the key events.  The step event would then not do the slowdown if the variable is true since you don't want it to.  You could also do key-checking directly in the step event, but it isn't necessary.  You still have to have the step event know somehow if the keys were down or not, and a variable is the simple way.

Also, about "var," I'm agreeing you shouldn't use it.  I mentioned it because I'm making the assumption that since you did not use it(intentionally did not use it), and therefore you can access it between step events, etc...You want the value to stay around between frames and for other events, so you are right by not using the "var" keyword for it.

##### Share on other sites
How I would implement acceleration and deceleration, when the player is pressing a movement key, increase the players velocity by a set amount, say 0.5, until it reaches a maximum speed of 4, then retain that speed while a key is pressed. If the player releases the key, decrease the movement speed by 0.5 untill it rests at 0. It may sound primitive but it works. Example -

If (moveleft) or (moveright) {
movespeed += acceleration
If (movespeed >= maxmovespeed) {
movespeed = maxmovespeed
}
}
Else {
While (movespeed > 0) {
movespeed -= acceleration
}
}

Something like that,sorry I'm on my break at work and doing this on my phone, some one can correct it if it's wrong or the idea can be simplified. From the top of my head it should work though.

##### Share on other sites

How I would implement acceleration and deceleration, when the player is pressing a movement key, increase the players velocity by a set amount, say 0.5, until it reaches a maximum speed of 4, then retain that speed while a key is pressed. If the player releases the key, decrease the movement speed by 0.5 untill it rests at 0. It may sound primitive but it works. Example -

If (moveleft) or (moveright) {
movespeed += acceleration
If (movespeed >= maxmovespeed) {
movespeed = maxmovespeed
}
}
Else {
While (movespeed > 0) {
movespeed -= acceleration
}
}

Something like that,sorry I'm on my break at work and doing this on my phone, some one can correct it if it's wrong or the idea can be simplified. From the top of my head it should work though.

Nah its ok, you helped me. This is a lot simpler than my actual code. Thank you

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633661
• Total Posts
3013226
×