更大的包围盒

原文地址:http://www.phaser.io/examples/v2/arcade-physics/larger-bounding-box。

效果

源代码

window.onload = function () {
    var game = new Phaser.Game(600, 400, 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.stage.backgroundColor = '#2d2d2d';

        sprite1 = game.add.sprite(130, 200, 'atari');
        sprite1.name = 'atari';

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

        //  本例中包围盒要比sprite大
        sprite1.body.setSize(400, 50, -100, 20);
        sprite1.body.immovable = true;

        sprite2 = game.add.sprite(500, 210, 'mushroom');
        sprite2.name = 'mushroom';
        game.physics.enable(sprite2, Phaser.Physics.ARCADE);
        sprite2.body.velocity.x = -100;
    }

    function update() {

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

    }

    function collisionHandler(obj1, obj2) {

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

    }

    function render() {

        game.debug.bodyInfo(sprite2, 32, 32);

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

    }
}

发布时间:2016/9/6 21:21:10  阅读次数:3955

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

沪ICP备18037240号-1

沪公网安备 31011002002865号