Actionscript 3 从舞台闪光灯AS3上拆下按钮

Actionscript 3 从舞台闪光灯AS3上拆下按钮,actionscript-3,flash,button,actionscript,stage,Actionscript 3,Flash,Button,Actionscript,Stage,我现在有几个电影剪辑和按钮在舞台上做不同的事情。我有一个按钮,可以“攻击”敌人玩家并降低他的生命值。这个按钮有一个用于点击的事件监听器,当它被激活时,它会通过一个IF语句,并根据他的健康状况有多低来更改他的健康栏等。当运行状况达到0时,我想将整个屏幕转换到另一个结束屏幕 我尝试使用.visible使我的所有其他对象都变为不可见,这是有效的,但是将我单击以攻击的实例按钮设置为不可见将不起作用。我还尝试了removeChild,它不会删除按钮,而gotoAndPlay/Stop to future

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

我尝试使用.visible使我的所有其他对象都变为不可见,这是有效的,但是将我单击以攻击的实例按钮设置为不可见将不起作用。我还尝试了removeChild,它不会删除按钮,而gotoAndPlay/Stop to future frame会给我一个空对象引用

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

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


}
stop();
OSButton.addEventListener(MouseEvent.CLICK,OSAttack);
函数OSAttack(事件:MouseEvent):void
{
var health1:int=parseInt(RegHealth.text);
health1=health1-1000;
如果(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尝试对变量和函数名使用带前导小写字符的格式,对类名使用带前导大写字符的格式。这是一种常见的做法,可以使阅读代码更容易

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

因此,您的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;
}

}
oSButton.addEventListener(MouseEvent.CLICK,oSAttack);
//或者使用弱引用
//oSButton.addEventListener(MouseEvent.CLICK,oSAttack,false 0,true);
函数oSAttack(事件:MouseEvent):void
{
var health1:int=parseInt(regHealth.text);
health1=health1-1000;
如果(health1==9000 | | | health1==8000 | | | health1==7000 | | | health1==5000 | | health1==4000 | | health1==2000 | | health1==1000 | health1==0){
REGHPBAR.play();
}
regHealth.text=health1.toString();

if(health1
visible
removeChild
应该可以工作。如果从显示列表中删除对象会在稍后的帧中出现
null
引用异常,则该对象必须已被删除。注意..50个or.if(health1>0){REGHBNAR.play();}else{removeChild(thingyThatDied);}