简体   繁体   English

移相器—如何检查精灵是否在移动?

[英]Phaser — How to check if sprite is moving?

I am trying to check in my update() function if a ball sprite has stopped moving after being thrown. 我正在尝试检查我的update()函数是否抛出了一个球精灵。 I tried the following: 我尝试了以下方法:

if (ball.body.velocity.x < 1 && ball.body.velocity.y < 1) {
  alert("ball not moving");
}

But the alert is being fired even when both conditions don't evaluate to true. 但是,即使两个条件都不为真,也会发出警报。 (Meaning the ball is still moving...") (意思是球还在移动……”)

Here is my code. 这是我的代码。

game.js game.js

window.onload = function() {

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

    function preload() {
        game.load.image('table', 'assets/img/table.png');
        game.load.image('cup', 'assets/img/cup.png');
        game.load.image('ball', 'assets/img/ball.png');
    }

    var table;
    var cups;
    var cup;
    var ball;
    var ballZ = 100;
    var ballAscending = false;
    var ballThrown = false;

    function create() {

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

        table = game.add.sprite(game.world.centerX, game.world.centerY, 'table');
        table.anchor.setTo(0.5,0.5);

        var cupW = game.cache.getImage('cup').width;
        var cupH = game.cache.getImage('cup').height;

        cups = game.add.group(game,game.world,'cups',false,true,Phaser.Physics.ARCADE);

        cup = cups.create(game.world.centerX, cupH / 2, 'cup');
        cup = cups.create(game.world.centerX - cupW, cupH / 2, 'cup');
        cup = cups.create(game.world.centerX + cupW, cupH / 2, 'cup');
        cup = cups.create(game.world.centerX - cupW / 2, cupH + (cupH / 2), 'cup');
        cup = cups.create(game.world.centerX + cupW / 2, cupH + (cupH / 2), 'cup');
        cup = cups.create(game.world.centerX, (cupH * 2) + (cupH / 2), 'cup');

        cup = cups.create(game.world.centerX, game.world.height - (cupH / 2), 'cup');
        cup = cups.create(game.world.centerX - cupW, game.world.height - (cupH / 2), 'cup');
        cup = cups.create(game.world.centerX + cupW, game.world.height - (cupH / 2), 'cup');
        cup = cups.create(game.world.centerX - cupW / 2, game.world.height - (cupH / 2) - cupH, 'cup');
        cup = cups.create(game.world.centerX + cupW / 2, game.world.height - (cupH / 2) - cupH, 'cup');
        cup = cups.create(game.world.centerX, game.world.height - (cupH / 2) - (cupH * 2), 'cup');

        ball = game.add.sprite(game.world.centerX, game.world.centerY + (cupH*4),'ball');
        ball.anchor.setTo(0.5,0.5);
        ball.inputEnabled = true;

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

        cups.forEach(function(item) {
            item.anchor.setTo(0.5);
            item.body.immovable = true;
        },this);

        ball.body.bounce.set(0.50);
        ball.body.drag.set(100);

        game.stage.backgroundColor = "#d3d3d3";

        game.input.onDown.add(onDown,this);
        game.input.onUp.add(throwBall,this);

    }

    var clickTime;

    function onDown() {
        clickTime = game.time.time;
    }

    function throwBall() {
        var delta = game.time.time - clickTime;
        game.physics.arcade.velocityFromAngle(ball.angle, delta, ball.body.velocity);
        ballThrown = true;      
    }

    function update() {

        if (ballThrown) {

            game.physics.arcade.collide(cups,ball);

            if (ballAscending) {
                ballZ = ballZ + 1;
                if (ballZ > 100) {
                    ballAscending = false;
                }
            } else {
                ballZ = ballZ - 1;
                if (ballZ < 1) {
                    ballAscending = true;
                }
            }

            ball.scale.set((ballZ + 100) / 100);

            if (ball.body.velocity.x < 1 && ball.body.velocity.y < 1) {
                alert("ball stopped moving");
            }
        }

        ball.rotation = game.physics.arcade.angleToPointer(ball);

    }

    function render() {
        game.debug.spriteInfo(ball,32,32);
    }

}

index.html 的index.html

<!DOCTYPE html>

<html lang="en">
    <meta charset="UTF-8" />
    <head>
        <link rel="stylesheet" type="text/css" href="stylesheet.css">
    </head>
<body>

<script src="js/phaser.min.js"></script>
<script src="js/game.js"></script>
</body>
</html>

Sorry I'm four months late, but you can use the Phaser.Point "equals" method to check if the body's velocity equals a point of (0,0) 抱歉,我迟到了四个月,但是您可以使用Phaser.Point“等于”方法来检查人体的速度是否等于(0,0)

if (Phaser.Point.equals(ball.body.velocity,new Phaser.Point(0,0) ) ){
    console.log("You ain't goin' anywhere!");
}

I found the problem, my if statements conditions should be 我发现了问题,我的if语句条件应该是

    if (Math.abs(ball.body.velocity.x) < 1 && Math.abs(ball.body.velocity.y) < 1) {
         alert("ball stopped moving");
    }

This works, but if anyone knows a cleaner way to accomplish this, feel free to share. 这行得通,但是如果有人知道更干净的方法来实现此目的,请随时分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM