发射

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

效果

源代码

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

    }

    var arrow;
    var ball;
    var catchFlag = false;
    var launchVelocity = 0;

    function create() {

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

        // 设置全局重力加速度
        game.physics.arcade.gravity.y = 200;
        game.stage.backgroundColor = '#0072bc';

        // 绘制竖直的杆
        var graphics = game.add.graphics(0, 0);
        graphics.beginFill(0x049e0c);
        graphics.drawRect(395, 350, 10, 250);

        analog = game.add.sprite(400, 350, 'analog');

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

        analog.body.allowGravity = false;
        analog.width = 8;
        analog.rotation = 220;
        analog.alpha = 0;
        analog.anchor.setTo(0.5, 0.0);

        arrow = game.add.sprite(400, 350, 'arrow');

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

        arrow.anchor.setTo(0.1, 0.5);
        arrow.body.moves = false;
        arrow.body.allowGravity = false;
        arrow.alpha = 0;

        ball = game.add.sprite(100, 400, 'ball');
        game.physics.enable(ball, Phaser.Physics.ARCADE);
        ball.anchor.setTo(0.5, 0.5);
        ball.body.collideWorldBounds = true;
        ball.body.bounce.setTo(0.9, 0.9);

        // 球可以处理交互事件
        ball.inputEnabled = true;
        ball.input.start(0, true);
        ball.events.onInputDown.add(set);
        ball.events.onInputUp.add(launch);

    }

    function set(ball, pointer) {

        ball.body.moves = false;
        ball.body.velocity.setTo(0, 0);
        ball.body.allowGravity = false;
        catchFlag = true;

    }

    function launch() {

        catchFlag = false;

        ball.body.moves = true;
        arrow.alpha = 0;
        analog.alpha = 0;
        Xvector = (arrow.x - ball.x) * 3;
        Yvector = (arrow.y - ball.y) * 3;
        ball.body.allowGravity = true;
        ball.body.velocity.setTo(Xvector, Yvector);

    }

    function update() {

        arrow.rotation = game.physics.arcade.angleBetween(arrow, ball);

        if (catchFlag == true) {
            //  若球被抓住,则随光标移动  
            ball.x = game.input.activePointer.worldX;
            ball.y = game.input.activePointer.worldY;

            arrow.alpha = 1;
            analog.alpha = 0.5;
            analog.rotation = arrow.rotation - 3.14 / 2;
            analog.height = game.physics.arcade.distanceToPointer(arrow);
            launchVelocity = analog.height;
        }

    }

    function render() {

        game.debug.text("拖拽球然后释放就可以发射", 32, 32);

        game.debug.bodyInfo(ball, 32, 64);

    }
}

发布时间:2016/9/15 20:58:37  阅读次数:4040

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

沪ICP备18037240号-1

沪公网安备 31011002002865号