Lukas Surblys
Lukas Surblys

Reputation: 13

How to fix a bug with 'image_xscale' in GameMaker Studio 2?

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

Upvotes: 1

Views: 2142

Answers (1)

Steven
Steven

Reputation: 2122

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.

Upvotes: 2

Related Questions