• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2 [solved] layer_sprite_x() usage?



IDE v2.2.0.343 Runtime v2.2.0.261
Windows 10 VM output

I am trying to get a background to move in the current room, the room has a single instance layer with an object.

According to the manual http://docs2.yoyogames.com/source/_...l_reference/rooms/sprites/layer_sprite_x.html I have the following code in my object:

// create
backgroundLayer = layer_create(100, "Background");
backgroundSprite = choose(sprBgA, sprBgB, sprBgC);
backgroundElement = layer_background_create(backgroundLayer, backgroundSprite);

// step or draw
var bgX   = layer_sprite_get_x(backgroundElement);
layer_sprite_x(backgroundElement, bgX + 1);
show_debug_message("bgX = " + string(layer_sprite_get_x(backgroundElement)));
I've tried the code in both the step and the draw events, the background does not move and the debug message continually prints out "bgX = 0".

Am I doing something incorrectly or does this function have a bug?


By what I recall, you need to manipulate layer position instead using layer_x, or set a constant speed with layer_hspeed. Your approach probably doesn't work because you're trying to use a sprite layer command on a background layer resource. (And background layer sprites have no positioning setters, as can be seen from layer_background_* commands)


It seems that though they are all sprite images, GMS makes a very definite separation between "sprites as sprites" and "sprites as backgrounds" to the point where "sprite layer sprite" commands will not work with "background layer sprite" resources, and vice versa. Well I suppose if there was no separation, they wouldn't have created different command sets.

But it looks like nothing stops you from drawing the background image to a sprite layer as a regular sprite and use layer_sprite_x and layer_sprite_y to control position. Maybe there's some internal optimization regarding backgrounds, since they bothered to create a different command for that. Or they're trying to be newbie friendly by creating commands that are named differently but do the same job.