Jump to content

How to update position of drawRect


SYNYST3R1
 Share

Recommended Posts

I am drawing a rectangle around the tile my mouse is over, but it draws a new rectangle every time I go over a new tile. How could I make it update the position of the rectangle instead of drawing a new rectangle every time?

if(msX >= tileX * tileWidth && msX <= (tileX + 1) * tileWidth && msY >= tileY * tileHeight && msY <= (tileY + 1) * tileHeight) {			var g = game.add.graphics(0, 0);			g.lineStyle(2, 0x000000, 1);			g.drawRect(tileX * tileWidth, tileY * tileHeight, tileWidth, tileHeight);		}
Link to comment
Share on other sites

Just draw the rectangle once, at the size you need it (your tileWidth, tileHeight values) and then position the graphics object where the mouse is. If you're using the current dev branch you can just do:

g.x = game.input.x;g.y = game.input.y;

Have a look at the examples/tilemaps/paint tile demo for a good example of doing just this.

 

If you're on the current master branch then I'm pretty sure you can still do it, but just use:

g.position.x

instead.

Link to comment
Share on other sites

I'm on the latest dev branch. I put g.y in the console log and the position is definitely updating, but the rectangle isn't moving.

var g;function create() {    g = game.add.graphics();    g.lineStyle(2, 0x000000, 1);    g.drawRect(0, 0, tileWidth, tileHeight);}function update() {   g.x = tileX*tileWidth;   g.y = tileY*tileHeight;   console.log(g.y);}

That's basically the short version of what I have

Link to comment
Share on other sites

  • 5 months later...
  • 5 months later...
 Share

  • Recently Browsing   0 members

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