移动指定距离

原文地址: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 21:09:07  阅读次数:4068

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

沪ICP备18037240号-1

沪公网安备 31011002002865号