碰撞边界事件

原文地址: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 下午3:58:04  阅读次数:4341

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

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号