在循环时需要帮助调试ActionScript

时间:2011-12-02 07:28:17

标签: actionscript-3 while-loop flash-builder

我讨厌我必须在这里问一个家庭作业问题,但我无法帮助它。我在学校学习ActionScript课程,这是我第一次学习编程语言,所以我觉得自己很吵。

问题是让RPG游戏在循环中练习。

        var fighter1:String = "Wonder Woman";
        var fighter2:String = "Ms. Marvel";
        var health1:uint = 300;
        var health2:uint = 180;
        var damage1:uint = 30;
        var damage2:uint = 40;


        while (health1 >= 0 && health2 >= 0 ) 
        {
            var damageDealt1:uint = Math.random() * damage1;
            var damageDealt2:uint = Math.random() * damage2;
            var attack1:uint = health2 - damageDealt1;
            var attack2:uint = health1 - damageDealt2;
            health1 = attack2;
            health2 = attack1;
            trace("After attack: " + fighter1 + " HP: " + health1 + ", " + 
            fighter2 + " HP: " + health2);
        }
        if (health1==0&&health2==0){
            trace("They both died in battle!  There will now be WAR!");
        } else if (health1==0) {
            trace(fighter2 + " won!");
        } else {
            trace(fighter1 + " won!"
        }

你可能已经知道,damage1是对战斗机1的潜在伤害,而damageDealt1是每次攻击造成的伤害。

一切似乎都正常工作,但是当我运行它时我得到了这个:

After attack: Wonder Woman HP: 4280752046, Ms. Marvel HP: 4284398685
After attack: Wonder Woman HP: 4280752039, Ms. Marvel HP: 4284398660
After attack: Wonder Woman HP: 4280752024, Ms. Marvel HP: 4284398658
After attack: Wonder Woman HP: 4280752018, Ms. Marvel HP: 4284398639
After attack: Wonder Woman HP: 4280751997, Ms. Marvel HP: 4284398638
After attack: Wonder Woman HP: 4280751996, Ms. Marvel HP: 4284398612
After attack: Wonder Woman HP: 4280751994, Ms. Marvel HP: 4284398609

ad无限...

我已将它翻译成php并且工作正常,我无法弄清楚我在这里做错了什么。

有什么想法吗?提前致谢。

1 个答案:

答案 0 :(得分:0)

正如turbosqel所说 - 你必须使用int而不是uint:

var fighter1:String = "Wonder Woman";
var fighter2:String = "Ms. Marvel";
var health1:int = 300;
var health2:int = 180;
var damage1:uint = 30;
var damage2:uint = 40;


    while (health1 > 0 && health2 > 0 ) 
    {
        var damageDealt1:uint = Math.random() * damage1;
        var damageDealt2:uint = Math.random() * damage2;
        var attack1:uint = health2 - damageDealt1;
        var attack2:uint = health1 - damageDealt2;
        health1 = attack2;
        health2 = attack1;

        if (health1 < 0) health1 = 0;
        if (health2 < 0) health2 = 0;

        trace("After attack: " + fighter1 + " HP: " + health1 + ", " + 
        fighter2 + " HP: " + health2);
    }

    if (health1==0&&health2==0){
        trace("They both died in battle!  There will now be WAR!");
    } else if (health1==0) {
        trace(fighter2 + " won!");
    } else {
        trace(fighter1 + " won!"
    }