角速度

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

效果

源代码

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('arrow', 'images/phaser/arrow.png');
    }

    var sprite;

    function create() {

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

        game.stage.backgroundColor = '#0072bc';

        sprite = game.add.sprite(300, 200, 'arrow');
        sprite.anchor.setTo(0.5, 0.5);

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

    }

    function update() {

        sprite.body.velocity.x = 0;
        sprite.body.velocity.y = 0;
        sprite.body.angularVelocity = 0;

        // 按键盘“向左”逆时针旋转
        if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
            sprite.body.angularVelocity = -200;
        }
        // 按键盘“向右”顺时针旋转
        else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) {
            sprite.body.angularVelocity = 200;
        }
        // 按键盘“向上”前进
        if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) {
            game.physics.arcade.velocityFromAngle(sprite.angle, 300, sprite.body.velocity);
        }

    }

    function render() {

        game.debug.spriteInfo(sprite, 32, 32);
        game.debug.text('角速度: ' + sprite.body.angularVelocity, 32, 200);
        game.debug.text('角加速度: ' + sprite.body.angularAcceleration, 32, 232);
        game.debug.text('angularDrag: ' + sprite.body.angularDrag, 32, 264);
        game.debug.text('deltaZ: ' + sprite.body.deltaZ(), 32, 296);

    }
}

发布时间:2016/8/27 下午9:04:20  阅读次数:4076

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

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号