试图在使用Phaser制作的游戏中宣布获胜者,但它不断重复警报

时间:2015-11-25 08:08:59

标签: javascript jquery html css phaser-framework

我一直试图提醒玩家,如果玩家1获胜,那么警告玩家1获胜并确实如此,但它会不断弹出,直到您点击停止显示此对话框。

此外,当我添加检测第二个播放器时,它只是不起作用。

function detectWin () {
    if (score + playerTwoScore == 5900 && score > playerTwoScore) {
        alert("Player 1 Wins!");
    }
}

detectWin();

当我这样做时,它不能显示玩家2获胜。

function detectWin () {
    if (score + playerTwoScore == 5900 && score > playerTwoScore) {
        alert("Player 1 Wins!");
    } else (score + playerTwoScore == 5900 && score < playerTwoScore) {
        alert("Player 2 Wins!");
  }
}
 detectWin();

---如果你想看看我正在做什么,或者你想要使用它的一部分我不关心的完整片段 -

var game = new Phaser.Game(1024, 700, Phaser.AUTO, '', { preload: preload, create: create, update: update });

function preload() {
    game.load.image('starrySky', 'assets/starry_sky.png');
    game.load.image('ground', 'assets/ground.png');
    game.load.image('bottomCenterIcyPlatform', 'assets/bottom-center.png');
    game.load.image('middleCenterIcyPlatform', 'assets/middle-center.png');
    game.load.image('topCenterIcyPlatform', 'assets/top-center.png');
    game.load.image('bottomIcyPlatform', 'assets/bottom-side.png');
    game.load.image('middleIcyPlatform', 'assets/side-middle.png');
    game.load.image('veryTopMiddleIcyPlatform', 'assets/very-top.png');
    game.load.image('veryTopSideIcyPlatform', 'assets/very-top-side.png');
    game.load.image('present', 'assets/presents.png');
    game.load.spritesheet('dude', 'assets/anak.png', 32, 48);
    game.load.spritesheet('dudeTwo', 'assets/rudolph.png', 32, 48);
    game.load.audio('collectPresent', ['assets/audio/collect-present.mp3', 'assets/audio/collect-present.ogg']);
    game.load.audio('daWinner', ['assets/audio/the-winner.mp3', 'assets/audio/the-winner.ogg']);
}

var player;
var playerTwo;
var platforms;
var cursors; 
var presents;
var score = 0; 
var playerTwoScore = 0; 
var scoreText; 
var scoreTextPlayerTwo; 

