Jump to content

How to update position of drawRect


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:



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...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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