• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Creating a Background Layer Draws Duplicate Sprite

I was testing out dynamically creating background layers today, and I noticed a strange behavior. I don't know whether it's an error in my code, or a bug with GMS2. What happens is that the game draws a duplicate sprite. One of the sprites is properly centered around the layer x and y, and the other has the top left corner of the sprite on that point, like this.


Here is my current script for creating a new background layer.
Code:
/// @description Create a new background layer.

/// @param   {int}  arrayVal     The array value of the background layer.
/// @param   {real} layerDepth   The depth of the background layer.

/// @param   {sprite} bgSprite   The sprite to use for the background.



var a = argument0;
var _lDepth = argument1;

//Create the layer.
bgLayerIndex  [a] = layer_create( _lDepth, "BackgroundLayer" + string(a) );
bgLayerElement[a] = layer_background_create( bgLayerIndex[a], argument2 );


var _lIndex   = bgLayerIndex[a];
var _lElement = bgLayerElement[a];

layer_x(_lIndex, 400);
layer_y(_lIndex, 200);


show_debug_message( "CREATING BACKGROUND LAYER " + string(a) );
And here's how I'm creating the layer in the game. Note that there should be only one being created.
Code:
for ( var i = 0; i < 1; i++; )
{
    BackgroundLayerCreate(i, -5000 - i, SprPlayerGrazebox);
}
Did I make a mistake somewhere, or is this a legit bug?
 

chance

predictably random
Forum Staff
Moderator
When you see odd behaviour like this, I'd recommend doing a test with everything stripped away except the essential features -- namely, the layer creation and the sprite addition to that layer. In this case, just two lines are needed:

Code:
new_layer = layer_create(10000);
bk_layer = layer_background_create(new_layer, sprite_name);

And that does reveal a bug, apparently. When I run this code, I see two copies of the sprite I've added as background, just as you described. And if I shift the background layer (with layer_x() ), both sprites move together. So they're both created in the new background layer.

Unless someone points out an error we're making, you should file a bug report.

EDIT: I experimented a bit with this. The offset between the two sprites is equal to the sprite origin offset. So if the sprite origin is top left (i.e. 0,0), then both sprites are drawn identically over each other. So the bug isn't noticeable. (Probably why it was missed in the first place.)

However, if the sprite origin is set to (say) middle-center, then the second sprite is drawn offset by that amount. So it's clearly visible.
 
Last edited:
Yeah, I posted all the code I was using just to make sure it wasn't a mistake I was making somewhere. I tried another project using just those two lines, and the same thing was happening. I filed a bug report a few minutes ago. I really wanted to use dynamic background layers, but I guess I'll hold off on that.
 
Top