Identify button click


(Savvas Antoniou) #1

If i have 2 buttons or more in one class how i get witch button is clicked?

package
{ import net.flashpunk.World;

public class MAIN_GUI extends World
{
	
	public function MAIN_GUI() 
	{
		trace("world init");
		add(new GUI_CLASS(4, 100, 30));
		add(new GUI_CLASS(1, 125, 60));
		add(new GUI_CLASS(3, 140, 100));
		add(new GUI_CLASS(2, 165, 110));
		add(new GUI_CLASS(5, 120, 20));
		
	}
	
}

}

package { import flash.display.Graphics; import net.flashpunk.Entity; import net.flashpunk.graphics.Image; import net.flashpunk.FP; import net.flashpunk.utils.Input; import net.flashpunk.utils.Key; /** * … * @author Savvas */ public class GUI_CLASS extends Entity { [Embed(source = “…/GUI/BOX_IN_MAIN_GUI.png”)] private const MAIN__BOX:Class; private var mainboxsprite:Image = new Image(MAIN__BOX);

	[Embed(source = "../GUI/EASY_BTN.png")]
	private const EASY_BUTTON:Class;	
	private var easybuttonsprite:Image = new Image(EASY_BUTTON);
	
	[Embed(source = "../GUI/EASY_BTN1.png")]
	private const EASY_BUTTON1:Class;	
	private var easybutton1sprite:Image = new Image(EASY_BUTTON1);
	
	[Embed(source = "../GUI/MAIN_GUI_BOX.png")]
	private const GUIMAINBOX:Class;	
	private var guimainbox:Image = new Image(GUIMAINBOX);
	
	[Embed(source = "../GUI/TITLE_MAIN_GUI.png")]
	private const TITLE:Class;	
	private var title:Image = new Image(TITLE);
	
	public function GUI_CLASS(getName:int, xpos:int, ypos:int) 
	{
		if (getName == 1)
		{
			graphic = mainboxsprite;
			
		}
		
		if (getName == 2)
		{
			graphic = easybuttonsprite;
			
		}
		
		if (getName == 3)
		{
			graphic = easybutton1sprite;
			setHitboxTo(graphic);
			type = "easy_btn";
		}
		
		if (getName == 4)
		{
			graphic = guimainbox;
			setHitboxTo(graphic);
			type = "big_box";
		}
		
		if (getName == 5)
		{
			graphic = title;
			
		}
		
		x = xpos;
		y = ypos;
		
		
	}

(David Williams) #2

What do you want to happen when a button is clicked? Personally, I would think that you’d want to pass the graphic, hitbox, and type as a parameter to the GUI_CLASS class, as well as the function to execute upon button click. If you’d like some pseudo-code, I could piece some together (It’s been a bit since I’ve programmed in AS3, but it should be close.)