function create() {
    game.physics.startSystem(Phaser.Physics.ARCADE);

    //  Background
    game.add.sprite(0, 0, 'starrySky');

    //  Contains all the platforms 
    platforms = game.add.group();

    platforms.enableBody = true;

    // The ground
    var ground = platforms.create(0, game.world.height - 64, 'ground');

    ground.scale.setTo(2, 2);

    //  So you don't fall through the ground... 
    ground.body.immovable = true;

    //center bottom platform 
    var ledge = platforms.create(193, 500, 'bottomCenterIcyPlatform');
    ledge.body.immovable = true;

    //center middle platform
    var ledge = platforms.create(274, 320, 'middleCenterIcyPlatform');
    ledge.body.immovable = true;

    //center top platform
    var ledge = platforms.create(390, 170, 'topCenterIcyPlatform');
    ledge.body.immovable = true;

    //left bottom platform
    ledge = platforms.create(0, 410, 'bottomIcyPlatform');
    ledge.body.immovable = true;

    //right bottom platform
    ledge = platforms.create(824, 410, 'bottomIcyPlatform');
    ledge.body.immovable = true;

    //left side middle platform 
    ledge = platforms.create(0, 250, 'middleIcyPlatform');
    ledge.body.immovable = true;

    //right side middle platform 
    ledge = platforms.create(874, 250, 'middleIcyPlatform');
    ledge.body.immovable = true;

    //very top middle icy platform
    ledge = platforms.create(490, 50, 'veryTopMiddleIcyPlatform');
    ledge.body.immovable = true;

    //very top left icy platform
    ledge = platforms.create(200, 110, 'veryTopSideIcyPlatform');
    ledge.body.immovable = true;

    //very top right icy platform
    ledge = platforms.create(722, 110, 'veryTopSideIcyPlatform');
    ledge.body.immovable = true;

    // The players and their settings
    player = game.add.sprite(10, game.world.height - 150, 'dude');
    playerTwo = game.add.sprite(980, game.world.height - 150, 'dudeTwo');
    game.physics.arcade.enable(player);
    game.physics.arcade.enable(playerTwo);
    //playerOne
    player.body.bounce.y = 0.2;
    player.body.gravity.y = 300;
    player.body.collideWorldBounds = true;
    //PlayerTwo
    playerTwo.body.bounce.y = 0.2;
    playerTwo.body.gravity.y = 300;
    playerTwo.body.collideWorldBounds = true;

    //  Our two animations, walking left and right.
    player.animations.add('left', [0, 1, 2, 3], 10, true);
    player.animations.add('right', [5, 6, 7, 8], 10, true);
    //Player Two
    playerTwo.animations.add('left', [0, 1, 2, 3], 10, true);
    playerTwo.animations.add('right', [5, 6, 7, 8], 10, true);

    //  Finally some presents to collect
    presents = game.add.group();

    //  We will enable physics for any present that is created in this group
    presents.enableBody = true;

    //Create presents ground level
   for (var i = 0; i < 13; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+197, 500, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }
    //Create presents bottom center ledge
    for (var i = 0; i < 13; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+197, 400, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }
    //Create presents center middle ledge
    for (var i = 0; i < 9; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+298, 200, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create presents center top ledge
    for (var i = 0; i < 5; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+394, 100, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create present very top center ledge 
    for (var i = 1; i < 2; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+440, 18, 'present');
    }

    //Create presents left bottom ledge
    for (var i = 0; i < 4; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+10, 300, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create presents left middle ledge
    for (var i = 0; i < 3; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+9, 200, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create presents left top ledge
    for (var i = 0; i < 2; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+210, 50, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create presents right bottom ledge
    for (var i = 0; i < 4; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+834, 300, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create presents right middle ledge
    for (var i = 0; i < 3; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+884, 200, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //Create presents right top ledge
    for (var i = 0; i < 2; i++)
    {
        //  Create a present inside of the 'presents' group
        var present = presents.create((i * 50)+731, 50, 'present');

        //  Let gravity do its thing
        present.body.gravity.y = 300;

        //  This just gives each present a slightly random bounce value
        present.body.bounce.y = 0.3;
    }

    //  The scores
    scoreText = game.add.text(25, 16, 'Player 1 Score: 0', { fontSize: '32px', fill: '#000' });
    scoreTextPlayerTwo = game.add.text(750, 16, 'Player 2 Score: 0', { fontSize: '32px', fill: '#000' });

    //  Player controls
    cursors = game.input.keyboard.createCursorKeys();
    //player two controls
    upButton = game.input.keyboard.addKey(Phaser.Keyboard.W);
    downButton = game.input.keyboard.addKey(Phaser.Keyboard.S);
    leftButton = game.input.keyboard.addKey(Phaser.Keyboard.A);
    rightButton = game.input.keyboard.addKey(Phaser.Keyboard.D);


}

function update() {

    //  Collide the players against each other and the presents and platforms
    game.physics.arcade.collide(player, platforms);
    game.physics.arcade.collide(playerTwo, platforms);
    game.physics.arcade.collide(presents, platforms);
    game.physics.arcade.collide(player, playerTwo);

    //  Checks to see if the players overlaps with any of the presents, if he does call the collectpresent function
    game.physics.arcade.overlap(player, presents, collectpresent, null, this);
    game.physics.arcade.overlap(playerTwo, presents, playerTwoCollectpresent, null, this);


    //  Reset the players one velocity (movement)
    playerTwo.body.velocity.x = 0;

    if (cursors.left.isDown)
    {
        //  Move to the left
        playerTwo.body.velocity.x = -300;

        playerTwo.animations.play('left');
    }
    else if (cursors.right.isDown)
    {
        //  Move to the right
        playerTwo.body.velocity.x = 300;

        playerTwo.animations.play('right');
    }
    else
    {
        //  Stand still
        playerTwo.animations.stop();

        playerTwo.frame = 4;
    }

    //  Allow the player to jump if they are touching the ground.
    if (cursors.up.isDown && playerTwo.body.touching.down)
    {
        playerTwo.body.velocity.y = -310;
    }

    //Reset Player Two velocity (movement)
    player.body.velocity.x = 0;

    if (leftButton.isDown)
    {
        //  Move to the left (- to go faster, )
        player.body.velocity.x = -300;

        player.animations.play('left');
    }
    else if (rightButton.isDown)
    {
        //  Move to the right (+ to go faster)
        player.body.velocity.x = 300;

        player.animations.play('right');
    }
    else
    {
        //  Stand still
        player.animations.stop();

        player.frame = 4;
    }

    //  Have the player jump if they're touching something (ground, present, ledge or another player)
    if (upButton.isDown && player.body.touching.down)
    {
        player.body.velocity.y = -350;
    }

function collectpresent (player, present) {
    // Removes the present from the screen
    present.kill();

    //  Add and update the score
    score += 100;
    scoreText.text = 'Player 1 Score: ' + score; 
    }

function playerTwoCollectpresent (playerTwo, present) {
    // Removes the present from the screen
    present.kill();

    //  Add and update the score
    playerTwoScore += 100;
    scoreTextPlayerTwo.text = 'Player 2 Score: ' + playerTwoScore; 
    }

function detectWin () {
    if (score + playerTwoScore == 5900 && score > playerTwoScore) {
        alert("Player 1 Wins!");
    }
}

detectWin();


//end update
}

1 个答案:

答案 0 :(得分:1)

每次更新迭代都会调用detectWin函数,因此如果该函数内的条件被解析为true,它将执行其内部的任何内容(本例中的警报函数)。因此,要么在调用一次之后将此条件设为false,方法是添加var&#34; finisehd&#34;,例如:

function detectWin () {
    if (score + playerTwoScore == 5900 && score > playerTwoScore && !finished) {
        alert("Player 1 Wins!");
        finished = true;
    }
}

或者您可以通过更改为可以管理游戏结束的其他状态(http://phaser.io/news/2015/06/using-states-tutorial)来停止更新操作。