Phaser: Sprite.{x,y} vs Sprite.position.{x,y} What should I use?

Created on 20 Sep 2013  ·  2Comments  ·  Source: photonstorm/phaser

Hi Richard!

Suppose the following code:

function update() {
    // game's world size = 800 x 600
    // At the begining sprite is located at (400,300)
    if ( player.position.x < -player.width ) {
      player.position.x = game.width
    } 

    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) {
      if ( 0 == player.velocity.x ) {
        player.velocity.x = -200;
        player.animations.play('walk');
      }
    }
  }

It simply moves the sprite to the left side, and when it reaches the border it will appear on the right side then. Unfortunatelly this won't happen until I change player.position.x by player.x:

function update() {
    // game's world size = 800 x 600
    // At the begining sprite is located at (400,300)
    if ( player.x < -player.width ) {
      player.x = game.width
    }
   ...
  }

So my question is, what is the correct attribute I should use? Sprite.{x, y} or Sprite.position.{x,y}?

Thanks!

🤷‍♂️ More info needed

Most helpful comment

You should use Sprite.x/y as it's the only value that includes in the camera and scrollFactor.

All 2 comments

You should use Sprite.x/y as it's the only value that includes in the camera and scrollFactor.

Thanks Richard!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rootasjey picture rootasjey  ·  3Comments

samme picture samme  ·  3Comments

HDouss picture HDouss  ·  3Comments

lilijreey picture lilijreey  ·  4Comments

JarLowrey picture JarLowrey  ·  4Comments