# Windows(Solved) Move then Stop (rogue-like)...(problem)

M

#### Merrick

##### Guest
Hi folks,
Having a problem with creating an object that will move a set distance (one 32x32 space, no diagonals) then stop (like in most rogue-like games).

Here's the code I've been playing with(which isn't working, the object just keeps moving up when the key is pressed and doesn't stop). Clearly I'm not seeing something here:

if keyboard_check(vk_up)
{
if place_empty(x,y-32)
{
goal_y = y-32;
vspeed = -4;
if y = goal_y
{
vspeed = 0;
}
}
}

I've managed to achieve my goal using different code, but I found it difficult to implement into a turn-based rogue-like game. Here's the alternate code that I abandoned. Maybe some of you will see a way to make this work in a turn based rogue-like game. I sure couldn't.

if place_snapped(32,32) && vspeed = 0
{
hspeed = keyboard_check(vk_right)*4 -keyboard_check(vk_left)*4;
dx = lengthdir_x(speed,direction);
if (place_meeting(x+dx,y,obj_wall)) {speed = 0}
}

if place_snapped(32,32) && hspeed = 0
{
vspeed = keyboard_check(vk_down)*4 -keyboard_check(vk_up)*4;
dy = lengthdir_y(speed,direction);
if (place_meeting(x,y+dy,obj_wall)) {speed = 0}
if (vspeed != 0){global.step += 1;}
}

If anyone knows what I'm missing/doing wrong, particularly in that first set of code, I would greatly appreciated the help.

Cheers,

PS: Alternatively, I can successfully create rogue-like movement where the object just jumps to the next spot, but I'm aiming to make movement animations this time around, so that unfortunately isn't an option anymore.

#### YoSniper

##### Member
Your "goal setting" should probably happen on a key press rather than just a key check.

Here's something that I would do:

Create Event
Code:
``````//Include this flag
moving = false;``````
Step Event
Code:
``````if moving {
if x == goal_x and y == goal_y {
moving = false;
vspeed = 0;
hspeed = 0;
}
} else {
if keyboard_check_pressed(vk_up) and place_empty(x, y-32) {
goal_x = x;
goal_y = y-32;
moving = true;
} else if keyboard_check_pressed(vk_down) and place_empty(x, y+32) {
goal_x = x;
goal_y = y+32;
moving = true;
}
//Do similar for left and right here
}``````
Notice how it is important that I distinguish between when I am moving and when I am not. Please see if this code works for you, and if you have any other questions, please ask.

M

#### Merrick

##### Guest
Thank you YoSniper, i've tried implementing your code. Perhaps I implemented it incorrectly, because for me the object just keeps on moving in the direction I pressed.

Code:
``````moving = flase;

if moving {
if x == goal_x and y == goal_y {
moving = false;
vspeed = 0;
hspeed = 0;
}
} else {
if keyboard_check_pressed(vk_up) and place_empty(x, y-32) {
goal_x = x;
goal_y = y-32;
moving = true;
vspeed = -4;   //this is what i added to make it move
} else if keyboard_check_pressed(vk_down) and place_empty(x, y+32) {
goal_x = x;
goal_y = y+32;
moving = true;
vspeed = 4;    //... and here too.``````

#### YoSniper

##### Member
If the first line:
Code:
``moving = false; //You misspelled "false" btw``
SHOULD NOT be in the Step Event with the rest of the code. I put it in the Create Event just to initialize it.

If it's in the Step Event like I think it is, then the code within the block
Code:
``if moving``
will never execute. And that's why the vspeed/hspeed never gets reset.

M

#### Merrick

##### Guest
Thanks again YoSniper, that solved it!
I also noticed that I forgot to changed the goal_x variables for left and right keys, but now it works great!

very much appreciate it,
you were a great help!

M

#### Merrick

##### Guest
Hey, just wanted to add a little bug i noticed.
When pressing, for example, both up and right keys, the object blasts off on the diagonal and cannot be stopped. Diagonal movement isn't required for my project so I'm gonna try and troubleshoot this.

Is it customary here to start a new thread when a new problem arises in the current thread?

M

#### Merrick

##### Guest
Hey, just wanted to add a little bug i noticed.
When pressing, for example, both up and right keys, the object blasts off on the diagonal and cannot be stopped. Diagonal movement isn't required for my project so I'm gonna try and troubleshoot this.

Is it customary here to start a new thread when a new problem arises in the current thread?
Update: fixed this issue by adding hspeed = 0 to the keyboard check if statement. See below:
Code:
``````if moving {
if x == goal_x and y == goal_y {
moving = false;
vspeed = 0;
hspeed = 0;
pa_enemy.energy += pa_enemy.speedy
turn = 0
}
} else {
if keyboard_check_direct(vk_up) and place_empty(x, y-32) && hspeed = 0 {
goal_x = x;
goal_y = y-32;
moving = true;
vspeed = -4;
} else if keyboard_check_direct(vk_down) and place_empty(x, y+32) && hspeed = 0{
goal_x = x;
goal_y = y+32;
moving = true;
vspeed = 4;
}``````
Also, noticed that changing the keyboard_check_presed to keyboard_check_direct allows for continual movement while key is held down.

S

#### Spartan121

##### Guest
Try to do that thing:
Code:
``````moved=0;
xmove=x;
ymove=y;``````
Code:
``````var leftr,leftp,rightr,rightp,upr,upp,downr,downp,released,pressed;
leftr=keyboard_check_released(vk_left);
rightr=keyboard_check_released(vk_right);
upr=keyboard_check_released(vk_up);
downr=keyboard_check_released(vk_down);
released=leftr or rightr or upr or downr;
leftp=keyboard_check_pressed(vk_left);
rightp=keyboard_check_pressed(vk_right);
upp=keyboard_check_pressed(vk_up);
downp=keyboard_check_pressed(vk_down);
pressed=leftp or rightp or upp or downp;

if moved=0 && pressed
{
xmove=x;
ymove=y;
moved=1;
}
if moved=1 && point_in_circle(x,y,xmove,ymove,32)
{
if leftp
{
//move left
}
//and for others
if released
{
moved=0;
}
}
else
{
moved=0;
}``````