Jump to content

Set xy coordinate on arcade body?


rendall
 Share

Recommended Posts

How do I simply set an x, y coordinate on an arcade body?  Specifically, I'm trying to set the starting position of a player after the sprite is created and physics enabled, but before there is a physics update.  When I do so as below, the body ignores the new setting and opens with the sprite at the initial dummy coordinates.

 

I have arranged my code like this:

// Map inherits from Tilemap and creates all of the layersvar map = new Map(this.game, 'map'); // Player inherits from Sprite// 0, 0 is the initial coordinate// the constructor enables physics on the bodyvar player = new Player(this.game, 0, 0, 1);//  This is so that map can insert the player into the proper//  group index and location.map.addPlayer(player);
in Map I have:
function addPlayer(player){this.game.world.addAt(player, this.PLAYER_LAYER);player.body.moves = false; // <-- doesn't seem to matter whether this is set or notplayer.body.x = this.START_X;player.body.y = this.START_Y;player.body.reset(this.START_X,this.START_Y); // <-- doesn't matter whether I set it this way or not}

How can I move the sprite to the proper starting coordinates?

Link to comment
Share on other sites

Just use x and y of the player itselfs.

While the physical properties are stored within the body, the position of the body is stored on the sprite.

The body.x/y is more of a readonly-value which will give you the sprite.x/y.

 

http://docs.phaser.io/Phaser.Physics.Arcade.Body.html#position

/**    * @property {Phaser.Point} position - The position of the physics body.    * @readonly    */    this.position = new Phaser.Point(sprite.x, sprite.y);/*** @name Phaser.Physics.Arcade.Body#x* @property {number} x - The x position.*/Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "x", {    get: function () {        return this.position.x;    },    set: function (value) {        this.position.x = value;    }});/*** @name Phaser.Physics.Arcade.Body#y* @property {number} y - The y position.*/Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "y", {    get: function () {        return this.position.y;    },    set: function (value) {        this.position.y = value;    }});
Link to comment
Share on other sites

Thank you, marvster.  That did the trick.

 

I had actually tried and rejected that earlier, but there was a subsequent weird physics popping and I incorrectly assumed the solution was wrong.  It actually was because of an errant collision.  Your answer gave me the confidence to try again.

 

Thanks again!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...