碰撞边界事件

原文地址:http://www.phaser.io/examples/v2/arcade-physics/world-bounds-event。

效果

源代码

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

    function preload() {

        game.load.image('space', 'images/phaser/space3.png');
        game.load.spritesheet('face', 'images/phaser/metalface78x92.png', 78, 92);
    }

    function create() {

        game.add.sprite(0, 0, 'space');

        var face = game.add.sprite(0, 0, 'face');

        //  石人眨眼的动画
        face.animations.add('flash', [0, 1, 2, 3, 2, 1, 0], 24, false);

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

        game.physics.arcade.enable(face);

        face.body.velocity.setTo(200, 200);
        face.body.bounce.set(1);

        face.body.collideWorldBounds = true;

        //  默认Signal为空,需要手动创建
        face.body.onWorldBounds = new Phaser.Signal();

        //  然后监听这个事件
        face.body.onWorldBounds.add(hitWorldBounds, this);

    }

    function hitWorldBounds(sprite) {

        //  播放眨眼动画
        sprite.play('flash');

    }
}

发布时间:2016/10/1 15:58:04  阅读次数:4292

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

沪ICP备18037240号-1

沪公网安备 31011002002865号