简体   繁体   English

从Stage Flash AS3中删除按钮

[英]Remove button from Stage Flash AS3

I currently have several movie clips and buttons on the stage that do different things. 我目前在舞台上有几个影片剪辑和按钮,可以做不同的事情。 I have one button, that "Attacks" the enemy player and decreases his HP. 我有一个按钮,可以“攻击”敌方玩家并降低其HP。 This button has an event listener for a click, and when that is activated it goes through an IF statement and changes his health bar etc based on how low his health is. 该按钮具有用于单击的事件侦听器,并且在激活该按钮时会通过IF语句并根据其健康状况如何更改其健康状况等。 When the health reaches 0 I want to transition the entire screen to another ending screen. 当运行状况达到0时,我想将整个屏幕过渡到另一个结束屏幕。

I tried using .visible to make all of my other objects go invisible and that worked, however setting the instance button that I click to attack as not visible will not work. 我尝试使用.visible使所有其他对象变为不可见并且可以正常工作,但是将单击以进行攻击的实例按钮设置为不可见将不起作用。 I have also tried removeChild, which won't remove the button, and gotoAndPlay/Stop to a future frame gives me a null object reference. 我也尝试过removeChild,它不会删除按钮,并且gotoAndPlay / Stop到以后的帧给了我一个空对象引用。

Here's the code for that specific button at that 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? 
    }


}

Try to use formatting with leading lowercase characters for your variable and function names and leading uppercase for class names. 尝试对变量和函数名称使用带小写字母开头的格式,对类名使用带大写字母开头的格式。 This is a common practice and will make reading your code easier. 这是一种常见的做法,它将使您阅读代码更加容易。

When removing the button you should remove the listener as well. 删除按钮时,您也应该删除监听器。 (look up and read about weak referencing as you might decide to start using this). (查找并阅读有关弱引用的信息,因为您可能决定开始使用此功能)。

So your AS3 might look something like this: 因此,您的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;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM