# How do i limit the number of jumps in a side scroller

C

#### chocolatehog

##### Guest
I recently started using game maker and have begun making a simple side scroller but am having trouble limiting the amount of jumps a character can make.
I tried to fix this problem on my own to no prevail.
Here is the code i am using for movement

var num limit = 0;
if place_meeting(x,y+1,obj_dirt_block) {gravity = 0}
else {gravity = 1}
if place_meeting(x,y+1,obj_grass_block) {gravity = 0}
else {gravity = 1}

if keyboard_check(vk_right) && place_free(x+1,y) {x+=5}
if keyboard_check(vk_left) && place_free(x-1,y) {x-=5}
if keyboard_check_pressed(vk_up) && limit = 0 {vspeed -= 10}
if keyboard_check_pressed(vk_up) && limit < 2 {limit += 1}
if place_meeting(x,y+1,obj_dirt_block) {limit = 0}
if place_meeting(x,y+1,obj_grass_block) {limit = 0}

any ideas?

#### jo-thijs

##### Member
Hi and welcome to the GMC!

I assume you meant limiting the amount of double jumps, rather than total amount of jumps.

There some problems in your code:

1) This part is made a bit confusing:
Code:
``var num limit = 0;``
What this does, is creating a temporary variable num, assigning it no value
and assigning the value 0 to the non-temporary variable limit.
Code:
``limit = 0;``
2) In this piece of code the last 2 lines undo what the first 2 lines do:
Code:
``````if place_meeting(x,y+1,obj_dirt_block) {gravity = 0}
else {gravity = 1}
if place_meeting(x,y+1,obj_grass_block) {gravity = 0}
else {gravity = 1}``````
You probably rather want to do this:
Code:
``````if place_meeting(x, y + 1, obj_dirt_block)
|| place_meeting(x, y + 1, obj_grass_block)
gravity = 0;
else
gravity = 1;``````
3) You can do even better than that code, by using parents.
If you create a parent object for the obj_dirt_block and obj_grass_block, you only have to perform 1 collision check.

4) These lines of code check for a collision 1 pixel away, while they should check for collision 5 pixels away:
Code:
``````if keyboard_check(vk_right) && place_free(x+1,y) {x+=5}
if keyboard_check(vk_left) && place_free(x-1,y) {x-=5}``````
5) The previous code suggests you're using solid objects, which is disrecommended, they often cause more trouble than they do good.

6) These lines are unoptimized and look wrong:
Code:
``````if keyboard_check_pressed(vk_up) && limit = 0 {vspeed -= 10}
if keyboard_check_pressed(vk_up) && limit < 2 {limit += 1}``````
This looks like it allows no double jump at all, because you only set vspeed relative to -10 if limit = 0, not if limit < 2.
I'm also guessing you rather want to set vspeed to -10, not relative.
This way, those lines would become:
Code:
``````if keyboard_check_pressed(vk_up) && limit < 2 {
vspeed = -10;
limit++;
}``````
7) These lines of code can also be shortened by using parent objects for blocks:
Code:
``````if place_meeting(x,y+1,obj_dirt_block) {limit = 0}
if place_meeting(x,y+1,obj_grass_block) {limit = 0}``````
8) I suppose this is all executed in the step event?
If you set limit to 0 at the start of every step event,
it defeats the whole purpose of having limit there at all.
You should set limit to 1 in the create event and not reset it to 0 at the start of the step event.

C

#### chocolatehog

##### Guest
Hi and welcome to the GMC!

I assume you meant limiting the amount of double jumps, rather than total amount of jumps.

There some problems in your code:

1) This part is made a bit confusing:
Code:
``var num limit = 0;``
What this does, is creating a temporary variable num, assigning it no value
and assigning the value 0 to the non-temporary variable limit.
Code:
``limit = 0;``
2) In this piece of code the last 2 lines undo what the first 2 lines do:
Code:
``````if place_meeting(x,y+1,obj_dirt_block) {gravity = 0}
else {gravity = 1}
if place_meeting(x,y+1,obj_grass_block) {gravity = 0}
else {gravity = 1}``````
You probably rather want to do this:
Code:
``````if place_meeting(x, y + 1, obj_dirt_block)
|| place_meeting(x, y + 1, obj_grass_block)
gravity = 0;
else
gravity = 1;``````
3) You can do even better than that code, by using parents.
If you create a parent object for the obj_dirt_block and obj_grass_block, you only have to perform 1 collision check.

4) These lines of code check for a collision 1 pixel away, while they should check for collision 5 pixels away:
Code:
``````if keyboard_check(vk_right) && place_free(x+1,y) {x+=5}
if keyboard_check(vk_left) && place_free(x-1,y) {x-=5}``````
5) The previous code suggests you're using solid objects, which is disrecommended, they often cause more trouble than they do good.

6) These lines are unoptimized and look wrong:
Code:
``````if keyboard_check_pressed(vk_up) && limit = 0 {vspeed -= 10}
if keyboard_check_pressed(vk_up) && limit < 2 {limit += 1}``````
This looks like it allows no double jump at all, because you only set vspeed relative to -10 if limit = 0, not if limit < 2.
I'm also guessing you rather want to set vspeed to -10, not relative.
This way, those lines would become:
Code:
``````if keyboard_check_pressed(vk_up) && limit < 2 {
vspeed = -10;
limit++;
}``````
7) These lines of code can also be shortened by using parent objects for blocks:
Code:
``````if place_meeting(x,y+1,obj_dirt_block) {limit = 0}
if place_meeting(x,y+1,obj_grass_block) {limit = 0}``````
8) I suppose this is all executed in the step event?
If you set limit to 0 at the start of every step event,
it defeats the whole purpose of having limit there at all.
You should set limit to 1 in the create event and not reset it to 0 at the start of the step event.
Thanks a lot that gave a lot of useful information, but did not fix my problem was that you could jump infinity into the air

#### jo-thijs

##### Member
That's what I said in 8, the first line of your code resets limit to 0 at the start of every step,
causing your character to be "at the ground" every step.

Delete the first line and set limit to 1 in the create event.

C

#### chocolatehog

##### Guest
nvm messed some other code up thanks