我有一个自定义的菜单按钮类。在主函数中,我创建了此按钮的对象,并在函数中使用它们。最终我想要实现:当我点击按钮1时,显示文本1;点击按钮2,显示文本2,以此类推...
以下是我的主函数和自定义类的代码示例:
以下是我的主函数和自定义类的代码示例:
class WeaponMenu extends Sprite{
var wpnMenuImagePath:String;
var _wpnMenuImagePath:String;
var wpnMenuName:String;
var _wpnMenuName:String;
var swordMenuBtmp:Bitmap = new Bitmap ();
var swordMenuSprt:Sprite = new Sprite();
var bowMenuBtmp:Bitmap = new Bitmap ();
var bowMenuSprt:Sprite = new Sprite();
public function new(wpnMenuImagePath, wpnMenuName) {
super();
_wpnMenuImagePath = wpnMenuImagePath;
_wpnMenuName = wpnMenuName;
createWpnMenu ();
}
public function createWpnMenu () :Void {
if (_wpnMenuName == "Sword") {
swordMenuSprt.x = -500;
swordMenuSprt.y = ((Lib.current.stage.stageHeight - swordMenuBtmp.height) / 2) -150;
swordMenuBtmp = new Bitmap (Assets.getBitmapData (_wpnMenuImagePath));
swordMenuSprt.addChild(swordMenuBtmp);
var swordMenuSprtX:Float = ((Lib.current.stage.stageWidth - swordMenuBtmp.width) / 2) -200;
var swordMenuSprtY:Float = ((Lib.current.stage.stageHeight - swordMenuBtmp.height) / 2) -150;
Actuate.tween(swordMenuSprt, 2, { x:swordMenuSprtX, y:swordMenuSprtY } ) .delay( -1.1) .ease (Back.easeIn);
addChild(swordMenuSprt);
}
if (_wpnMenuName == "Bow") {
bowMenuSprt.x = (Lib.current.stage.stageWidth - bowMenuBtmp.width) / 2;
bowMenuSprt.y = Lib.current.stage.stageHeight + 500;
bowMenuBtmp = new Bitmap (Assets.getBitmapData (_wpnMenuImagePath));
bowMenuSprt.addChild(bowMenuBtmp);
var bowMenuSprtX:Float = (Lib.current.stage.stageWidth - bowMenuBtmp.width) / 2;
var bowMenuSprtY:Float = ((Lib.current.stage.stageHeight - bowMenuBtmp.height) / 2) -150;
Actuate.tween(bowMenuSprt, 2, { x:bowMenuSprtX, y:bowMenuSprtY } ) .delay( -1.1) .ease (Back.easeIn);
addChild(bowMenuSprt);
}
if (_wpnMenuName == "Staff") {
staffMenuSprt.x = Lib.current.stage.stageWidth + 500;
staffMenuSprt.y = ((Lib.current.stage.stageHeight - staffMenuBtmp.height) / 2) -150;
staffMenuBtmp = new Bitmap (Assets.getBitmapData (_wpnMenuImagePath));
staffMenuSprt.addChild(staffMenuBtmp);
var staffMenuSprtX:Float = ((Lib.current.stage.stageWidth - staffMenuBtmp.width) / 2) +200;
var staffMenuSprtY:Float = ((Lib.current.stage.stageHeight - staffMenuBtmp.height) / 2) -150;
Actuate.tween(staffMenuSprt, 2, { x:staffMenuSprtX, y:staffMenuSprtY } ) .delay( -1.1) .ease (Back.easeIn);
addChild(staffMenuSprt);
}
}
}
以下是来自主函数的代码:
class Main extends Sprite {
public var swordMenu:WeaponMenu;
public var bowMenu:WeaponMenu;
public var staffMenu:WeaponMenu;
public function new () {
super ();
swordMenu = new WeaponMenu ("ui/swordBtn.png", "Sword");
bowMenu = new WeaponMenu ("ui/bowBtn.png", "Bow");
staffMenu = new WeaponMenu ("ui/staffBtn.png", "Staff");
weaponChoose ();
}
public function weaponChoose ():Void {
swordMenu.buttonMode = true;
swordMenu.useHandCursor = true;
bowMenu.buttonMode = true;
bowMenu.useHandCursor = true;
staffMenu.buttonMode = true;
staffMenu.useHandCursor = true;
swordMenu.createWpnMenu();
bowMenu.createWpnMenu();
staffMenu.createWpnMenu();
addChild(swordMenu);
addChild(bowMenu);
addChild(staffMenu);
swordMenu.addEventListener(MouseEvent.CLICK, choose);
bowMenu.addEventListener(MouseEvent.CLICK, choose);
staffMenu.addEventListener(MouseEvent.CLICK, choose);
}
public function choose (event:MouseEvent):Void {
if (event.currentTarget == swordMenu) {
trace ("good");
}
swordMenu.removeEventListener(MouseEvent.CLICK, choose);
bowMenu.removeEventListener(MouseEvent.CLICK, choose);
staffMenu.removeEventListener(MouseEvent.CLICK, choose);
removeChild(swordMenu);
removeChild(bowMenu);
removeChild(staffMenu);
}
很抱歉这是原始的代码示例。
我想要做的是检查在主程序中按下了哪个按钮,并将此信息发送到自定义类中并触发一个函数,以显示每个按钮的适当文本。