How to send back to the user class that the object of this class was clicked on

I ran into this problem, I can’t figure out how to do it right. [Haxe / OpenFL]

I want to make the following menu. Three images / buttons are displayed for the player on the screen. When a player clicks on one of the images / buttons, a text with a description appears under this button.

My is, I don’t know how to send from Main (where I create these buttons and using them), send information to the user class of these images / buttons, which specific button / image was pressed.

Here is an example code, first from a custom image / button class:

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 the code from the 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.

0
1

Button .

static var instances = new Array();

Button ,

instances.push(this);

, , :

public static function setClickedInstance(instance:Button):Void{
   //manipulation code goes here
}

:

Button.setClickedInstance();

, , . , , .

Button, , "" .

+2

Source: https://habr.com/ru/post/1627426/


All Articles