GameMaker (Solved) How to set background sprites by code?

K

Karlabos

Guest
How does one add a sprite to a background via code?

I tried the following:
globalvar town_background;
town_background = layer_background_get_id("Background");
//I assume Background and Instances are default layers belonging to every room? At least they appear in the room editor
layer_background_sprite(town_background,back_Neboohoo); //the latter is a sprite available in the ide

However when I test, the room is just the color of the Background layer, with no sprite at all...
 

TsukaYuriko

☄️
Forum Staff
Moderator
While it is not the source of your problem today, it may very well be the source of your problem anytime in the future: globalvar is deprecated and you should not be using it. For further information, check out the manual entry on variables and scope.

That aside, take a look at the manual entry of layer_background_get_id. It shows the correct usage of the function. In your code, you are calling the function and supplying a string as the argument, but it expects an ID: The ID of the layer, which you have to obtain first.
 
K

Karlabos

Guest
That aside, take a look at the manual entry of layer_background_get_id. It shows the correct usage of the function. In your code, you are calling the function and supplying a string as the argument, but it expects an ID: The ID of the layer, which you have to obtain first.
I have tried proceeding as in the example:

idtoget = layer_get_id("Background");
town_background = layer_background_get_id(idtoget);
layer_background_sprite(town_background,back_Neboohoo);

Still... No sprite appears. Only the color set for the Background layer.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Still... No sprite appears. Only the color set for the Background layer.
Is that color black?

When you define a sprite for a background layer, the "color" will be used to blend the sprite and the background layer will no longer be filled with the defined color. If you don't define any other background color anywhere else, the initial fill of the back buffer will default to black, and you will be drawing a black-blended sprite onto a black background.
 
K

Karlabos

Guest
Is that color black?

When you define a sprite for a background layer, the "color" will be used to blend the sprite and the background layer will no longer be filled with the defined color. If you don't define any other background color anywhere else, the initial fill of the back buffer will default to black, and you will be drawing a black-blended sprite onto a black background.
Oh i see. Thanks
 
Top