TheNegroShoddy
Member
Been searching via google and looked at a few youtube videos, but I can't find a good tutorial on how to do poke'mon-like movement. From most of the tutorial codes I've seen most suggest using a timer variable and a grid variable, but that didn't work well for me, so I took the code out. Was hoping somewhere here has an simpler solution.
Here is my code: (note my object is alligned to a 32x32 grid).
create event
Step event
Here is my code: (note my object is alligned to a 32x32 grid).
create event
Code:
movespeed = 4; // how fast player will move...?
moving = false; // Whether the player is moving or not
// movement keys
up_key = "W";
down_key = 'S';
left_key = 'A';
right_key = 'D';
// Other keys
confirm_key = 'N' // equal to X or A on a controller
back_key = 'K'; // equal to O or B on a controller
// Other important variables
can_move = true;
Code:
// Check if player can move first
if can_move == true
{
var can_move_vert = true, can_move_horiz = true; // prevent moving vertically and horizontally at the same time
if can_move_vert == true
if keyboard_check(ord(up_key)) //&& !keyboard_check(ord(left_key)) && !keyboard_check(ord(right_key))
{
can_move_horiz = false;
moving = true;
y -= movespeed;
}
if can_move_vert == true
if keyboard_check(ord(down_key)) //&& !keyboard_check(ord(left_key)) && !keyboard_check(ord(right_key))
{
can_move_horiz = false;
moving = true;
y += movespeed
}
if can_move_horiz == true
if keyboard_check(ord(left_key)) //&& !keyboard_check(ord(down_key)) && !keyboard_check(ord(up_key))
{
can_move_vert = false;
moving = true;
x -= movespeed;
}
if can_move_horiz == true
if keyboard_check(ord(right_key)) //&& !keyboard_check(ord(down_key)) && !keyboard_check(ord(up_key))
{
can_move_vert = false;
moving = true;
x += movespeed;
}
// Change the state of moving variable to false when the player isn't moving
if moving == true
if keyboard_check_released(ord(left_key)) || keyboard_check_released(ord(right_key)) || keyboard_check_released(ord(up_key)) || keyboard_check_released(ord(down_key))
{ moving = false }
// Reset the ability to move horiziontally or vertically
if keyboard_check_released(ord(left_key)) || keyboard_check_released(ord(right_key)) && can_move_vert == false
{ can_move_vert = true; }
if keyboard_check_released(ord(up_key)) || keyboard_check_released(ord(down_key)) && can_move_horiz == false
{ can_move_horiz = true; }
// Prevent movement if all keys are held at the same time
if keyboard_check(ord(right_key)) && keyboard_check(ord(left_key)) && keyboard_check(ord(up_key)) && keyboard_check(ord(down_key))
{
can_move_horiz = false
can_move_vert = false
}
}