M
Mili90
Guest
Hello everyone!
This is my first post on here and I'm quite a beginner so I apologise if I make some total schoolboy errors.
Here's my situation. I've managed to make my main character walk up, down, left, right and diagonally too. Perfect.
However, what I want to do now (and I've posted this on Reddit as well) is to make my character stop when two opposite buttons have been pressed (e.g. the up button and then the down button). When I start walking, let's say left, and then I press the right button, my character stops and faces the original direction he was walking in (left in this case). That's totally good.
But here's the major problem. When I let go of the right button to carry on moving left, my character only moves one pixel to the left and doesn't continue moving left like I'd like him too. However, if I let go of the left key and then start moving right, he happily goes to the right.
Here's my code (and just for the record, I think I've made things a little bit too complicated in the coding so I totally apologise. If anyone can give me any pointers on how to shorten the code or make it more simple, that'd be lovely! Finally, if someone needs a video of my problem, I'll try send one in this thread).
//Set direction to "None" if keys are not pressed
if(Direction = "Right") && (!keyboard_check(vk_right)) || (Direction = "Right") && (keyboard_check (vk_right)) && (!keyboard_lastkey = vk_left)
Direction = "None"
if(Direction = "Up") && (!keyboard_check(vk_up)) || (Direction = "Up") && (keyboard_check (vk_up)) && (keyboard_lastkey = vk_down)
Direction = "None"
if(Direction = "Down") && (!keyboard_check(vk_down)) || (Direction = "Down") && (keyboard_check (vk_down)) && (keyboard_lastkey = vk_up)
Direction = "None"
if(Direction = "Left") && (!keyboard_check(vk_left)) || (Direction = "Left") && (keyboard_check (vk_left)) && (keyboard_lastkey = vk_right)
Direction = "None"
//Move Down
if (keyboard_check(vk_down)) && (place_free(x, y+2))
{
vspeed +=3
if(Direction = "None")
{
sprite_index = spr_rundwn
Direction = "Down"
}
}
//Move Up
if (keyboard_check(vk_up)) && (place_free(x, y-2))
{
vspeed -=3
if(Direction = "None")
{
sprite_index = spr_runup
Direction = "Up"
}
}
//Move Left
if (keyboard_check(vk_left)) && (place_free(x-2, y))
{
hspeed -=3
if(Direction = "None")
{
sprite_index = spr_runlft
Direction = "Left"
}
}
//Move Right
if (keyboard_check(vk_right)) && (place_free(x+2, y))
{
hspeed +=3
if(Direction = "None")
{
sprite_index = spr_runrgt
Direction = "Right"
}
}
//Stop Animations
if (!keyboard_check (vk_down)) && (!keyboard_check (vk_up)) && (!keyboard_check (vk_left)) && (!keyboard_check (vk_right))
{
if (sprite_index = spr_rundwn)
sprite_index = spr_lookdwn
if (sprite_index = spr_runup)
sprite_index = spr_lookup
if (sprite_index = spr_runlft)
sprite_index = spr_looklft
if (sprite_index = spr_runrgt)
sprite_index = spr_lookrgt
}
//Opposite Keys (Down First)
if (keyboard_check (vk_down)) && (keyboard_lastkey = vk_up)
{
Direction = "None"
speed = 0
sprite_index = spr_lookdwn
if (keyboard_check_released (vk_up)) && (keyboard_check(vk_down))
{
vspeed +=3
sprite_index = spr_rundwn
Direction = "Down"
}
}
//Opposite Keys (Up First)
if (keyboard_check (vk_up)) && (keyboard_lastkey = vk_down)
{
Direction = "None"
speed = 0
sprite_index = spr_lookup
if (keyboard_check_released (vk_down)) && (keyboard_check(vk_up))
{
vspeed -=3
sprite_index = spr_runup
Direction = "Up"
}
}
//Opposite Keys (Left First)
if (keyboard_check (vk_left)) && (keyboard_lastkey = vk_right)
{
Direction = "None"
speed = 0
sprite_index = spr_looklft
if (keyboard_check_released (vk_right)) && (keyboard_check(vk_left))
{
hspeed -=3
sprite_index = spr_runlft
Direction = "Left"
}
}
//Opposite Keys (Right First)
if (keyboard_check (vk_right)) && (keyboard_lastkey = vk_left)
{
Direction = "None"
speed = 0
sprite_index = spr_lookrgt
if (keyboard_check_released (vk_left)) && (keyboard_check(vk_right))
{
hspeed +=3
sprite_index = spr_runrgt
Direction = "Right"
}
}
//Other
if (speed>6) speed=6
x=min(x,room_width-32);
x=max(x,0);
y=min(y,room_height-32);
y=max(y,0);
This is my first post on here and I'm quite a beginner so I apologise if I make some total schoolboy errors.
Here's my situation. I've managed to make my main character walk up, down, left, right and diagonally too. Perfect.
However, what I want to do now (and I've posted this on Reddit as well) is to make my character stop when two opposite buttons have been pressed (e.g. the up button and then the down button). When I start walking, let's say left, and then I press the right button, my character stops and faces the original direction he was walking in (left in this case). That's totally good.
But here's the major problem. When I let go of the right button to carry on moving left, my character only moves one pixel to the left and doesn't continue moving left like I'd like him too. However, if I let go of the left key and then start moving right, he happily goes to the right.
Here's my code (and just for the record, I think I've made things a little bit too complicated in the coding so I totally apologise. If anyone can give me any pointers on how to shorten the code or make it more simple, that'd be lovely! Finally, if someone needs a video of my problem, I'll try send one in this thread).
//Set direction to "None" if keys are not pressed
if(Direction = "Right") && (!keyboard_check(vk_right)) || (Direction = "Right") && (keyboard_check (vk_right)) && (!keyboard_lastkey = vk_left)
Direction = "None"
if(Direction = "Up") && (!keyboard_check(vk_up)) || (Direction = "Up") && (keyboard_check (vk_up)) && (keyboard_lastkey = vk_down)
Direction = "None"
if(Direction = "Down") && (!keyboard_check(vk_down)) || (Direction = "Down") && (keyboard_check (vk_down)) && (keyboard_lastkey = vk_up)
Direction = "None"
if(Direction = "Left") && (!keyboard_check(vk_left)) || (Direction = "Left") && (keyboard_check (vk_left)) && (keyboard_lastkey = vk_right)
Direction = "None"
//Move Down
if (keyboard_check(vk_down)) && (place_free(x, y+2))
{
vspeed +=3
if(Direction = "None")
{
sprite_index = spr_rundwn
Direction = "Down"
}
}
//Move Up
if (keyboard_check(vk_up)) && (place_free(x, y-2))
{
vspeed -=3
if(Direction = "None")
{
sprite_index = spr_runup
Direction = "Up"
}
}
//Move Left
if (keyboard_check(vk_left)) && (place_free(x-2, y))
{
hspeed -=3
if(Direction = "None")
{
sprite_index = spr_runlft
Direction = "Left"
}
}
//Move Right
if (keyboard_check(vk_right)) && (place_free(x+2, y))
{
hspeed +=3
if(Direction = "None")
{
sprite_index = spr_runrgt
Direction = "Right"
}
}
//Stop Animations
if (!keyboard_check (vk_down)) && (!keyboard_check (vk_up)) && (!keyboard_check (vk_left)) && (!keyboard_check (vk_right))
{
if (sprite_index = spr_rundwn)
sprite_index = spr_lookdwn
if (sprite_index = spr_runup)
sprite_index = spr_lookup
if (sprite_index = spr_runlft)
sprite_index = spr_looklft
if (sprite_index = spr_runrgt)
sprite_index = spr_lookrgt
}
//Opposite Keys (Down First)
if (keyboard_check (vk_down)) && (keyboard_lastkey = vk_up)
{
Direction = "None"
speed = 0
sprite_index = spr_lookdwn
if (keyboard_check_released (vk_up)) && (keyboard_check(vk_down))
{
vspeed +=3
sprite_index = spr_rundwn
Direction = "Down"
}
}
//Opposite Keys (Up First)
if (keyboard_check (vk_up)) && (keyboard_lastkey = vk_down)
{
Direction = "None"
speed = 0
sprite_index = spr_lookup
if (keyboard_check_released (vk_down)) && (keyboard_check(vk_up))
{
vspeed -=3
sprite_index = spr_runup
Direction = "Up"
}
}
//Opposite Keys (Left First)
if (keyboard_check (vk_left)) && (keyboard_lastkey = vk_right)
{
Direction = "None"
speed = 0
sprite_index = spr_looklft
if (keyboard_check_released (vk_right)) && (keyboard_check(vk_left))
{
hspeed -=3
sprite_index = spr_runlft
Direction = "Left"
}
}
//Opposite Keys (Right First)
if (keyboard_check (vk_right)) && (keyboard_lastkey = vk_left)
{
Direction = "None"
speed = 0
sprite_index = spr_lookrgt
if (keyboard_check_released (vk_left)) && (keyboard_check(vk_right))
{
hspeed +=3
sprite_index = spr_runrgt
Direction = "Right"
}
}
//Other
if (speed>6) speed=6
x=min(x,room_width-32);
x=max(x,0);
y=min(y,room_height-32);
y=max(y,0);