# How to get accurate drop position in AS3

**URL:** <https://forum.kirupa.com/t/how-to-get-accurate-drop-position-in-as3/329624>\
**Category:** flash\
**Created:** [September 24, 2012, 8:17am UTC](https://forum.kirupa.com/t/how-to-get-accurate-drop-position-in-as3/329624 "2012-09-24T08:17:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![arjun1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@arjun1](https://forum.kirupa.com/u/arjun1)\
**Post date:** [September 24, 2012, 8:17am UTC](https://forum.kirupa.com/t/how-to-get-accurate-drop-position-in-as3/329624/1 "2012-09-24T08:17:44Z")

</div>

[COLOR=#333333][FONT=Segoe UI]\<mx:Script\>[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]\<![CDATA[[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]import mx.containers.Canvas;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]import mx.controls.Image;[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]private var images:Array;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var images1:Array;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private const IMAGE\_COUNT:uint = 5;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var img:Image;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var img1:Image;[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]private var points:flash.geom.Point;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var offset\_x:int;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var offset\_y:int;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var canal:Canvas;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var doDrag:Boolean;[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI][Embed(source=‘cards/2C.png’)][/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var Image0:Class;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI][Embed(source=‘cards/2D.png’)][/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var Image1:Class;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI][Embed(source=‘cards/2H.png’)][/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var Image2:Class;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI][Embed(source=‘cards/2S.png’)][/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var Image3:Class;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI][Embed(source=‘cards/3C.png’)][/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private var Image4:Class;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]public function carcan():void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal = new Canvas();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setStyle(“backgroundColor”,“blue”);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.x=100;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.y=50;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.width=500;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.height=400;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]this.addChild(canal);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]init();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]public function init():void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]images = new Array(IMAGE\_COUNT);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]for (var i:int = 0; i \< IMAGE\_COUNT; i++)[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img= new Image();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img1= new Image();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]images\* = this[“Image” + i];[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]trace(images\*);[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]img.x=(i_30)+50;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.source=images_;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.id=“Image”+i;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.addChild(img);[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_DOWN, md);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_MOVE, mm);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.addEventListener(MouseEvent.MOUSE\_OUT,smu);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_UP, mu);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]public function smu(event:MouseEvent):void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.alpha=1;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.stopDrag();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]doDrag=false;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]public function mo(event:MouseEvent):void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]if(doDrag==true)[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_DOWN, md);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_UP, mu);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.stopDrag();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.alpha=1;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.removeEventListener(MouseEvent.MOUSE\_MOVE, mm);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]else[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_MOVE, mm);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]} [/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]public function md(event:MouseEvent):void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img = new Image();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]doDrag=true;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(Image(event.target),images.len gth-1);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_MOVE, mm);[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]public function mm(event:MouseEvent):void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]if(doDrag==true)[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]points = new Point();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]images = new Array(IMAGE\_COUNT);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img = new Image();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img = Image(event.target);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]points.x=event.target.x;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]points.y=event.target.y;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]points = localToGlobal(points);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.x=points.x;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.y=points.y;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.alpha=0.7;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.addEventListener(MouseEvent.MOUSE\_UP, mu); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]var boundar:flash.geom.Rectangle = new Rectangle(this.x, this.y, 250, 100);[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]public function mu(event:MouseEvent):void[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]img.alpha=1;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.stopDrag();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]doDrag=false;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.stopDrag();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]doDrag=false;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]var current:Image = event.currentTarget as Image;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]var num1:int = current.x; [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]if(num1 \< 50){[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(current, images.length-5);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]current.y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]if(num1 \> 50 && num1 \< 80)[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(current, images.length-4);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]current.y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]if(num1 \> 80 && num1 \< 110){[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(current, images.length-3);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]current.y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]if(num1 \> 110 && num1 \< 140){[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(current, images.length-2);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]current.y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]if(num1 \> 140 && num1 \< 170){[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(current, images.length-2);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]current.y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]

[COLOR=#333333][FONT=Segoe UI]if(num1 \> 170){[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.setChildIndex(current, images.length-1);[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]current.y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]setCards(); [/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]private function setCards():void{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]var b:int = 0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]var a:int;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]var cardsArray:Array = canal.getChildren();[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]for(a = 0;a \< cardsArray.length; a++)[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]{[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.getChildAt(a).x = 50+b;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]b=b+30;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]canal.getChildAt(a).y=0;[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]}[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]]]\>[/FONT][/COLOR]  
[COLOR=#333333][FONT=Segoe UI]\</mx:Script\> [/FONT][/COLOR]
