Transparency Not Working Correctly With Trigger

I'm wanting to create a transparency effect when the player walks under a tree, so that the alpha fades to 0, then after the player leaves, the alpha goes back to 1.

This is the code I've been trying out, but it only works without the else if statement.

GML:
if place_meeting(x, y, o_trigger) {
    if instance_exists(tree_patch) {
        o_tree_patch.image_alpha -= 0.05;
    }
}
else if !place_meeting(x, y, o_trigger) {
    if instance_exists(tree_patch) {
        o_tree_patch.image_alpha += 0.1;
    }
}
What's a better way of doing this?
 
Last edited:

FoxyOfJungle

Kazan Games
My method:

Player collision with tree (faster than the tree collide with the player!):

GML:
var col = instance_place(x, y, obj_tree);
if (col != noone) {
    col.fade();
}

obj_tree Create Event:
GML:
fade_timer = 0;
fade = function() {
    image_alpha = max(image_alpha-0.08, 0.5);
    fade_timer = 1;
}

Step Event:
GML:
fade_timer -= 0.1;
if (fade_timer <= 0) {
    image_alpha = min(image_alpha+0.08, 1);
    fade_timer = 0;
}
0.5 is the min alpha when colliding.
0.08 is the fade speed.

Instead of creating a method for each tree, you can create a normal function (in scripts), and then call it in the same way.


Result:



Another thing, you need to have a clamp(), otherwise the values will pass from 1 and 0. That's what I did when using max and min.
You can now adapt my code with your trigger.
 
Last edited:
My method:

Player collision with tree (faster than the tree collide with the player!):

GML:
var col = instance_place(x, y, obj_tree);
if (col != noone) {
    col.fade();
}

obj_tree Create Event:
GML:
fade_timer = 0;
fade = function() {
    image_alpha = max(image_alpha-0.08, 0.5);
    fade_timer = 1;
}

Step Event:
GML:
fade_timer -= 0.1;
if (fade_timer <= 0) {
    image_alpha = min(image_alpha+0.08, 1);
}
0.5 is the min alpha when colliding.
0.08 is the fade speed.

Instead of creating a method for each tree, you can create a normal function (in scripts), and then call it in the same way.


Result:



Another thing, you need to have a clamp(), otherwise the values will pass from 1 and 0. That's what I did when using max and min.
You can now adapt my code with your trigger.
Thank you for the reply, but how I'm using the trigger, is when the player steps on a single trigger object, it runs code for the tree objects to fade. Since there are more than one, of the tree objects, I don't think your code will work.
 
Thank you for the reply, but how I'm using the trigger, is when the player steps on a single trigger object, it runs code for the tree objects to fade. Since there are more than one, of the tree objects, I don't think your code will work.
Sounds like you need to get the instance id of the tree near that trigger and only run the alpha code on that singular instance.
 
Or if what you want is for multiple trees to fade instead of just a single one, then use the collision_*_list() functions and loop through the list running the fade() method for each instance in the collision list.
 

CMAllen

Member
The tree object should be doing the checking, not the player. And you don't need to be doing collision tests for that, because it's overkill. A simple point_in_rectangle() function is more than enough, checking the player's position against the bounding box of the tree's sprite. This simply tests to see if the player object's origin point is inside bounding box of the tree's sprite. Very simple, extremely lightweight. When the function returns true, the player object is in range for the tree to fade, which is when the tree object can adjust its alpha to whatever minimum amount is acceptable for the effect you're trying to achieve.
 
The tree object should be doing the checking, not the player. And you don't need to be doing collision tests for that, because it's overkill. A simple point_in_rectangle() function is more than enough, checking the player's position against the bounding box of the tree's sprite. This simply tests to see if the player object's origin point is inside bounding box of the tree's sprite. Very simple, extremely lightweight. When the function returns true, the player object is in range for the tree to fade, which is when the tree object can adjust its alpha to whatever minimum amount is acceptable for the effect you're trying to achieve.
That sounds like it could work, but I've started using a tileset layer "Tree_Tops", for the part I would like fade or turned off.

I don't think the point_in_rectangle() would work, because the tree tops are more like paths of tress that go in angles like L shapes and not just squares. But, that's almost like what I'm already doing with the layer. But since I have multiple objects, it doesn't seem to work because each trigger object is checking if the player is colliding with it and if not don't go transparent. Since all objects are checking and the player is only colliding with one, the others are returning false and not triggering to turn off the visibility of the Tree_Tops layer.
 

CMAllen

Member
That sounds like it could work, but I've started using a tileset layer "Tree_Tops", for the part I would like fade or turned off.

I don't think the point_in_rectangle() would work, because the tree tops are more like paths of tress that go in angles like L shapes and not just squares. But, that's almost like what I'm already doing with the layer. But since I have multiple objects, it doesn't seem to work because each trigger object is checking if the player is colliding with it and if not don't go transparent. Since all objects are checking and the player is only colliding with one, the others are returning false and not triggering to turn off the visibility of the Tree_Tops layer.
In that case, yes, definitely a different approach required.
 
Top