00:00
00:00
Newgrounds Background Image Theme

TheADHX just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Draw a rectangle from it center

879 Views | 2 Replies
New Topic Respond to this Topic

Draw a rectangle from it center 2016-01-08 19:48:02


I have this code:

import flash.geom.Rectangle;

var DBx:Number = 320; 
var DBy:Number = 240; 
var DBwidth:Number = 150; 
var DBheight:Number = 150; 
var _DB:Sprite = new Sprite();

addChild(_DB);
_DB.graphics.lineStyle(5, 0xFFFFFF)
_DB.graphics.drawRect(DBx, DBy, DBwidth, DBheight);

But it got drawed from the upper-left corner, i searched and couldn't found how to draw from it's center point.
Any ideas?


At 1/8/16 07:48 PM, KolkeLight wrote: I have this code:

import flash.geom.Rectangle;

var DBx:Number = 320;
var DBy:Number = 240;
var DBwidth:Number = 150;
var DBheight:Number = 150;
var _DB:Sprite = new Sprite();

addChild(_DB);
_DB.graphics.lineStyle(5, 0xFFFFFF)
_DB.graphics.drawRect(DBx, DBy, DBwidth, DBheight);

But it got drawed from the upper-left corner, i searched and couldn't found how to draw from it's center point.
Any ideas?

We're gonna need some more details, to be honest. is DBx and DBy intended to be the center of your circle?

If that's the case, then you need to do a touch of math. I don't know Actionscript, but I can safely assume its rectangle class works like any other language. The X and Y you feed it are always your top-left corner. If you want DBx and DBy to be your center, you need to make them the center. Try this:

: _DB.graphics.drawRect( (DBx - ( DBwidth/2 ) ) , (DBy - (DBheight / 2) ), DBwidth, DBheight);

To be honest, I'm a bit too lazy to check if this is correct right now. Also, keep in mind that even numbers will always land slightly "off-center" because there isn't a true center (draw a 10x10 rectangle and find the center pixel - you'll see what I mean :p )

EDIT: Also, if you're going to extend this to something like, "Click somewhere on the screen to draw a rectangle there", you'll want to do some validation to make sure you aren't drawing half of the rectangle off-screen. Unless you want that.


Thanks!

With this

_DB.graphics.drawRect(-75, -75, 150, 150);

The "rectangle" got drawn from the center, then i can move it everywhere changing the x & y.

Problem solved :)