我正在尝试制作一款游戏,在游戏中,点击一个按钮,健康条中的45点生命值就会下降。我有我的所有代码,它工作得很好,但我想让按钮,以便如果健康低于45,不会从健康栏中取走任何东西。我尝试使用:
if(health < 45) health = health;但这并没有奏效。我有一种感觉,解决这个问题很容易,但我就是想不出来。显然,我对这一切都很陌生,而且仍然发现很难理解一些概念。这是我的代码:
fortyfivedown_btn.addEventListener(MouseEvent.CLICK, fortyfivedownClick);
var health:int = 100;
lifebar.gotoAndStop(101);
function fortyfivedownClick(event:MouseEvent):void{
health -= 45;
if(health < 0) health = 0;
else if(health > 100) health = 100;
lifebar.gotoAndStop(health + 1);
}发布于 2012-07-16 16:29:40
如果健康小于或等于45,那么简单地什么都不做有什么错吗?例如,如下所示:
function fortyfivedownClick(event:MouseEvent):void {
if (health <= 45) {
return;
}
// Perform action
}如果玩家没有足够的健康,这将导致函数提前退出。
发布于 2012-07-16 16:34:24
如果我理解这个问题:
if(health>=45) // just add this
lifebar.gotoAndStop(health + 1);发布于 2012-07-16 16:34:57
这很简单,实际上,你的事件告诉你的健康降低45,然后检查健康是否低于0,你只需要在方法的开始检查你有多少健康,如果它是45或更低,就跳出方法。
不知道"break“是否能在flash中工作,但这将是最简单的解决方案。
例如:
function fortyfivedownClick(event:MouseEvent):void{
if (health <= 45) {
break;
}
health -= 45;
if(health < 0) health = 0;
else if(health > 100) health = 100;
lifebar.gotoAndStop(health + 1);
}https://stackoverflow.com/questions/11500564
复制相似问题