移动指定距离

原文地址:http://www.phaser.io/examples/v2/arcade-physics/move-over-distance。

效果

源代码

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

    function preload() {

        game.load.image('clown', 'images/phaser/clown.png');
        game.load.image('block', 'images/phaser/block.png');

    }

    var sprite;
    var block;
    var startTime = 0;
    var endTime = 0;
    var duration = 0;

    function create() {

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

        sprite = game.add.sprite(200, 300, 'clown');
        block = game.add.sprite(600, 280, 'block');

        game.physics.arcade.enable(sprite);
        game.physics.arcade.enable(block);

        sprite.body.bounce.set(1);
        sprite.body.collideWorldBounds = true;

        block.body.immovable = true;

        sprite.body.onMoveComplete.add(moveOver, this);

        game.input.onDown.addOnce(move, this);

    }

    function move() {

        //  小丑在2000毫秒内向右移动300像素
        sprite.body.moveTo(2000, 300, Phaser.ANGLE_RIGHT);

        startTime = game.time.time;
        duration = 0;

    }

    function moveOver() {

        endTime = game.time.time;
        duration = endTime - startTime;

    }

    function update() {

        game.physics.arcade.collide(sprite, block);

    }

    function render() {

        game.debug.text("expire: " + sprite.body.moveTimer, 32, 32);
        game.debug.text("vx: " + sprite.body.velocity.x, 300, 32);
        game.debug.text("vy: " + sprite.body.velocity.y, 600, 32);
        game.debug.text("duration: " + duration, 32, 64);
        game.debug.text("m: " + sprite.body.isMoving, 300, 64);

    }
}

发布时间:2016/9/17 下午9:09:07  阅读次数:4510

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

沪 ICP 备 18037240 号-1

沪公网安备 31011002002865 号