2012-11-28 70 views
0

我目前在舞台上有几个影片剪辑和按钮,可以做不同的事情。我有一个按钮,即“攻击”敌方玩家并降低他的HP。这个按钮有一个点击事件监听器,当它被激活时,它会通过一个IF语句并根据他的健康状况变化他的健康状况等。当运行状况达到0时,我想将整个屏幕转换到另一个结束屏幕。从舞台闪光灯AS3删除按钮

我尝试使用。可见,使所有我的其他物体的隐身和工作,但设置实例按钮,我点击攻击为不可见的将无法正常工作。我也尝试过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? 
    } 


} 
+3

'visible'或'removeChild'应该工作。如果从显示列表中删除该对象会在稍后的帧中给出一个“null”引用异常,则该对象必须已被删除。 –

+0

作为一个吧?注意什么50个。 if(health1> 0){REGHBNAR.play(); } else {removeChild(thingyThatDied); } –

回答

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

}