1

I'm making a game in GameMaker Studio 2, and I have a problem. When object turns to left or right, he was puching forward. But he has to be in the same position like the first time.

I tried to use this code:

/// @description vaksciojimas
// You can write your code in this editor

key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);

var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,obj_wall)) && (key_jump) {
    vsp = -8;
}

//horizontaliai susiduria
if (place_meeting(x+hsp,y,obj_wall)) {
    while (!place_meeting(x+sign(hsp),y,obj_wall)) {
        x = x + sign(hsp);
    }
    hsp = 0;
}

x = x + hsp;

//vertikaliai susiduria
if (place_meeting(x,y+vsp,obj_wall)) {
    while (!place_meeting(x,y+sign(vsp),obj_wall)) {
        y = y + sign(vsp);
    }
    vsp = 0;
}

y = y + vsp;
//animacijos
if (!place_meeting(x,y+1,obj_wall)) {
    sprite_index = sprite_jumping_player;
    image_speed = 0;
    if (sign(vsp) > 0){
        image_index = 1;
    }
    else {
        image_index = 0;
    }
}
else {
    image_speed = 1;
    if (hsp == 0) {
        sprite_index = sprite_player;
    }
    else {
        sprite_index = sprite_runing_player;
    }
}
if (hsp != 0) { // hsp = horizontal speed
    image_xscale = sign(hsp);
}

When I do this, I was wathing this tutorial: https://www.youtube.com/watch?v=fCeyiEcWRAs&t=8s

Rob
  • 4,927
  • 4
  • 26
  • 41
  • Can you explain 'pushing forward'? It's moving while turning around? I think that has to do with your movement system rather than the image_xscale. Unless you mean that it's not turning around it's origin point well, which you need to set in the center of the sprite. – Steven Apr 10 '19 at 06:32
  • if you are having a problem with the position, I don't think it has anything to do with the image_xscale cause all it does is turn the player around to look the other way. please explain what is exactly your problem and show us more of your code in the step even – YOUSFI Mohamed Walid Apr 10 '19 at 13:48
  • So, my sprite is moving while he turns. When it turns, it looks to the other way(this is ok) ,but it is moving to another position (few centimeters forward, this is not ok). More code I added to the question. – Lukas Surblys Apr 11 '19 at 05:26

1 Answers1

2

From the looks of it, I think this has to do with the origin of the sprite.

With the origin, you're deciding where the center of the sprite is. At which point it should rotate/turn around ect.

You need to set the origin in the sprite itself (not the sprite editor, just the sprite image), because by default it's set on top-left. Setting it on middle-center is the usual method. The origin point appears as an "+" symbol.

Steven
  • 1,996
  • 3
  • 22
  • 33