• 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!

setting transparency when walking behind object

M

milch

Guest
I want my tree sprite to turn transparent when the player walks behind it, but with place_meeting it checks for the collision area, not for the actual sprite area...

if place_meeting(x,y,character){
image_alpha = 0.5;
}else{
image_alpha = 1;
}

so right now it only turns the tree transparent when the player walks against it...
could someone tell me if there's a way to check if an object is meeting with a sprite? thanks!!
 

CloseRange

Member
the best way to do this is just change the trees collision mask. Go to the sprite: click modify mask: and change the shape to Precise.
That will make it so place_meeting is based on the pixels.

If for some reason you have to have the mask how it is then duplicate the tree sprite and just code up a way to link the real tree with the modified tree. If you need help with that code just ask!

hope this helped! :confused:
-CloseRange
 
M

milch

Guest
ahh thanks so much, this works perfectly!! :D

so in case someone else wants to do this:

I set the collision mask of the first tree object to pixels and took off "solid", put this in the step event:

if place_meeting(x,y,character) and character.y - 20 < y {
image_alpha = 0.5;
}else{
image_alpha = 1;
}

instance_create_depth(x,y,-y, obj_tree1);

the second tree object (obj_tree1) has the rectangle collision mask and is set to "solid". so now the player will collide with the roots, but can walk behind the foliage and when he does so, it will turn the sprite transparent.
"and character.y - 20 < y" <---- this is so that if the player walks against the roots from the bottom is doesn't turn transparent and it only does if he collides with it above 20 pixels.
 

CMAllen

Member
ahh thanks so much, this works perfectly!! :D

so in case someone else wants to do this:

I set the collision mask of the first tree object to pixels and took off "solid", put this in the step event:

if place_meeting(x,y,character) and character.y - 20 < y {
image_alpha = 0.5;
}else{
image_alpha = 1;
}

instance_create_depth(x,y,-y, obj_tree1);

the second tree object (obj_tree1) has the rectangle collision mask and is set to "solid". so now the player will collide with the roots, but can walk behind the foliage and when he does so, it will turn the sprite transparent.
"and character.y - 20 < y" <---- this is so that if the player walks against the roots from the bottom is doesn't turn transparent and it only does if he collides with it above 20 pixels.
Alternatively, since you're using objects for trees, it can look for the player's x/y coordinates within a rectangle that's based on the tree's x/y coordinates and its sprite_width, sprite_height, and origin. Essentially, it's looking for if the player's location falls within the sprite's bounding rectangle. You could refine that further by cropping the bounding rectangle to only use the space above the sprite's origin point (since if the player's y location is higher than that, you'd want the player to appear in front of the tree instead of behind it).
 
Top