Hide Entity Behind A Specific Area In The World


(Zouhair Elamrani Abou Elassad) #11

Thanks buddy, i’ll check that out :smile:


(Martí Angelats i Ribera) #12

If you want to have a graphic in the slave, you need to add super.render(); at the end of the MagicSlave's render function. I thought you weren’t using it so i simply didn’t add it (makes the code a little faster).


(Zouhair Elamrani Abou Elassad) #13

Actually i wanted to copypixels every frame from the moving background, so the user won’t be able to see that there is an entity in there.


(Martí Angelats i Ribera) #14

Isn’t that what it does?


(Zouhair Elamrani Abou Elassad) #15

It only takes a clip for the first time, then when the parallax changes, the MagicMirror doesn’t change also, it always shows the first image copied from the bachground.


(Martí Angelats i Ribera) #16

Hmm… Could you try this?

Slave

	override public function render():void
	{
		//Render the current screen (the one rendered so far) to the shwon by the parent.
		_rect.setTo(_parent.x, _parent.y, _parent.img.width, _parent.img.height);
		_parent.img.fillRect(_parent.img.rect, 0);
		_parent.img.copyPixels(FP.buffer, _rect, FP.zero);
	}

(rostok) #17

Here’s the working code. Hope this is what you wanted: http://rostok.3e.pl/download/MagicMirror.zip

/**
 * MagicMirror creates two entangled entities at different layers. 
 * The one at top renders everything that is below the one at the bottom.
 * 
 * Master is at the bottom because it is processed first
 * @author rostok
 */
public class MagicMirror extends Entity
{
	public var slave:MagicMirror = null;
	public var master:MagicMirror = null;
	public var saved:BitmapData = null;
	public var topLayer:int;
	public var isMaster:Boolean;
	
	public function MagicMirror(x:Number=0, y:Number=0, w:Number=0, h:Number=0, layer:int=0, topLayer:int=0, isMaster:Boolean=true, g:Graphic = null) 
	{
		// store position, dimensions and layers, and is it master
		this.layer = isMaster ? layer : topLayer;
		this.width = w;
		this.height = h;
		this.topLayer = topLayer;
		this.isMaster = isMaster;
		this.x = x;
		this.y = y;
		
		if (g) this.graphic = g;
	}
	
	override public function added():void 
	{
		super.added();
		if (isMaster)
		{
			slave = new MagicMirror(x, y, width, height, layer, topLayer, false);
			slave.master = this;
			world.add( slave );
		}
	}
	
	override public function render():void 
	{
		//super.render();
		//return;
		
		if (isMaster)
		{
			// copy position and dimensions to slave		       	
			if (slave)
			{
				slave.x = x;
				slave.y = y;
				slave.width = width;
				slave.width = height;
			}

			// store part of the buffer
			saved = new BitmapData(width, height, true, 0);
			// copy part of the screen 
			if (graphic && graphic is Image)
				saved.copyPixels(FP.buffer, new Rectangle(x, y, width, height), new Point(), Image(graphic).getBuffer(), new Point(),  true);
			else
				saved.copyPixels(FP.buffer, new Rectangle(x, y, width, height), new Point());
		}
		else
		{
			// paste stored part of the buffer
			if (master && master.saved) FP.buffer.draw(master.saved, new Matrix( 1, 0, 0, 1, x, y ));
		}
	}
}

(Zouhair Elamrani Abou Elassad) #18

Thanks for the feedback, i tried it too, still it’s not working :stuck_out_tongue:


(Zouhair Elamrani Abou Elassad) #19

This is Great man, it’ working :wink:, can please explain a bit the code, that will be great ^^


(rostok) #20

Basically you create two entities of the same class, one is master another its slave. Master is placed at lower layer because it’s render function is called first. Then it captures the screen buffer of specified area and stores it. In case graphics is assigned copyPixels function uses its alpha channel to create mask. The slave which is above is rendered afterwards and it uses reference to master to paste previously stored buffer. Two more things: I forgot to overwrite the removed function to remove slave as well and also you should assign position to x and y as integers.


(Zouhair Elamrani Abou Elassad) #21

Thanks buddy, that was very helpful :wink:


(Zouhair Elamrani Abou Elassad) #22

Hi again, i’m still using this wonderful class of yours :slight_smile:, i was just wondering if it’s possible to make the MagicMirror prevent mouse collisions (using masks) with buttons that exist behind it ?


(rostok) #23

Don’t know how what kind of buttons you use and how they are implemented. Provided that you can inject your code in button being pressed handler you could get all the MagicMirror objects with world.getClass() and simply iterate through them and see if there is actually any mirror above the button and also if cursor position is colliding with it. In case you have simple MagicMirror with rectangular area test is simple. In case you use mask you will have to test position with master’s graphic’s. If it is an Image you will have to use getPixel() in BitmapData.


(Zouhair Elamrani Abou Elassad) #24

Thanks for the quick feedback, in fact these are my buttons, once i click on the settings button(bottom right), i hide my Story and Survival buttons using magic mirror (picture 2).

But when i hover my mouse on top of the Magic Mirror (just where the buttons are) it’s like there is no mirror on top of them, i’m using Mask to detect collision when it comes to the button.

Thank you.


(rostok) #25

i think this is overcomplicated or i dont get the idea. what would you like to achieve?


(Zouhair Elamrani Abou Elassad) #26

The idea is that i want to show a credit popup, it’s transparent but i don’t want it to show the buttons behind it instead, it should show the parallax in the background, what i did is i created a Mirror of the parallax in the popup but i noticed that even when the buttons are hidden behind the Mirror, their collisions with the mouse is always active, there a few solutions i can try, like hide all the entities behind the Mirror(alpha = 0) and in this case i will no longer need the Mirror, but in other cases there are a lot of entities so it’ll be so frustrating.


(rostok) #27

But aren’t you really just trying to hide these buttons when credits is shown?


(Zouhair Elamrani Abou Elassad) #28

That’s the idea, but also there will be a text in that Popup that the user may interact with, so i have to disable access to the hidden buttons.


(rostok) #29

So why you just don’t hide/deactivate buttons once popup is visible?


(Zouhair Elamrani Abou Elassad) #30

Yes that’s the alternative idea, in fact; there other worlds where i’m willing to use the same mechanism, but in those worlds it’s not just about two buttons, but instead there are images, sliders, texts and stuff, so i’ll have to hide all of them which can be good, but i thought why not find out a better solution :), but eventually if nothing works out, i’m gonna have to go for that :slight_smile: