Last active
March 13, 2023 11:53
-
-
Save mhulse/9bc032ec55cf92ba9c17 to your computer and use it in GitHub Desktop.
AS3: Drag object and detect what’s under the mouse during move.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.*; | |
import flash.events.*; | |
public class Main extends MovieClip { | |
// Constructor: | |
public function Main() { | |
(this.stage) ? init() : this.addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true); | |
} | |
// Begin program: | |
private function init($event:Event = null):void { | |
if ($event !== null) { | |
this.removeEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
// End boilerplate ^^^^^^^^^^^ | |
// Make sure the draggable object is on top of the target object: | |
drag.parent.setChildIndex(drag, drag.parent.numChildren - 1); | |
// Begin the good stuff vvvvvvvvvvvvv | |
// Setup mouse events: | |
drag.addEventListener(MouseEvent.MOUSE_DOWN, dragStart, false, 0, true); | |
// Note that "drag" is MC on stage with instance name of "drag". | |
// All you really need to do is copy the below methods and | |
// copy the above line, but change `drag` to desired instance. | |
} | |
// We’re starting our drag: | |
private function dragStart($event:MouseEvent):void { | |
var target = $event.target; | |
var parent = target.parent.stage; // Using `parent` because `this.stage` already exists. | |
var that = this; | |
// Disable mouse interactions for this object: | |
target.mouseEnabled = false; | |
// Disable mouse interactions for this object’s children: | |
target.mouseChildren = false; | |
// Begin drag: | |
target.startDrag(); | |
// Listen for mouse up on the parent’s stage: | |
parent.addEventListener(MouseEvent.MOUSE_UP, function($e:MouseEvent) { | |
that.dragStop(target, parent); | |
}, false, 0, true); | |
// Detect for mouse movement when dragging: | |
parent.addEventListener(MouseEvent.MOUSE_MOVE, dragMove, false, 0, true); | |
} | |
// We’re drag moving: | |
private function dragMove($event:MouseEvent) { | |
// This tells us what we’re hovering over: | |
//trace('Currently hovering over:', $event.target.name); | |
// Are we hovering over a specific object? | |
if ($event.target.name) { | |
trace('Hovering over object:', $event.target.name); | |
} | |
} | |
// We’re done dragging: | |
function dragStop($target:MovieClip, $parent:DisplayObject):void { | |
// GC: | |
$parent.removeEventListener(MouseEvent.MOUSE_UP, dragStop); | |
$parent.removeEventListener(MouseEvent.MOUSE_MOVE, dragMove); | |
// End drag: | |
$target.stopDrag(); | |
// Enable mouse interactions for this object: | |
$target.mouseEnabled = true; | |
// Enable mouse interactions for this object’s children: | |
$target.mouseChildren = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: The
drag
reference is MC on stage with instance name of "drag".If adapting to your own code:
I made
dragStart()
,dragMove()
anddragStop()
generic so all you really need to do is copy those methods and add:… replace
drag
with object your are dragging