【问题标题】:How to send back to custom class which object of that class was clicked如何将单击该类的哪个对象发送回自定义类
【发布时间】:2016-02-09 06:35:26
【问题描述】:

我对这个问题很感兴趣,无法弄清楚如何正确地做到这一点。 [Haxe/OpenFL]

我想做以下菜单。在播放器屏幕上显示三个图像/按钮。当玩家点击其中一个图像/按钮时,该按钮下方会出现带有描述的文本。

我的是,我不知道如何从 Main(我在其中创建此按钮并使用它们)发送信息,将信息发送到此图像/按钮的自定义类,按下哪个特定按钮/图像。

这是示例代码,首先来自图像/按钮的自定义类:

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
    }
}
}

这是来自 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();
    }
}
}

主要问题是如何使用一个函数来显示不同的描述文本。

【问题讨论】:

    标签: haxe openfl


    【解决方案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 类本身中添加一个鼠标侦听器,但是您将无法在主类中控制何时对点击“做出反应”,何时不做出反应。

    【讨论】:

    • 感谢您的回答。我很抱歉我的愚蠢:) 但我还在学习。你可以说得更详细点吗。前两行我了解它们是如何工作的以及将它们放在哪里。但是我把 setClickedInstance 函数实例放在哪里?如果可以的话,你能不能用我的例子来说明它是如何完成的。
    • 你把它放在 Button 类(在你的例子中是 CusstomButtons)然后通过类名引用它从主类调用它,如下所示:CusstomButtons.setClickedInstance(event.currentTarget) 在点击监听器中.
    • 感谢您的回复和解决方案,非常有帮助。 :)
    猜你喜欢
    • 2012-04-07
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多