Better moving

I'm currently making a game on Game Maker Pro. I've only just started and I'm still doing the moving code for the character.I'm using tiles to add graphics to my game. I use invisible object to control where the player can and can't go. I'll put up code, screenshots etc. if you need it. This is my moving code:(in the step event)

Code:
//Shortcuts for key listeners
moveLeft = keyboard_check(vk_left);
moveRight = keyboard_check(vk_right);
moveUp = keyboard_check(vk_up);
moveDown = keyboard_check(vk_down);
space = keyboard_check(vk_space);

image_speed = 0.3;

//player moves up when up is pressed

if (moveUp && !moveRight && !moveDown && !moveLeft && y >= 0 + sprite_height/2 && up)
{   
    y -= playerSpeed;
    sprite_index = spr_farmerWalkUp;
}

//Move down
else if (!moveUp && !moveRight && moveDown && !moveLeft && y <= room_height - sprite_height/2 && down)
{
    
    y += playerSpeed;
    sprite_index = spr_farmerWalkDown;
}

//Move Right
else if (!moveUp && moveRight && !moveDown && !moveLeft && x <= room_width - sprite_width/2 && right)
{   
    x += playerSpeed;
    sprite_index = spr_farmerWalkRight;
}

//Move left
else if (!moveUp && !moveRight && !moveDown && moveLeft && x >= 0 + sprite_width/2 && left)
{   
    x -= playerSpeed;
    sprite_index = spr_farmerWalkLeft;
}

//Closes the gap on the right
else if (!moveUp && moveRight && !moveDown && !moveLeft && x < room_width - sprite_width/2 && !right)
{
    while (!place_meeting( x + 1, y, obj_stopBlock) )
    {       
        x += 1;
    }
}

//Closes the gap on the left
else if (!moveUp && !moveRight && !moveDown && moveLeft &&
    x >= 0 + sprite_width/2 && !left)
{
    while (!place_meeting( x - 1, y, obj_stopBlock) )
    { 
        x -= 1;
    }
}

//Closes the gap on the up
else if (moveUp && !moveRight && !moveDown && !moveLeft &&
    y >= 0 + sprite_height/2 && !up)
{
    while (!place_meeting( x, y - 1, obj_stopBlock) )
    {       
        y -= 1;
    }
}

//Closes the gap on the down
else if (!moveUp && !moveRight && moveDown && !moveLeft &&
    y <= room_height - sprite_height/2 && !down)
{
    while (!place_meeting( x, y + 1, obj_stopBlock) )
    {       
        y += 1;
    }
}

//Idle
if(playerSpeed <= 0 && image_index == spr_farmerWalkUp)
{
  image_index = spr_farmerIdleUp;
}

else if(playerSpeed <= 0 && image_index == spr_farmerWalkLeft)
{
  image_index = spr_farmerIdleLeft;
}

else if(playerSpeed <= 0 && image_index == spr_farmerWalkDown)
{
  image_index = spr_farmerIdleDown;
}

else if(playerSpeed <= 0 && image_index == spr_farmerWalkRight)
{
  image_index = spr_farmerIdleRight;
}
 

NicoFIDI

Member
theres a clean way to improve the code lenght

ON CREATE
--------------------------------------------------------------------------------
walkSprites[0] = spr_farmerWalkLeft;
walkSprites[1] = spr_farmerWalkRight;
walkSprites[2] = spr_farmerWalkUp;
walkSprites[3] = spr_farmerWalkDown;

IdleSprites[0] = spr_farmerIdleLeft;
IdleSprites[1] = spr_farmerIdleRight;
IdleSprites[2] = spr_farmerIdleUp;
IdleSprites[3] = spr_farmerIdleDown;

face_direction = 0;
--------------------------------------------------------------------------------

ON STEP
--------------------------------------------------------------------------------
//Shortcuts for key listeners
var moveLeft = keyboard_check(vk_left);
var moveRight = keyboard_check(vk_right);
var moveUp = keyboard_check(vk_up);
var moveDown = keyboard_check(vk_down);
var space = keyboard_check(vk_space);

var moveX = 0;
var moveY = 0;

// Get actual move by step
if (moveLeft) moveX -= 1;
if (moveRight) moveX += 1;
if (moveUp) moveY -= 1;
if (moveDown) moveY += 1;

// Get facing direction
if (moveX != 0) face_direction = (1 + moveX) / 2;
if (moveY != 0) face_direction = 1 + (1+moveY) / 2;

// Move player
x += moveX*playerSpeed;
y += moveY*playerSpeed;

// Select correct sprite
if (moveX == 0 && moveY == 0)
sprite_index = idleSprites[face_direction];
else
sprite_index = walkSprites[face_direction];

// Close the gap
// [too lazy to write it] have fun writing it yourself(?)
--------------------------------------------------------------------------------

:p
 
Last edited:
theres a clean way to improve the code lenght

ON CREATE
--------------------------------------------------------------------------------
walkSprites[0] = spr_farmerWalkLeft;
walkSprites[1] = spr_farmerWalkRight;
walkSprites[2] = spr_farmerWalkUp;
walkSprites[3] = spr_farmerWalkDown;

IdleSprites[0] = spr_farmerIdleLeft;
IdleSprites[1] = spr_farmerIdleRight;
IdleSprites[2] = spr_farmerIdleUp;
IdleSprites[3] = spr_farmerIdleDown;

face_direction = 0;
--------------------------------------------------------------------------------

ON STEP
--------------------------------------------------------------------------------
//Shortcuts for key listeners
var moveLeft = keyboard_check(vk_left);
var moveRight = keyboard_check(vk_right);
var moveUp = keyboard_check(vk_up);
var moveDown = keyboard_check(vk_down);
var space = keyboard_check(vk_space);

var moveX = 0;
var moveY = 0;

// Get actual move by step
if (moveLeft) moveX -= 1;
if (moveRight) moveX += 1;
if (moveUp) moveY -= 1;
if (moveDown) moveY += 1;

// Get facing direction
if (moveX != 0) face_direction = (1 + moveX) / 2;
if (moveY != 0) face_direction = 1 + (1+moveY) / 2;

// Move player
x += moveX*playerSpeed;
y += moveY*playerSpeed;

// Select correct sprite
if (moveX == 0 && moveY == 0)
sprite_index = idleSprites[face_direction];
else
sprite_index = walkSprites[face_direction];

// Close the gap
// [too lazy to write it] have fun writing it yourself(?)
--------------------------------------------------------------------------------

:p
Thanks Nicofidi for your help! I'm still looking for ideas to improve the code.
 
Top