Legacy GM Need help with movement.

L

LWCreations

Guest
Hi, I'm an absolute beginner to Gamemaker coming from Fusion 2.5 and having some issues with movement that doesn't make sense in why it's not working, I'm using GML rather than the DnD interface as I want to move away from being limited to what I can do and need more freedom.

Currently, I'm trying to move my player in increments of 4 pixels when XRemaining is greater than 0 but instead of move 16 pixels as that is what I add the XRemaining it only moves 4 pixels.

I'll add the code below so it makes more sense, hopefully, someone can help me.

This code is in the STEP event.
Code:
XSpeedInc = 4;
YSpeedInc = 4;
XRemaining = 0;
YRemaining = 0;

//Directions are 1 = up, 2 = right, 3 = down, 4 = left
PlayerDirection = 0;


if keyboard_check(vk_left) && place_free (x-16,y) && XRemaining = 0
{
//    x = (x - XSpeedInc);
    XRemaining = 16;
    PlayerDirection = 4;
}   

if keyboard_check(vk_right) && place_free (x+16,y) && XRemaining = 0
{
//    x = (x + XSpeedInc);
    XRemaining = (16);
    PlayerDirection = 2;
}   

//Left Movement
if XRemaining > 0 && place_free (x-XSpeedInc,y) && PlayerDirection = 4
{
    x = (x - XSpeedInc);
    XRemaining = XRemaining - XSpeedInc
}

//Right Movement
if XRemaining > 0 && place_free (x+XSpeedInc,y) && PlayerDirection = 2
{
    x = (x + XSpeedInc);
    XRemaining = XRemaining - XSpeedInc
}
I feel like this should work but it doesn't work as it should.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You should separate out your inputs from the movement and deal with them separately. Something like this perhaps:

GML:
// CREATE EVENT ONLY
XSpeedInc = 4;
YSpeedInc = 4;
XRemaining = 0;
YRemaining = 0;

//Directions are 1 = up, 2 = right, 3 = down, 4 = left
PlayerDirection = 0;

// STEP EVENT
if XRemaining == 0
{
if keyboard_check(vk_left) && place_free (x - 16, y)
    {
    XRemaining = 16;
    PlayerDirection = 4;
    }
else if keyboard_check(vk_right) && place_free (x + 16, y)
    {
    // etc... using if...else for up and down
    }
}
else
{
switch (PlayerDirection)
    {
    case 4:
        x -= XSpeedInc;
        XRemaining -= XSpeedInc;
        break;
    // other directions here...
    }
// If the speed value is going to be anything other than a perfect integer then maybe have a check in here too
if XRemaining <= 0
    {
    Xremaining = 0;
    }
// And the same for Y
}
 
L

LWCreations

Guest
Thanks for helping, it worked great, not sure how it works but it looks like I got a lot to learn... :)
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Thanks for helping, it worked great, not sure how it works but it looks like I got a lot to learn... :)
Happy to help! And if I may, I'll just quickly explain what the code is doing... basically, it's using the "XRemaining" value as a control to prevent input while the instance is moving. So, if the variable is 0, it'll permit keyboard input, which then sets the variable to a value greater than 0. Now input is impossible and so the instance can move, which is the "else" part of the code. In this section of the code we use the direction variable to tell the instance HOW to move, using a "switch" so only the value used in the direction variable will be processed. In each case of the switch we simply subtract the speed vallue from the XRemaining variable and add (or subtract) it from the position of the instance. This will repeat until the the XRemaining value is 0 and the instance can accept input again.

Hope that helps!
 
Top