
Welcome Guest
|
|
|
#1
|
|||
|
|||
|
create selection rectangle
I need to create a selection rectangle on the stage that works as such:
1. Mouse down on stage activates mouse move 2. As the mouse moves, a rectangle is drawn 3. Mouse up highlights all objects that are within the boundaries of the rectangle I have parts 1 and 2 working fine, but have been unable to think of an effective way to do part 3. All I can think of is to loop through all items on the stage and check if their positions are within the boundaries of the selection rectangle, but that seems really inefficient to me. Does anyone know a better way to do this? Thanks in advance. |
|
#2
|
||||
|
||||
|
I don't think there is any easy way to do it. There is nifty getObjectsUnderPoint method, but that doesn't help you. You can use the methods of the Rectangle class to help your determine if an objects x and y are within your selection area. Specifically the contains method. Or you can use the getBounds method to get the bounding rectangle for the object and then pass it off to the containsRect method.
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius Blog | Shared Items |
|
#3
|
|||
|
|||
|
Okay...I'm not quite sure what you mean regarding either of those options, but I'll look into it.
I thought about the hitTestObject method, but I don't think that helps me much either. |
|
#4
|
|||
|
|||
|
A bit late, but thought I'd share my solution. I added ENTER_FRAME listeners for every object and in the handler, I check if there was a collision with hitTestObject. It works...not sure of a better solution.
|
|
#5
|
||||
|
||||
|
That's not a good option for a couple of reasons:
You only want to highlight items once you release the mouse. Here's a quick example. (You need some items on stage to test): Actionscript:
var glow:GlowFilter = new GlowFilter(0xFFFF00, 1, 16, 16, 5);
var rect:Rectangle = new Rectangle();
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(event:MouseEvent):void {
rect.left = mouseX;
rect.top = mouseY;
trace(rect);
}
function mouseUpHandler(event:MouseEvent):void {
rect.right = mouseX;
rect.bottom = mouseY;
trace(rect);
highlight();
}
function highlight():void {
var max:int = this.numChildren;
for(var i:int = 0 ; i < max; i++) {
var displayObject:DisplayObject = this.getChildAt(i);
var bounds:Rectangle = displayObject.getBounds(this);
var contained:Boolean = rect.containsRect(bounds);
if(contained) {
trace(displayObject.name, "is in rect");
displayObject.filters = [glow];
}
}
}
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius Blog | Shared Items |
|
#6
|
|||
|
|||
|
Yeah, I definitely agree with your points, especially the first one. I hated to do it but couldn't think of another way. I made it so I wouldn't "forget" to turn on the listener...all my objects are added at runtime and are custom classes, so their parent class turns on the listener.
Anyway, thanks a lot, Scottae. I'll try it out, but one question...my selection utility is actually a class that extends Sprite and the rectangle is drawn with drawRect....I see the containsRect method works with two rectangles in mind. How do I account for that with my set up? |
|
#7
|
|||
|
|||
|
That's good to hear, hopefully the process isn't too painful.
__________________
Removed by FM moderators. |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Flash Component creating rectangle at runtime | heartykalp | Flash 8 |
1 | 01-04-2008 03:46 PM |
| Mini Map Selection Box | paulcybulski | Actionscript 2.0 |
0 | 11-30-2007 04:52 PM |
| rectangle with grid/scale issues | hardcorey | Newbies |
0 | 01-23-2007 12:49 AM |
| How difficult to create Flash games like these? | Shazbot | Newbies |
2 | 04-11-2005 12:26 AM |
| Create Custom Multimedia Apps and Players with New Slick TV Studio | FlashJester Sup | 3rd Party Tools and Plugins | 0 | 04-02-2004 01:54 AM |




