Author Topic: Making a selection rectangle  (Read 1144 times)

Gilda

  • Active Member
  • ***
  • Posts: 123
  • Karma: +0/-0
    • View Profile
Making a selection rectangle
« on: Sat, May 10, 2014 »
I'm trying to let the player drag out their mouse to making a rectangle similar to a lot of RTSs, I've got the logistics down but I can't see to figure out how to make it work graphically.

I've tried using an FlxSprite and playing with the scale, but it seems to act very oddly. It seems like the pivot is at the top left of the image, but it scales from the centre, very confusing.
I've also tried using FlxSpriteUtils to draw a rectangle, then using FlxSprite().replaceColor to remove it, but it's horrendously laggy.

Any insite would be very appreciated.

EDIT: Just realized I can just call makeGraphic over and over again, it seems a tad slow and I have to do some fancy maths to get it to do negative selections but it'll do unless there some easy method I'm missing.

After trial and error I've came up with this:

Code: [Select]
private function updateMouse():Void
{
if (FlxG.mouse.justPressed)
{
_selectionRect.x = FlxG.mouse.x;
_selectionRect.y = FlxG.mouse.y;
_selectionGraphic.x = _selectionRect.x;
_selectionGraphic.y = _selectionRect.y;

_selectionGraphic.visible = true;
}

if (FlxG.mouse.pressed)
{
_selectionRect.width = FlxG.mouse.x - _selectionRect.x;
_selectionRect.height = FlxG.mouse.y - _selectionRect.y;

var tempRect:Rectangle = new Rectangle();
tempRect.x = _selectionRect.x;
tempRect.y = _selectionRect.y;
tempRect.width = _selectionRect.width;
tempRect.height = _selectionRect.height;

if (tempRect.width < 0)
{
tempRect.x += _selectionRect.width;
tempRect.width = _selectionRect.width * -1;
}

if (tempRect.height < 0)
{
tempRect.y += _selectionRect.height;
tempRect.height = _selectionRect.height * -1;
}

_selectionGraphic.x = tempRect.x;
_selectionGraphic.y = tempRect.y;

if (tempRect.width == 0) tempRect.width = 1;
if (tempRect.height == 0) tempRect.height = 1;

_selectionGraphic.makeGraphic(Math.ceil(tempRect.width), Math.ceil(tempRect.height), 0xFFFFFFFF);
}

if (FlxG.mouse.justReleased)
{
selectUnits();
_selectionGraphic.visible = false;
}
}

Since sprites warn that they're going to run slow as hell if scaled this can't be all that bad of a solution.
« Last Edit: Sat, May 10, 2014 by Gilda »

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: Making a selection rectangle
« Reply #1 on: Sun, May 11, 2014 »
The reason why sprite scaling behaves this way is the default origin, which is in the center. You want to set it to (0, 0) when doing things like this.

replaceColor() is not a function you should be using on a per-frame basis, it loops over every single pixel in the BitmapData.

Not sure why you'd need replaceColor() with the FlxSpriteUtil approach? You could simply use drawRectangle() and fill() the sprite graphic with FlxColor.TRANSPARENT every frame.

Btw, you might want to use the HaxeFlixel forums for HaxeFlixel questions in the future.