Actionscript 3动态创建的按钮未显示

时间:2012-07-24 16:43:59

标签: actionscript-3 flash

我是Flash和Actionscript的新手。我有一部从C#程序启动的电影。在电影中,我正在创建不同的文本字段并将数据传递回C#程序。我也有一个热点,点击它时我想创建一个弹出的小菜单。我已经看了很多方法来做到这一点,我认为最简单的方法(或者我认为)就是在热点下创建几个按钮。由于某些原因,当我点击热点时,按钮不会出现在舞台上。我知道它正在通过创建按钮的例程,因为我显示了一条消息。我发布了我的代码。谢谢你的帮助!!

import flash.text.TextField;
import fl.controls.Button; 
import flash.events.Event;

hotSpot.addEventListener(MouseEvent.CLICK, showMenu);

var continueBtn:Button;
var exitBtn:Button;

function showMenu(evt: Event):void
{
    continueBtn = new Button();

    continueBtn.x = 20;
    continueBtn.y = 100;
    continueBtn.width = 30;
    continueBtn.height = 20;
    continueBtn.border = true;
    continueBtn.visible = true;
    continueBtn.label = "Continue";
    addChild(continueBtn);

    exitBtn = new Button();

    exitBtn.x = continueBtn.x;
    exitBtn.y = continueBtn.y + continueBtn.height;
    exitBtn.width = 30;
    exitBtn.height = 20; 
    exitBtn.border = true;
    exitBtn.visible = true;
    exitBtn.label = "Exit";
    addChild(exitBtn);

    continueBtn.addEventListener(MouseEvent.CLICK, sendMsg);
    exitBtn.addEventListener(MouseEvent.CLICK, endFlash);

    inTxt.text = "showMenu";

}

显示消息“showMenu”,但没有一个按钮显示。

加里

1 个答案:

答案 0 :(得分:0)

function showMenu(evt: Event):void

evt必须是“MouseEvent”而不是“Event”,因为您添加到“hotSpot”精灵(可能或其他显示对象)的侦听器是MouseEvent而不是Event。

hotSpot.addEventListener(MouseEvent.CLICK, showMenu);

这会调用showMenu函数来侦听和捕获MouseEvent,而不是事件事件。

您还只导入了Event,导入了MouseEvent! 新法典:

import flash.text.TextField;
import fl.controls.Button; 
import flash.events.MouseEvent; //This line changed!

hotSpot.addEventListener(MouseEvent.CLICK, showMenu);
var continueBtn:Button;
var exitBtn:Button;
 //This line changed!
function showMenu(evt:MouseEvent):void{
continueBtn = new Button();
continueBtn.x = 20;
continueBtn.y = 100;
continueBtn.width = 30;
continueBtn.height = 20;
continueBtn.border = true;
continueBtn.visible = true;
continueBtn.label = "Continue";
addChild(continueBtn);

exitBtn = new Button();

exitBtn.x = continueBtn.x;
exitBtn.y = continueBtn.y + continueBtn.height;
exitBtn.width = 30;
exitBtn.height = 20; 
exitBtn.border = true;
exitBtn.visible = true;
exitBtn.label = "Exit";
addChild(exitBtn);

continueBtn.addEventListener(MouseEvent.CLICK, sendMsg);
exitBtn.addEventListener(MouseEvent.CLICK, endFlash);

inTxt.text = "showMenu";
}