在函数之间共享随机变量

时间:2018-01-20 18:41:04

标签: javascript

我试图对游戏进行编码,其中一部分有战斗系统。我希望玩家能够多次点击按钮找到对手,然后在找到他们喜欢的按钮时单击按钮,然后定时事件会慢慢显示结果。

我遇到的问题是:
- 如果VAR在函数之外,那么你只能渗透一次,但如果它在第一个函数内部,那么其他函数不能使用这些值进行战斗。
- 胜利的max_acres以字符串形式出现,而不是10 + 3 = 13,它变为103.我该如何解决?

非常感谢您的期待,我感谢您的帮助!

使用Javascript:

    var titles = ["Peasant", "Knight"];
    var first = ["Ada", "Adro", "Ama"];
    var last = ["nija", "har", "nake"];

    var random_name1 = titles[Math.floor(Math.random() * titles.length)] + ' ' + first[Math.floor(Math.random() * first.length)] + last[Math.floor(Math.random() * last.length)];
    var random_acres1 = (max_acres * (Math.floor(Math.random() * (140 - 75)) + 75) / 100).toFixed(0);
    var random_troops1 = (random_acres1 * (Math.floor(Math.random() * (1500 - 600)) + 600) / 100).toFixed(0);
    var random_off1 = (random_troops1 * (Math.floor(Math.random() * (1200 - 400)) + 400) / 100).toFixed(0);
    var combat_victory_acres1 = (random_acres1 * (((Math.random() * (35 - 11)) + 11) / 100)).toFixed(0);
    var combat_defeat_acres1 = (random_acres1 * (Math.floor(Math.random() * (20 - 11)) + 11) / 100).toFixed(0);
    var text_victory = 'You have been awarded with ';
    var text_defeat = 'You have lost control of ';
    var text_acres = ' acres.';

function infiltrate(){
    var x = document.getElementById("Combat_table");
    if (x.style.display === "none") {
        x.style.display = "block";
    }
    document.getElementById('combat_army_strength').innerHTML = army_strength;
    document.getElementById('combat_max_acres').innerHTML = max_acres;
    document.getElementById('random_name1').innerHTML = random_name1;
    document.getElementById('random_acres1').innerHTML = random_acres1;
    document.getElementById('random_troops1').innerHTML = random_troops1;
    document.getElementById('random_off1').innerHTML = random_off1;
};

function attack_random1(){
    document.getElementById("attack_button1").style.display="none";
    document.getElementById("infiltration").style.display="none";
            var y = document.getElementById("Combat_Results");
    if (y.style.display === "none") {
        y.style.display = "block";
    }
    setTimeout(Combat_Text4, 5000)
    var final_outcome1 = army_strength - random_off1;
    if (final_outcome1 >= 0) {
        setTimeout(Combat_Text_Victory1, 6000);
    } else {
        setTimeout(Combat_Text_Defeat1, 6000);
    }
};

function Combat_Text4() {
    document.getElementById("Combat_Results4").innerHTML = "The battle is over, a scout is dispatched to you with the results.";
}
function Combat_Text_Victory1() {
    max_acres = max_acres + combat_victory_acres1;
    var text_victory_1 = text_victory + combat_victory_acres1 + text_acres;
    document.getElementById("Combat_Results5").innerHTML = "You achieved <b>Victory!</b>";
    document.getElementById("Combat_Results6").innerHTML = text_victory_1;
    document.getElementById('max_acres').innerHTML = max_acres;
    document.getElementById('combat_max_acres').innerHTML = max_acres;
}
function Combat_Text_Defeat1() {
    max_acres = max_acres - combat_defeat_acres1;
    var text_defeat_1 = text_defeat + combat_defeat_acres1 + text_acres;
    document.getElementById("Combat_Results5").innerHTML = "You have been <b>Defeated!</b>";
    document.getElementById("Combat_Results6").innerHTML = text_defeat_1;
    document.getElementById('max_acres').innerHTML = max_acres;
    document.getElementById('combat_max_acres').innerHTML = max_acres;
}

HTML:

   <div id="Combat" class="tabcontent">

        Total Land: <span id="combat_max_acres">10</span><br>
        Total Offense: <span id="combat_army_strength">0</span><p>
        <button id="infiltration" onclick="infiltrate()">Infiltrate Kingdoms</button>
        <div id="Combat_table" style="display: none">
        <center><table>
            <tr valign="center">
                <th>Kingdom Name</th>
                <th>Acres</th>
                <th>Troop <br>Numbers</th>
                <th>Total <br>Offense</th>
                <th></th>
            </tr>
            <tr id="combat_row1">
                <td><span id="random_name1"></span></td>
                <td><span id="random_acres1"></span></td>
                <td><span id="random_troops1"></span></td>
                <td><span id="random_off1"></span></td>
                <td><button onclick="attack_random1()" id="attack_button1">Attack!</button></td>
            </tr>
        </table>
    </div>
    <br>
    <div id="Combat_Results" style="display: none">
        <center><table><tr>
                <td><center><span id="Combat_Results4"></span></td>
            </tr><tr>
                <td><center><span id="Combat_Results5"></span></td>
            </tr><tr>
                <td><center><span id="Combat_Results6"></span></td>
            </tr>
        </table>
    </div>

1 个答案:

答案 0 :(得分:0)

  

胜利的max_acres以字符串形式出现,而不是10 + 3 = 13,它变为103.我该如何解决?

使用Math.round代替Number#toFixed,因为对于附加数值,您需要两个操作数作为数字,而不是字符串。

仅使用toFixed来显示数字。

对于其他部分,我建议使用带有数组的对象用于相同的项目,例如

{
    name: [],
    acres: [],
    troops: [],
    off: [],
    victory_acres: [],
    combat_defeat_acres: [],
    // etc.
}