Make secret blocks transparent when discovered [Solved]


(Lozza JP) #1

Hey all, using OGMO to create levels and use a set of tiles. Create the ground grid as usual to walk on. I have a second set of tiles that I can place on and I don’t place a ground so they are non collidable and have it’s layer set lower than the player, so he appears to go behind this secret wall.

So the hidden map tiles are rendered as one Entity during world creation, so far I have managed to detect the player colliding with hidden map as an entity, but can’t get the behaviour to make it invisible.

I can’t even make them disappear, I have got it working that it is placed into a variable hiddenMap and nothing like this works:

hiddenMap.visible = false; hiddenMap.graphic.visible = false;

I can however do: hiddenMap.layer = 10; // and then it is behind me again (not what I want though)

Anyone got suggestions or can explain why the visible thing is not working? Am I missing something how this all works? Thanks

Update: I have debug controls, when I normally try to pass through the wall nothing happens, If I happen to pass through using debug it does turn visible to false!! The only difference is debug makes my x or y +=/-= for faster movement. I am baffled as to what is happening.

When I find out how to collide properly, can I do something like

hiddenMap.graphic.image.alpha = 0.5;


(Martí Angelats i Ribera) #2

You have to create a second type of wall. Then check the collision and if it does, change the opacity. How can you do this? Simply use this code:

public class SecretWall extends Entity
{
	//embed
	public static const SECRET_WALL:Class;
	public image:Image;
	
	public SecretWall(x, y)
	{
		image = new Image(SECRET_WALL);
		super(x, y, image);
		
		width = 20; //I don't know your case. This sets the hitbox
		heiht = 20;
	}
}

And then:

//inside the update class of the player
var e:Entity = collide("secretWall", x, y);
if (e)
{
	(e as SecretWall).image.alpha = 0.5;
}

PD: To change a image that’s not public you can use:

(entity.graphic as Image).alpha = 0.5;

(Lozza JP) #3

The problem is I have the secret wall constructed in OGMO and imported XML as a tilemap.

hiddenMapImage = new Tilemap(Assets.BASETILE, uint(mapXML.@width), uint(mapXML.@height), 24, 24);

hiddenMapEntity = new Entity(0, 0, hiddenMapImage, mapGrid);

add(mapEntity);

I have then added type = “hiddenmap” but I can’t get the player to collide with it unless he collides with a solid wall via forceful x/y movement. So it is something to dow ith mapGrid maybe?


(Martí Angelats i Ribera) #4

I hardly suggest to use different classes. You will have to modify a bit the way you import but it will be for the best.


(Lozza JP) #5

So you think I should make it a class and then add each block individually by code?


(Martí Angelats i Ribera) #6

When loading the XML, you can set a parameter saying wich class you want. Or simply add a parameter saying if it is a secret wall


(Lozza JP) #7

Can you give me an example or where to look further?

Can’t see much from the documentation, all I can find is getXML I can’t even find loadFromString -_-


(Lozza JP) #8

I fixed this by making a new grid layer in OGMO.

then, importing the grid, the hidden map entity now uses hidden grid as the mask and gave the entity a type and player collides with type.

The only downside is now there are two grid layers, does this affect performance at all? It is going to be way easier for level design vs coding every single hidden tile.


(Lozza JP) #9

Hey Copying,

getting a 1009 null object reference with (e as SecretWall).image.alpha = 0.5;

even if I try putting it in right after it is added to the world!

is it becauase I am using a mask grid?

update: I can make visible = false work though :frowning:

update again: Im reading the documentation and the entity is added new Entity(x, y, mapImage, hiddenGrid)

mapImage is a tilemap, and the entity constructor takes in a graphic as the parameter. So is my hiddenMap.graphic as far as it goes, it doesnt subclass to image?


(Martí Angelats i Ribera) #10

Try doing:

(e.graphic as Image).alpha = 0.5;

(Lozza JP) #11

In my player update method:

		var hiddenMap:Entity = world.getInstance("hiddenmap") as Entity;
		if (collideTypes("hiddenmap", x, y))
		{
			
			(hiddenMap.graphic as Image).alpha = 0.5;
			
			
		}

returns the 1009 error when I collide with it.

But if I swap the graphic as image line for hiddenMap.visible = false; that works and makes it disappear compeltely.


(Jacob Albano) #12

You’re casting your graphic to an Image

…but it’s a Tilemap.

┐(゚~゚)┌


(Lozza JP) #13

sooo I cant alpha it? :frowning:


(Jacob Albano) #14

I gave you the tools to figure it out. Everything you need to know is in those two documentation pages.


(Lozza JP) #15

graphic and image?

thank you will keep looking :slight_smile:

update: ah ha! tilemap is a subclass of canvas which holds the alpha variable. got it going perfect now thank you again Jacob!


(Jacob Albano) #16

I don’t know how casting works in Java, but in as3 when you cast a value to the wrong type with the as keyword, it returns a null variable. In this case, you were casting a graphic to Image when it should have been a Tilemap. Hence, null reference error.


(Lozza JP) #17

Not really sure what would happen in Java either to be honest.

I made it slowly fade in a counting fashion, what do you think? Could it be improved?

stepCounter += FP.elapsed;

if (stepCounter > 0.1)

(hiddenMap.graphic as Canvas).alpha = 0.9;

if (stepCounter > 0.2)

(hiddenMap.graphic as Canvas).alpha = 0.8;

if (stepCounter > 0.3)

(hiddenMap.graphic as Canvas).alpha = 0.7;

if (stepCounter > 0.4)

(hiddenMap.graphic as Canvas).alpha = 0.6;

if (stepCounter > 0.5)

(hiddenMap.graphic as Canvas).alpha = 0.5;


(Zachary Lewis) #18
override public function update():void {
  stepCounter += FP.elapsed;
  Canvas(hiddenMap.graphic).alpha = stepCounter < 0.5 ? 1 - stepCounter : 0.5;

  super.update();
}

:wink:


(Lozza JP) #19

what does ? and : do in this case?

Also I notice a super update call, should you do this anytime you override update?

Or make a super call anytime you override any default function?


(Jacob Albano) #20

Those mystery operators are what’s called a [ternary operation][1]. It works out to this:

result = valueToTest ? valueIfTrue : valueIfFalse;

It’s the same as doing this:

if (valueToTest)
{
    result = valueIfTrue;
}
else
{
    result = valueIfFalse;
}

As for using super, a general rule of thumb is to always use it unless you explicitly want to prevent the overridden behavior from running. If you don’t call super.update() on an Entity, for example, particle emitters and spritemaps that are attached to it won’t update, and tweens won’t run. [1]: https://en.wikipedia.org/wiki/?: