How do i change a sprite alpha when players sprite is behind it.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tamanor

Hi,

Not sure if this is the correct place, But I have just started to use Godot today mostly just trying things out at the moment to get the hang of things,
But I was wondering if anyone could point me in the correct direction, I want trying to make it so if the player sprite walks behind another sprite (this sprite being a tree) so that the alpha transparency of the sprite (tree) is set to a new value so that you can see the player behind the tree, Basically how games like Stardew Valley handle it.

Thanks in advanced.

:bust_in_silhouette: Reply From: A112

Slap an Area2D on that tree and use it’s body_entered() and body_exited() signals to control opacity

Hi

Thanks for your answer,
I will try this out,
But currently I have all my trees on a seperate TileMap so im guessing and correct me if im wrong, But if i put the Area2D on this to control the opacity, Am I right in thinking that if I went to one of the trees this would make all trees on the map have the same opacity or just the one where the player is at?
And if that is the case would it be best to have all trees seperate as a staticbody? or if there some other better method

Thanks

tamanor | 2020-05-03 15:34

I tried the body_entered signal,

At first i tested it and made it return a print in the output which worked when the player entered the body of the tree,
But then i tried with adding

func _on_Area2D_body_entered(body):
if body.get_name() == “Player”:
modulate.a = 0.5

which currently does not seem to be doing anything with the TileMap I can only guess that it only effecting the Area2D.

Is there anyway to make this work with Tilemaps or does all my trees have to be sererate Area2D which kinda sounds like it would make the game have lots of uneeded things.

thanks

tamanor | 2020-05-03 17:52

try selecting area2d parent node containing the sprite and then change its opacity

A112 | 2020-05-05 22:09

Hey cheers,
I will take a look and see if i can get it to work with tilemap again,
But currently i managed to get it working VIA a StaticBody2D not sure if this is an optimal way since each tree is it own thing, But for testing purposes it working.

Off topic quests but you may or may not know and I thought it would save me opening a new question.

I currently have a 2d Player that has animations for W, A, S, D which is all working fine, But im having a small issue with the diagonal movement. I want to make it so if you move in a diagonal direction it forces the use of the Left(A) and Right(D) animations,
Because currently if i press W and then A it will use the walking up animation when going diagonal left.

Thanks

tamanor | 2020-05-07 16:17