• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

I need help with my Background Layers alpha values

This error is driving me crazy. Any help is greatly appreciated.

I got this room with three background layers.

"Background_floor" at depth 300 with a picture of a floor as background.
"Backgrounds_Lava_top" at depth 400 with a yellow color background.
"Backgrounds_Lava_bottom" at depth 500 with a red color background.

My idea was to increase and decrease the alpha of the Backgrounds_Lava_top layer to make it looks like there is some glowing lava/magma underneath the walkway where the player is walking.

So I have an object in the room that does this in every step:

GML:
var layer_id = layer_get_id("Backgrounds_Lava_top");
var layer_alpha = layer_background_get_alpha(layer_id);
layer_alpha += alpha_change_speed;
layer_background_alpha(layer_id,layer_alpha);
if(alpha_change_speed < 0 and layer_alpha <= 0){
    alpha_change_speed = 0.01;
}else if(alpha_change_speed > 0 and layer_alpha >= 0.8){
    alpha_change_speed = -0.01;
}
The result?

shootergif.gif

For some reason the yellow layer gets on top of the floor!

I have been trying to figure this out with the debugger and the only thing I have seen is that when the game starts the "Background_floor" layer starts with an alpha of zero. But here is the thing as you can see in the GIF: The alpha can't be zero because the floor is visible and it is above the yellow layer.

I can't recall or find any other part of the code where I mess with the background layers, so I have no idea what might be happ

Anybody here has encountered a similar issue?

EDIT: I have tried changing the alpha of the Background_floor layer to 1 at the beginning of the room and it didn't solve the problem.
 
Last edited by a moderator:

Jaan

Member
To get a background ID, I've always needed another step. Changes to your code in bold below:

var layer_id = layer_get_id("Backgrounds_Lava_top");
var back_id= layer_background_get_id(layer_id);
var layer_alpha = layer_background_get_alpha(back_id);
layer_alpha += alpha_change_speed;
layer_background_alpha(lback_id,layer_alpha);

I have just implemented fading seven background layers into seven new backgrounds. An excerpt of the background management code is below. In this case the sprites of the next seven layers have been specifically named as sBG0Layer0 through to sBG0Layer6. Then sBG1Layer0 and so on for 8 background sets. The background layers have been named BGLayer0, BGLayer0a, BGLayer1, BGLayer1a etcetera. All told there are 14 background layers and 56 background sprites. The code below will load the next set of backgrounds to an array, apply the sprites to the next backgrounds to fade in, fade the old backgrounds out and the new ones in.

Feel free to use whatever part of the code is useful :D

Code:
BGChangeTimer--;

//Change backgrounds
if (BGChangeTimer <= 0){
    //Load next background set
    for(var _i = 0;_i < numLayers; _i++){
        var _str = "sBG" + string(changeNo) + "Layer" + string(_i)
        BGSet[_i] = asset_get_index(_str);
    }
    
    //Apply next background set
    var _layTxt = "BGLayer";
    
    for(var _i = 0; _i < numLayers; _i++){
        if (changeNo mod 2 == 0){
            var _layID = layer_get_id(_layTxt + string(_i));
        } else{
            var _layID = layer_get_id(_layTxt + string(_i) + "a");
        }
        var _backID = layer_background_get_id(_layID);
        layer_background_sprite(_backID, BGSet[_i]);;
    }
    
    change = true;
}


//Fade backgrounds
if (change){
    var _layTxt = "BGLayer";
    for(var _i = 0; _i < numLayers; _i++){
        if (changeNo mod 2 == 0){
            downAlpha -= alphaChange;
            upAlpha += alphaChange;
            var _layID = layer_get_id(_layTxt + string(_i));
            var _backID = layer_background_get_id(_layID);
            layer_background_alpha(_backID, upAlpha);
            _layID = layer_get_id(_layTxt + string(_i) + "a");
            _backID = layer_background_get_id(_layID);
            layer_background_alpha(_backID, downAlpha);
        } else{
            upAlpha += alphaChange;
            downAlpha -= alphaChange;
            var _layID = layer_get_id(_layTxt + string(_i));
            var _backID = layer_background_get_id(_layID);
            layer_background_alpha(_backID, downAlpha);
            _layID = layer_get_id(_layTxt + string(_i) + "a");
            _backID = layer_background_get_id(_layID);
            layer_background_alpha(_backID, upAlpha);
        }
    }

    //Reset for next change
    if (upAlpha >= 1){
        changeNo++;
        upAlpha = 0;
        downAlpha = 1;
        change = false;
        if (changeNo >= numBacks) changeNo = 0;
        BGChangeTimer = BGChangeTimerInit;
    }
}
 
To get a background ID, I've always needed another step. Changes to your code in bold below:

var layer_id = layer_get_id("Backgrounds_Lava_top");
var back_id= layer_background_get_id(layer_id);
var layer_alpha = layer_background_get_alpha(back_id);
layer_alpha += alpha_change_speed;
layer_background_alpha(lback_id,layer_alpha);
Thank you, that solved it!
 
Top