Legacy GM Get a block to move so many points and stop [Solved]

R

rmcdra

Guest
Trying to make a block that you can push. It moves a set number of points (16 points) and then stops, until you push it again. tried using the move_towards_point function but because the point I want it to move to is variable, it doesn't stop moving when it reaches the point. I've included my code attempts so far:
In this code the object doesn't move
Code:
if (sprite_index == spr_walking_left)
{
    for (l = 0; l < 17; l++)
    {
        obj_move_block.hspeed = (1/16)*l - 1;
    }
}
In this code the object moves but doesn't stop moving
Code:
if (sprite_index == spr_walking_left)
{
    obj_move_block.x -= 1;
    obj_move_block.hspeed = -1;
    if (obj_move_block.x % 16 = 0)
    {
    obj_move_block.hspeed = 0;
    }
}
Any help would be welcome
 

NicoDT

Member
Hello! try this and let me know
Code:
if (sprite_index == spr_walking_left)
{
   obj_move_block.hspd = -1;
   obj_move_block.x_end = obj_move_block.x-16
}
And add this step event on the block
Code:
if (round(x) = x_end) hspd = 0
//make sure to create the variables x_end and y_end in the create event
 
R

rmcdra

Guest
Hello! try this and let me know
Code:
if (sprite_index == spr_walking_left)
{
   obj_move_block.hspd = -1;
   obj_move_block.x_end = obj_move_block.x-16
}
And add this step event on the block
Code:
if (round(x) = x_end) hspd = 0
//make sure to create the variables x_end and y_end in the create event
Block moves but doesn't stop sadly. Seemed like it would have worked though
 

NicoDT

Member
Is there a chance that the code keeps running and the x_end keeps updating?

Can you check in the debugger to see what happens? The position of the block should be closer to the x_end position each time until it reaches it and stops.

Let me know
 
R

rmcdra

Guest
Is there a chance that the code keeps running and the x_end keeps updating?

Can you check in the debugger to see what happens? The position of the block should be closer to the x_end position each time until it reaches it and stops.

Let me know
I really don't know how to use the debugger application and the guide for it isn't making sense to me. I still had hspeed in the code. Updated it the variable hspd that you used and from what I could see the x_end calculates once but the object doesn't move. When I used hspeed in place of the variable hspd the block moves but doesn't stop. x_end seems to calculate once but the x coordinates of the block continues to decrease though.

With these two current pieces of code, the object doesn't move
Step event in object
Code:
if (round(x) = x_end) hspd = 0;
Collision event with object
Code:
if (sprite_index == spr_walking_left)
{
    obj_move_block.hspd = -1;
    obj_move_block.x_end = obj_move_block.x-16
}
UPDATE
Don't know what changed other than using hspeed instead of hspd but the code works as intended now. Which is strange because it wasn't working when I did put in hspeed but anyway it's good. Thank you very much. Figured out how to use the debugging windows as well.
Code:
if (sprite_index == spr_gan_walking_left)
{
    obj_move_block.hspeed = -1;
    obj_move_block.x_end = obj_move_block.x-16
}
Code:
if (round(x) = x_end) hspeed = 0;
 
Last edited by a moderator:
Top