September 11th, 2008 by Kyle
Tags: ActionScript, as3, drag, drag select, Flex
Posted in: ActionScript, Flex
I have a few samples I am working on to demonstrate how to do some “drag selection” and I thought some of the base code I use in these samples would be good to post on its own.
So here it is. Pretty basic, but it will serve as the basis for a few samples that I should have out next week (hopefully).
Download a zipfile containing the source to this sample.
Browse the source of this example.
Or continue into the blog entry to see the source:
Here is the app code:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*">
<MyCanvas borderStyle="solid" backgroundColor="white" backgroundAlpha=".5"
width="400" height="300"/>
</mx:Application>
Here is the custom component code:
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
mouseMove="onMouseMove(event)" mouseDown="onMouseDown(event)"
mouseOut="onMouseOut(event)" mouseUp="onMouseUp(event)">
<mx:Script>
<![CDATA[
import flash.events.MouseEvent;
import mx.core.UIComponent;
private var startMouseX:int;
private var startMouseY:int;
private var isMouseDown:Boolean=false;
private function onMouseDown(event:MouseEvent):void{
isMouseDown=true;
startMouseX=mouseX;
startMouseY=mouseY;
}
private function onMouseUp(event:MouseEvent):void{
isMouseDown=false;
graphics.clear();
}
private function onMouseMove(event:MouseEvent):void{
if (isMouseDown){
graphics.clear();
graphics.beginFill( 0xCCCCCC );
graphics.drawRect( startMouseX, startMouseY, event.localX – startMouseX, event.localY – startMouseY );
}
}
private function onMouseOut(event:MouseEvent):void{
isMouseDown=false;
dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
}
]]>
</mx:Script>
</mx:Canvas>
2 Comments »











October 4th, 2008 at 9:32 am
Hey..
any more further examples using this ’simple’ drag selection code as your promised?
I was wondering how do you actually select the items using this code. for eg: in a TileList?
Cheers
Andy
January 28th, 2010 at 2:35 pm
Year+ later than the last comment — but I would be interested in more on this as well. Same type of application — selecting a number of chosen items. Cntl-click to deselect one of many items selected, would be great too.
Thanks