全局暂停

原文地址:http://www.phaser.io/examples/v2/arcade-physics/global-pause。

效果

源代码

window.onload = function () {
    var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser_container', 
                            preload: preload, create: create, update: update });

    function preload() {

        game.load.image('car', 'images/phaser/car90.png');
        game.load.image('baddie', 'images/phaser/space-baddie.png');

    }

    var car;
    var aliens;
    var cursors;
    var spaceKey;

    function create() {

        game.physics.startSystem(Phaser.Physics.ARCADE);

        aliens = game.add.group();
        aliens.enableBody = true;

        for (var i = 0; i < 50; i++) {
            var s = aliens.create(game.world.randomX, game.world.randomY, 'baddie');
            s.name = 'alien' + s;
            s.body.collideWorldBounds = true;
            s.body.bounce.setTo(0.8, 0.8);
            s.body.velocity.setTo(10 + Math.random() * 40, 10 + Math.random() * 40);
        }

        car = game.add.sprite(400, 300, 'car');
        car.name = 'car';
        car.anchor.set(0.5);

        game.physics.enable(car, Phaser.Physics.ARCADE);

        car.body.collideWorldBounds = true;
        car.body.bounce.set(0.8);
        car.body.allowRotation = true;
        car.body.immovable = true;

        cursors = game.input.keyboard.createCursorKeys();

        spaceKey = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        spaceKey.onDown.add(togglePause, this);

    }

    function togglePause() {

        // 按下空格键暂停
        game.physics.arcade.isPaused = (game.physics.arcade.isPaused) ? false : true;

    }

    function update() {

        game.physics.arcade.collide(car, aliens);

        car.body.velocity.x = 0;
        car.body.velocity.y = 0;
        car.body.angularVelocity = 0;

        if (cursors.left.isDown) {
            car.body.angularVelocity = -200;
        }
        else if (cursors.right.isDown) {
            car.body.angularVelocity = 200;
        }

        if (cursors.up.isDown) {
            car.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(car.angle, 300));
        }

    }
}

发布时间:2016/10/6 20:54:09  阅读次数:3805

2006 - 2023,推荐分辨率1024*768以上,推荐浏览器Chrome、Edge等现代浏览器,截止2021年12月5日的访问次数:1872万9823 站长邮箱

沪ICP备18037240号-1

沪公网安备 31011002002865号