• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED does anyone knows how to make a functional topdown 2d movement script?????

kureoni

Member
GML:
var move_left = keyboard_check(ord( "A"));

var move_right = keyboard_check(ord("D"));

var move_down = keyboard_check(ord("S"));

var move_up = keyboard_check(ord("W"));



if (move_left && place_free(x-spd,y)){

    x-=spd;

}

if (move_right && place_free(x+spd,y)){

    x+=spd;

}

if (move_down && place_free(x, y+spd)){

    y+=spd;

}

if (move_up && place_free(x, y-spd)){

    y-=spd;

}







if(y > mouse_y){

sprite_index = spr_player_back

}



if(y < mouse_y){

sprite_index = spr_player_front

}





if(y != yprevious && y > mouse_y){

sprite_index = spr_player_walkback

}



if(y != yprevious && y < mouse_y){

sprite_index = spr_player_walkfront

}



if(y < mouse_y && x != xprevious){

    sprite_index = spr_player_walkfront

}



if(y > mouse_y && x != xprevious){

    sprite_index = spr_player_walkback

}



if(x < mouse_x && x = xprevious){

    image_xscale = 3

    sprite_index = spr_player

}



if(x > mouse_x && x = xprevious){

    image_xscale = -3

    sprite_index = spr_player

}



if(x < mouse_x && x != xprevious){

    image_xscale = 3

    sprite_index = spr_player_walk

}



if(x > mouse_x && x != xprevious){

    image_xscale = -3

    sprite_index = spr_player_walk
i have this one but it just wont change the sprites acording to the mouse position
(it works on x but i does not on y, or it works on y but not on x)
 

chamaeleon

Member
i have this one but it just wont change the sprites acording to the mouse position
(it works on x but i does not on y, or it works on y but not on x)
You have a ton of conditions that should be protected using "else if" instead of plain if, and some of them are identical to boot. It's up to you to figure out the one and only case that should be applied instead of a sequence of potential tests that can return true, each one overriding the previous sprite change.
 

LunaticEdit

Member
I disagree with chameleon. There's nothing wrong with that direction test.. Though I test up/down and left/right. Without that, pushing up+down will result in not moving, which you may or may not want (not up to me to decide, it's your game!).

I do this:
GML:
var did_move = false;

if (input_move_up()) {
    if (!check_collision_range(x - PLAYER_COLLISION_W, y - PLAYER_COLLISION_H - 1, PLAYER_COLLISION_W * 2, PLAYER_COLLISION_H)) {
        y -= 1;
    }
    mob_direction = e_direction.up;
    sprite_index = sprHeroWalkUp;
    image_xscale = 1.0;
    image_speed = 1;
    did_move = true;
} else if (input_move_down()) {
    if (!check_collision_range(x - PLAYER_COLLISION_W, y + 1, PLAYER_COLLISION_W * 2, PLAYER_COLLISION_H)) {
        y += 1;
    }
    mob_direction = e_direction.down;
    sprite_index = sprHeroWalkDown;
    image_xscale = 1.0;
    image_speed = 1;
    did_move = true;
}
  
if (input_move_left()) {
    if (!check_collision_range(x - PLAYER_COLLISION_W - 1, y - PLAYER_COLLISION_H, PLAYER_COLLISION_W, PLAYER_COLLISION_H * 2)) {
        x -= 1;
    }
    mob_direction = e_direction.left;
    sprite_index = sprHeroWalkRight;
    image_xscale = -1.0;
    image_speed = 1;
    did_move = true;
} else if (input_move_right()) {
    if (!check_collision_range(x + 1, y - PLAYER_COLLISION_H, PLAYER_COLLISION_W, PLAYER_COLLISION_H * 2)) {
        x += 1;
    }
    mob_direction = e_direction.right;
    sprite_index = sprHeroWalkRight;
    image_xscale = 1.0;
    image_speed = 1;
    did_move = true;
}

if (!did_move) {
    image_index = 1;
}
The only issue I see with your code is your assuming the mouse position is relative to the camera's viewport. Remember what you see on the screen is NOT the actual position, but a combination of the position and the camera's viewport.

If you're getting relative mouse position you need to do something like this (PSUDO CODE, I didn't test or run it):
GML:
mouse_actual_x = view_xview[0] + device_mouse_get_raw_x(0) * (view_wview[0]/window_get_width());
mouse_actual_y = view_yview[0] + device_mouse_get_raw_y(0) * (view_hview[0]/window_get_height());
You may want to read up on the camera documentation to understand what's going on.
 

kureoni

Member
thanks for the help every1
there may be something else wrong
but that is fine, i gave up on the mouse position thingy etc
maybe later ill find that out
 
Top