Saya mencoba membuat permainan papan yang mirip dengan Monopoli di JS menggunakan HTML. Masalahnya adalah, beberapa peristiwa harus terjadi, secara ketat satu demi satu selama giliran pemain. Saya memiliki satu fungsi yang disebut takeTurn() di mana semua acara berlangsung. Saat ini saya tidak memiliki dialog dan hanya gerakan pemain dasar, yang mengambil nomor acak. Jadi pada kondisinya saat ini, game dapat bermain sendiri dengan kecepatan kilat. Apakah ada cara agar saya bisa menunggu suatu fungsi selesai? Async dan Await memiliki waktu tunggu yang ketat, saya ingin sesuatu yang benar-benar menunggu perhitungan fungsi selesai, dan nilai dikembalikan? Bagaimana saya bisa melakukan ini.


async function takeTurn(who=1) {
    // Move the player on the board.
    calcPos(who);

    // Display the player's money.
    displayBills(who);

    // Change player heading.
    document.getElementById('playerturn').innerText = "Player " + who + "'s Turn";

    // Implement getting out of jail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    // Roll and move.

    // TRY TO GET OUT OF JAIL.
    if (playerdata[who].pos == "jail") {
        console.log(`Player ${who} rolls to attempt to get out of jail.`);
        let roll1 = rollDice();
        let roll2 = rollDice();

        // The player attempts to role doubles and get out of jail.
        if (roll1 == roll2) {
            console.log(`Player ${who} rolled doubles and got out of jail.`);
            move(who, roll1+roll2);
            endTurn(who);
        } else {
            console.log(`Player ${who} failed to roll doubles and is still stuck in jail.`);
            endTurn(who);
        }
    // Move if we aren't in jail.
    } else {
        // Roll the dice.
        console.log(`Player ${who} rolls to move.`);
        let roll1 = rollDice();
        let roll2 = rollDice();
        console.log(`Player ${who} rolls a ${roll1} and a ${roll2}. They move ${roll1+roll2} spaces.`);
        move(who, roll1+roll2);

        // End the turn.
        endTurn(who);
    }
}
0
TimTam 20 November 2020, 06:46

1 menjawab

Jawaban Terbaik

Coba Kode ini:

async function takeTurn(who=1) {
// Move the player on the board.
await calcPos(who);

// Display the player's money.
await displayBills(who);

// Change player heading.
document.getElementById('playerturn').innerText = "Player " + who + "'s Turn";

// Implement getting out of jail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Roll and move.

// TRY TO GET OUT OF JAIL.
if (playerdata[who].pos == "jail") {
    console.log(`Player ${who} rolls to attempt to get out of jail.`);
    let roll1 = await rollDice();
    let roll2 = await rollDice();

    // The player attempts to role doubles and get out of jail.
    if (roll1 == roll2) {
        console.log(`Player ${who} rolled doubles and got out of jail.`);
        await move(who, roll1+roll2);
        await endTurn(who);
    } else {
        console.log(`Player ${who} failed to roll doubles and is still stuck in jail.`);
        await endTurn(who);
    }
// Move if we aren't in jail.
} else {
    // Roll the dice.
    console.log(`Player ${who} rolls to move.`);
    let roll1 = await rollDice();
    let roll2 = await rollDice();
    console.log(`Player ${who} rolls a ${roll1} and a ${roll2}. They move ${roll1+roll2} spaces.`);
    move(who, roll1+roll2);

    // End the turn.
    await endTurn(who);
}
}

Ini akan menunggu setiap fungsi selesai jika rollDice tidak membutuhkan waktu lama untuk dieksekusi.

1
mayur kukadiya 20 November 2020, 05:27