价值进入负数 - 如何阻止它?

时间:2014-02-08 02:49:44

标签: actionscript-3 flash function numbers

我有一个小型升级系统,用户可以通过使用屏幕上的方向箭头来增加或减少它们的健康和防御状态。基本上,我已经做到这一点,当你增加或减少统计数据时,剩余的点数相应地上升或下降。问题是当健康和防御的两个值都达到零时,你按下按钮减少/增加所有点消失后的数据,这些点变为负数。

我想知道是否有办法让它成为当用户使用了所有他们的统计分数时,统计数字会识别出他们全部被使用并且没有进入负数。这是在AS3中编码的。这是我的代码。

addEventListener(Event.ENTER_FRAME, healthScore);
addEventListener(Event.ENTER_FRAME, defenceScore);
addEventListener(Event.ENTER_FRAME, remainPoints);

decBtnDef.addEventListener(MouseEvent.CLICK, decreaseDefenceDef);
incBtnDef.addEventListener(MouseEvent.CLICK, increaseDefenceDef);
decBtnHP.addEventListener(MouseEvent.CLICK, decreaseHealthHP);
incBtnHP.addEventListener(MouseEvent.CLICK, increaseHealthHP);

function healthScore (event:Event){
healthShow.text = String (health);
}
function defenceScore (event:Event){
defenceShow.text = String (defence);
}
function remainPoints (event:Event){
pointsShow.text = String (points);
}

var health:Number = 10;
var defence:Number = 10;
var points:Number = 10;

_test3.text = "You have"; pointsShow.text = String (points); _pointstxt.text = "points.";
_test.text = "You have"; healthShow.text = String (health); _healthtxt.text = "health.";
_test2.text = "You also have"; defenceShow.text = String (defence); _deftxt.text =     "defence.";

function increaseDefenceDef (e:MouseEvent):void{ //when mouse click on up btn
if (defence >= 0)
    defence +=1;
    points -=1;
} 

function decreaseDefenceDef (e:MouseEvent):void{ //when mouse click on down btn
if (defence > 0)
defence -= 1;
points +=1;
}

function increaseHealthHP (e:MouseEvent):void{ //when mouse click on up btn
if (health >= 0)
    health +=1;
    points -=1;
} 

function decreaseHealthHP (e:MouseEvent):void{ //when mouse click on down btn
if (health > 0)
health -= 1;
points +=1;
}

1 个答案:

答案 0 :(得分:0)

   function increaseDefenceDef (e:MouseEvent):void
   { //when mouse click on up btn
        if (points >= 1)
        {
            defence +=1;
            points -= 1;
        }
    } 

    function decreaseDefenceDef (e:MouseEvent):void
    { //when mouse click on down btn
        if (defence >= 1)
        {
            defence -= 1;
            points += 1;
        }
    }

    function increaseHealthHP (e:MouseEvent):void
    { //when mouse click on up btn
        if (points >= 1)
        {
            health +=1;
            points -= 1;
        }
    } 

    function decreaseHealthHP (e:MouseEvent):void
    { //when mouse click on down btn
        if (health >= 1)
        {
        health -= 1;
        points += 1;
        }
    }