垂直碰撞

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

效果

源代码

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

    function preload() {

        game.load.image('atari', 'images/phaser/atari130xe.png');
        game.load.image('mushroom', 'images/phaser/mushroom2.png');

    }

    var sprite1;
    var sprite2;

    function create() {

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

        game.stage.backgroundColor = '#2d2d2d';

        sprite1 = game.add.sprite(300, 50, 'atari');
        sprite1.name = 'atari';

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

        sprite1.body.velocity.y = 100;

        //  下面的代码调整碰撞体的大小.
        //  220x10是新的宽/高.
        sprite1.body.setSize(220, 10, 0, 0);

        sprite2 = game.add.sprite(400, 450, 'mushroom');
        sprite2.name = 'mushroom';

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

        sprite2.body.immovable = true;

    }

    function update() {

        game.physics.arcade.collide(sprite1, sprite2, collisionHandler, null, this);

    }

    function collisionHandler(obj1, obj2) {

        game.stage.backgroundColor = '#992d2d';

    }

    function render() {

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

    }
}

发布时间:2016/10/2 下午8:51:59  阅读次数:4573

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

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号