从Stage Flash AS3中删除按钮

时间:2012-11-28 03:21:33

标签: actionscript-3 flash button actionscript stage

我目前在舞台上有几个不同的电影剪辑和按钮。我有一个按钮,“攻击”敌方玩家并降低他的HP。这个按钮有一个单击的事件监听器,当它被激活时,它会通过一个IF语句并根据他的健康状况来改变他的健康栏等。当生命值达到0时,我想将整个屏幕转换到另一个结束屏幕。

我尝试使用.visible使我的所有其他对象变得不可见并且有效,但是将我点击进行攻击的实例按钮设置为不可见将无效。我也尝试过removeChild,它不会删除按钮,gotoAndPlay / Stop到未来的帧会给我一个空对象引用。

这是该帧特定按钮的代码。

stop();

OSButton.addEventListener(MouseEvent.CLICK, OSAttack);

function OSAttack(event:MouseEvent):void
{
    var health1:int = parseInt(RegHealth.text);
    health1 = health1 - 1000;


        if(health1 == 9000 || health1 == 8000 || health1 == 7000 || health1 == 6000 || health1 == 5000
       || health1 == 4000 || health1 == 3000 || health1 == 2000 || health1 == 1000 || health1 ==0){
        REGHPBAR.play();
    }


    RegHealth.text = health1.toString();


    if(health1 <= 0){
        ////// WHAT CODE DO I PUT HERE? 
    }


}

1 个答案:

答案 0 :(得分:0)

尝试使用带有前导小写字符的格式作为变量和函数名称,并使用大写字母表示类名。这是一种常见做法,可以让您更轻松地阅读代码。

删除按钮时,您也应该删除侦听器。 (查看并阅读弱引用,因为您可能决定开始使用此功能)。

所以你的AS3看起来像这样:

oSButton.addEventListener(MouseEvent.CLICK, oSAttack);

//or using weak referencing
//oSButton.addEventListener(MouseEvent.CLICK, oSAttack, false 0, true);

function oSAttack(event:MouseEvent):void
{
var health1:int = parseInt(regHealth.text);
health1 = health1 - 1000;

if(health1 == 9000 || health1 == 8000 || health1 == 7000 || health1 == 6000 || health1 == 5000 || health1 == 4000 || health1 == 3000 || health1 == 2000 || health1 == 1000 || health1 ==0){
REGHPBAR.play();
}


regHealth.text = health1.toString();

if(health1 <= 0){
////// remove the button
oSButton.removeEventListener(MouseEvent.CLICK, oSAttack);
oSButton.parent.removeChild(oSButton);

//if you no longer need the button you can null it
oSButton = null;
}

}