LOADING
Loading
Hi , welcome back.
LogoutLOGOUT
 
  Lost password?  
Hi
 


  #1  
Old 02-23-2010, 12:32 PM
skitzer skitzer is offline
Registered User
 
Join Date: Oct 2006
Posts: 166
Rep Power: 7
skitzer is on a distinguished road
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.
Reply With Quote
  #2  
Old 02-23-2010, 01:11 PM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: United States
Posts: 6,532
Rep Power: 16
Scottae is on a distinguished road
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
Reply With Quote
  #3  
Old 02-23-2010, 01:29 PM
skitzer skitzer is offline
Registered User
 
Join Date: Oct 2006
Posts: 166
Rep Power: 7
skitzer is on a distinguished road
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.
Reply With Quote
  #4  
Old 03-05-2010, 04:12 PM
skitzer skitzer is offline
Registered User
 
Join Date: Oct 2006
Posts: 166
Rep Power: 7
skitzer is on a distinguished road
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.
Reply With Quote
  #5  
Old 03-08-2010, 11:53 AM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: United States
Posts: 6,532
Rep Power: 16
Scottae is on a distinguished road
That's not a good option for a couple of reasons:
  1. Having enter frame performing a hittest on all objects is going to throttle the CPU. Maybe not with one or two items, but having many doing it will.
  2. You have to put that enter frame on all objects. That is a hassle to keep up with. What happens if you forget?

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
Reply With Quote
  #6  
Old 03-09-2010, 12:23 PM
skitzer skitzer is offline
Registered User
 
Join Date: Oct 2006
Posts: 166
Rep Power: 7
skitzer is on a distinguished road
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?
Reply With Quote
  #7  
Old 03-11-2011, 09:17 AM
Annagyijjk Annagyijjk is offline
Registered User
 
Join Date: Mar 2011
Location: Andorra
Posts: 3
Rep Power: 0
Annagyijjk is on a distinguished road
That's good to hear, hopefully the process isn't too painful.
__________________
Removed by FM moderators.
Reply With Quote


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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




All times are GMT. The time now is 08:46 AM.