单向碰撞检测

原文地址:http://www.phaser.io/examples/v2/arcade-physics/one-way-collision。

效果

源代码

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

    function preload() {

        game.load.spritesheet('gameboy', 'images/phaser/gameboy_seize_color_40x60.png', 40, 60);
        game.load.image('atari', 'images/phaser/atari130xe.png');

    }

    var sprite;
    var sprite2;
    var sprite3;

    function create() {

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

        game.stage.backgroundColor = '#124184';

        //	本例中,我们设置了忽略sprite的顶/底的碰撞,
        //  因此Gameboy的图片在竖直方向上不与Atari图片发生碰撞

        sprite = game.add.sprite(200, 150, 'atari');
        sprite.name = 'atari';
        game.physics.enable(sprite, Phaser.Physics.ARCADE);
        sprite.body.collideWorldBounds = true;
        sprite.body.checkCollision.up = false;
        sprite.body.checkCollision.down = false;
        sprite.body.immovable = true;

        sprite2 = game.add.sprite(250, 350, 'gameboy', 2);
        sprite2.name = 'gameboy';

        game.physics.enable(sprite2, Phaser.Physics.ARCADE);
        sprite2.body.collideWorldBounds = true;
        sprite2.body.bounce.setTo(1, 1);

        sprite3 = game.add.sprite(0, 160, 'gameboy', 4);

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

        sprite3.name = 'gameboy2';
        sprite3.body.collideWorldBounds = true;
        sprite3.body.bounce.setTo(1, 1);

        sprite2.body.velocity.y = -200;
        sprite3.body.velocity.x = 200;

    }

    function update() {

        game.physics.arcade.collide(sprite, sprite2);
        game.physics.arcade.collide(sprite, sprite3);

    }

    function render() {

        game.debug.bodyInfo(sprite, 16, 24);

        game.debug.body(sprite);
        game.debug.body(sprite2);

    }
}

发布时间:2016/9/10 下午3:47:28  阅读次数:4239

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

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号