title: Phaser -- How to check if sprite is moving?
body: <p>I am trying to check in my update() function if a ball sprite has stopped moving after being thrown.  I tried the following:</p>
  
  <pre><code>if (ball.body.velocity.x &lt; 1 &amp;&amp; ball.body.velocity.y &lt; 1) {
    alert("ball not moving");
  }
  </code></pre>
  
  <p>But the alert is being fired even when both conditions don't evaluate to true.  (Meaning the ball is still moving...")</p>
  
  <p>Here is my code.</p>
  
  <p><strong>game.js</strong></p>
  
  <pre><code>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 &gt; 100) {
                      ballAscending = false;
                  }
              } else {
                  ballZ = ballZ - 1;
                  if (ballZ &lt; 1) {
                      ballAscending = true;
                  }
              }
  
              ball.scale.set((ballZ + 100) / 100);
  
              if (ball.body.velocity.x &lt; 1 &amp;&amp; ball.body.velocity.y &lt; 1) {
                  alert("ball stopped moving");
              }
          }
  
          ball.rotation = game.physics.arcade.angleToPointer(ball);
  
      }
  
      function render() {
          game.debug.spriteInfo(ball,32,32);
      }
  
  }
  </code></pre>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html lang="en"&gt;
      &lt;meta charset="UTF-8" /&gt;
      &lt;head&gt;
          &lt;link rel="stylesheet" type="text/css" href="stylesheet.css"&gt;
      &lt;/head&gt;
  &lt;body&gt;
  
  &lt;script src="js/phaser.min.js"&gt;&lt;/script&gt;
  &lt;script src="js/game.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>