• 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!

Scrolling Backgrounds

P

Perullo

Guest
I'm working on adding parallax backgrounds to my game and am struggling with GMS2's new syntax. Back in 1.4 a developer could scroll background images using the background_x and background_y variables, which I understand are now obsolete and have been removed from GMS2. What would be the equivalent way to scroll an image in a background layer in GMS2?
 
P

Perullo

Guest
Look at the layer_x() and the layer_hspeed() functions.
Thanks, that does help. That said, is there also a way to get a hold of whatever the sprite asset is that a layer is drawing as a background? I want to be able to get some info from it, mainly the width and height of the sprite.

Just to provide some context, I'm working on taking the info from this video and translating them into GMS2:
 

Perseus

Not Medusa
Forum Staff
Moderator
I just came across a problem with this function. Do I have to assign the sprite to the layer with code? The background is set on the room editor but the function is returning -1.
Nope, it should work the same way for backgrounds placed using the room editor, unless there's a bug.

How do you get the background element id? Judging from what you just said, it appears that you're using id of the layer, while you need element id of the background placed on the layer for this function.
 
M

maratae

Guest
Nope, it should work the same way for backgrounds placed using the room editor, unless there's a bug.

How do you get the background element id? Judging from what you just said, it appears that you're using id of the layer, while you need element id of the background placed on the layer for this function.
So I made this function to lerp backgrounds, in which I pass the layer name:
Code:
scr_bg_parallax_lerp("BG00");
This is what I'm doing inside the function, which I set in a rather dumb way to try and pinpoint the problem:
Code:
var bg = argument[0]; //The background name
var bgId = layer_get_id(bg); //Get the layer id from the name
var bgSprite = layer_background_get_sprite(bgId); //get the sprite assigned to that layer id
The problem is in this last line, in which bgSprite returns -1.

You're saying I should use layer_background_get_id() instead?

EDIT: Now the number are still wrong (returning -1) but the background is moving as expected..? o_o'
 
Last edited by a moderator:
M

maratae

Guest
So I made this function to lerp backgrounds, in which I pass the layer name:
Code:
scr_bg_parallax_lerp("BG00");
This is what I'm doing inside the function, which I set in a rather dumb way to try and pinpoint the problem:
Code:
var bg = argument[0]; //The background name
var bgId = layer_get_id(bg); //Get the layer id from the name
var bgSprite = layer_background_get_sprite(bgId); //get the sprite assigned to that layer id
The problem is in this last line, in which bgSprite returns -1.

You're saying I should use layer_background_get_id() instead?

EDIT: Now the number are still wrong (returning -1) but the background is moving as expected..? o_o'
Nevermind. I was testing individual backgrounds, and forgot the function was working 4 times, one for each background. The last one had no image assigned and so it returned -1.
Idiot me.
 
Top