如何发送回自定义类单击该类的哪个对象 [英] How to send back to custom class which object of that class was clicked
问题描述
我很困惑这个问题,无法弄清楚如何正确地做到这一点. [Haxe/OpenFL]
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.
我是,我不知道如何从Main(在其中创建和使用它们的地方)发送信息,将信息发送到该图像/按钮的自定义类别,以及按下了哪个特定按钮/图像.
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
}
}
}
这是来自main的代码:
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.
推荐答案
您可以在Button类中创建一个静态字段,以保存该类的所有创建实例.
You could make a static field in the Button class to hold all created instances of it.
static var instances = new Array();
然后在Button构造函数中存储当前正在创建的实例
Then in the Button constructor store the instance that is currently being created
instances.push(this);
最后,从主类中调用按钮类中的静态方法,并传递所单击的实例:
Finally, from the main class call a static method in the button class, passing the clicked instance:
public static function setClickedInstance(instance:Button):Void{
//manipulation code goes here
}
然后在主类中根据需要调用该方法:
And in the main class call the method when necessary:
Button.setClickedInstance();
很抱歉,如果上面的代码无法编译,因为我现在无法对其进行测试.如果没有,可能只需要进行一些调整即可.
Sorry if above doesn't compile as I couldn't test it right now. If not, it probably just needs a bit of tweaking.
另一种选择是在Button类本身中添加一个鼠标侦听器,但是什么时候对点击反应"或什么时候不响应,您将无法在主类中拥有控制权.
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.
这篇关于如何发送回自定义类单击该类的哪个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!