I am stack at this problem can't figure out how to do it properly. [Haxe/OpenFL]
I want to make following menu. On screen for player display three images/buttons. When player clicks on one of the images/buttons a text with description appears under that button.
My is, i dont know how to send from Main (where i create this buttons and using them), send info to custom class of this images/buttons, what specific button/image was pressed.
Here is example code, first from custom class of the images/buttons:
class CusstomButtons extends Sprite {
var buttonImagePath:String;
var _buttonImagePath:String;
var buttonName:String;
var _buttonName:String;
var button1Btmp:Bitmap = new Bitmap ();
var button1Sprt:Sprite = new Sprite();
var button2Btmp:Bitmap = new Bitmap ();
var button2Sprt:Sprite = new Sprite();
var buttonText1:TextField = new TextField ();
var buttonText2:TextField = new TextField ();
public function new(buttonImagePath, buttonName) {
super();
_buttonImagePath = buttonImagePath;
_buttonName = buttonName;
createButton ();
}
public function createButton () :Void {
if (_buttonName == Button1){
button1Btmp = new Bitmap (Assets.getBitmapData (_buttonImagePath));
button1Sprt.addChild(button1Btmp);
addChild(button1Sprt);
//Here goes the code for button position and tweening
}
if (_buttonName == Button2){
button2Btmp = new Bitmap (Assets.getBitmapData (_buttonImagePath));
button2Sprt.addChild(button2Btmp);
addChild(button2Sprt);
//Here goes the code for button position and tweening
}
}
public function displayButtonDescrition () :Void {
if (button1) {
buttonText1.text = "Some text for Button 1"
addChild(buttonText1);
//Here goes code for button position and etc
}
if (button2) {
buttonText2.text = "Some text for Button 2"
addChild(buttonText2);
//Here goes code for button position and etc
}
}
}
And here is code from main:
class Main extends Sprite {
public var button1Menu:CusstomButtons;
public var button2Menu:CusstomButtons;
public function new () {
super ();
button1Menu = new CusstomButtons ("path/button1", "button1");
button1Menu = new CusstomButtons ("path/button1", "button2");
}
public function createButtonMenu ():Void {
button1Menu.createButton();
addChild(button1Menu);
button2Menu.createButton();
addChild(button2Menu);
button1Menu.addEventListener(MouseEvent.CLICK, onClick);
button2Menu.addEventListener(MouseEvent.CLICK, onClick);
}
public function onClick (event:MouseEvent):Void {
if (event.currentTarget == button1Menu) {
button1Menu.displayButtonDescrition();
}
if (event.currentTarget == button2Menu) {
button2Menu.displayButtonDescrition();
}
}
}
The main question is how to use one function to display different description texts.
You could make a static field in the Button class to hold all created instances of it.
Then in the Button constructor store the instance that is currently being created
Finally, from the main class call a static method in the button class, passing the clicked instance:
And in the main class call the method when necessary:
Sorry if above doesn't compile as I couldn't test it right now. If not, it probably just needs a bit of tweaking.
An alternative would be to add a mouse listener in the Button class itself, but then you wouldn't have control in the main class when to "react" on clicks and when not.