Show and Hide menu when the user press the button


(Zouhair Elamrani Abou Elassad) #1

Hi, i want to create a menu that a show inside the game when the user press escape, i have a class to generate buttons :

public class ButtonClass extends Entity
{	
	private var button:Image;
	private var _words:Text = new Text (" ");
	private var _type:int;
	
	private var _isAdded:Boolean = false;
	private var _isRemoved:Boolean = false;
	
	public function ButtonClass(words:String,type:int, posX:int, posY:int) 
	{
		super(posX, posY);
		
		//get parameters and set to private variables
		_words.text = words;
		_type = type;
		
		//create button to specs of incomming text and set equal to graphic for clicking
		if(_type == 0) button = Image.createRect(_words.width, _words.height,0x008040);
		else button = Image.createRect(_words.width, _words.height, 0xFF0000);
		//button.x = posX;
		//button.y = posY;
		
		//move _words to same location
		//_words.x = posX;
		//_words.y = posY;
		
		//set graphic to button and add hitbox
		graphic = button;
		setHitbox(_words.width, _words.height);
		
		//add elements to screen
		addGraphic(button);
		addGraphic(_words);
		
	}
	
	override public function update():void
	{
		trace("button ::: "+button.x +" -- "+ button.y);
		if (collidePoint(x, y, world.mouseX, world.mouseY) && _type != -1)
		{
			if (Input.mouseReleased) click();
		}

           }

       override public function added():void 
	{
		_isAdded = true;
		super.added();
	}
	
	override public function removed():void 
	{
		_isRemoved = true;
		
		super.removed();
	}

 }

Then in my world, i check if there is a button menu when the user hits escape, if there is one i remove otherwise i show it :

        if (Input.pressed(Key.ESCAPE)) {
			
			if (_backToMenu != null && _backToMenu.isAdded) {
				remove(_backToMenu);
				_backToMenu = null;
			}
			else {
				_backToMenu = new ButtonClass("Back", 4, FP.camera.x + 450, 250);
				add(_backToMenu);					
				
			}
			
		}

I’m wondering if there is a cleaner way to show and hide the menu when the user hits the escape button !


(Jean) #2

Have a instance of your menu always added, just make his position always out of the world, and the ESC is pressed(have caution with this key in flash, sometimes I believe this isn’t caught by the hook) just move it to 0,0 of the camera.