碰撞反弹

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

效果

源代码

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('dude', 'images/phaser/phaser-dude.png');
            game.load.image('ball', 'images/phaser/pangball.png');

        }

        var image;

        function create() {

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

            cursors = game.input.keyboard.createCursorKeys();

            ball = game.add.sprite(400, 200, 'ball');

            knocker = game.add.sprite(400, 200, 'dude');

            game.physics.enable([knocker, ball], Phaser.Physics.ARCADE);

            knocker.body.immovable = true;

            //  设置球的初速度
            ball.body.velocity.setTo(200, 200);
            //  将小人和球限制在边界内
            ball.body.collideWorldBounds = true;
            knocker.body.collideWorldBounds = true;

            ball.body.bounce.setTo(1, 1);

        }

        //  键盘控制小人的运动
        function update() {

            //  开启小人和球之间的碰撞效果
            game.physics.arcade.collide(knocker, ball);

            if (cursors.up.isDown) {
                knocker.body.velocity.y = -300;
            }
            else if (cursors.down.isDown) {
                knocker.body.velocity.y = 300;
            }
            else if (cursors.left.isDown) {
                knocker.body.velocity.x = -300;
            }
            else if (cursors.right.isDown) {
                knocker.body.velocity.x = 300;
            }
            else {
                knocker.body.velocity.setTo(0, 0);
            }
        }

        function render() {

            game.debug.spriteInfo(ball, 32, 32);
        }
}

发布时间:2016/9/1 下午1:49:02  阅读次数:5017

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

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号