Drag object in ActionScript 3 - actionscript-3

How can I achieve this dragging effect? I am using ActionScript3.
http://l2on.net/swf/MapView2.swf

This looks a very standard drag drop functionality:-
You can use startDrag function at MouseDown and stopDrag at MouseUp event
like:
this.addEventListener(MouseEvent.MOUSE_DOWN, startMapDragging);
this.addEventListener(MouseEvent.MOUSE_UP, stopMapDragging);
function startMapDragging(e:MouseEvent) {
map.startDrag();
}
function stopMapDragging(e:MouseEvent) {
map.stopDrag();
}

Related

Click after mouse up outside stage

I'am drag game scene in Flash project (mouse down - start drag, mouse up - stop drag). If I mouse up outside stage, click on any object (buttons) don't work once. After one click other click works fine. What's wrong?
update: Trace logs shown that there event as mouseOver, mouseDown, mouseUp, mouseOut are dispatches, but not CLICK.
update: There is silencer of first click after drag in the project. It's necessary to eliminate situation of end drag on some game object (dispath click). Sorry. Thank you all for answers.
You might be losing focus when leaving the stage. Try using (Event.MOUSE_LEAVE) to 'force' a mouse_up.
something like this:
private var _draggedItem:Sprite;
myDisplayObject.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
private function mouseDownHandler(event:MouseEvent):void {
_draggedItem = event.currentTarget as Sprite;
_draggedItem.startDrag();
_draggedItem.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
stage.addEventListener(Event.MOUSE_LEAVE, stageMouseOutHandler);
}
private function stopDragCurrentItem():void {
if (_draggedItem) {
_draggedItem.stopDrag();
_draggedItem.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
if (stage) {
stage.removeEventListener(Event.MOUSE_LEAVE, stageMouseOutHandler);
}
_draggedItem = null;
}
}
private function mouseUpHandler(event:MouseEvent):void {
stopDragCurrentItem();
}
private function stageMouseOutHandler(event:Event):void {
trace("stage out!")
stopDragCurrentItem();
}
update:
And concerning the lost focus, you cold do the following in html where you embed your flash:
<object classid="..." codebase="...." width=550 height=400
name="myMovie" onmouseover="window.document.myMovie.focus();">
though i haven't tested it.

Stop Drag, Add Up Release for a separate function

I have a movie clip where I've placed start drag and stop drag actions, and I'd like to activate a button action (run a movie clip named person_mo) with the ReleaseToDrop function. However, I cannot code it correctly. Here is the AS I have for the drag:
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
SCOPE2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
SCOPE2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
SCOPE2.stopDrag();
}
How can I play the person_mo movie clip and stop the drag?
After I tested your code, your stopDrag function is working well. You just need to fire your event/function/ command when you fire the ReleaseToDrop function.
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
SCOPE2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
SCOPE2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
SCOPE2.stopDrag();
person_mo.play(); // if you want to run the movie clip
yourFunction(); //or else if you want a function to fire, simply add yourFunction which fires the button action.
}
I hope this helps.

Spawn MC addChild to stage but how to tie in the draggable function? AS3

I'm running into some problems with making the movie clip so I can drag it about the stage. The scenario: the user clicks a button that spawns the movie clip to the stage, from there they can move the item around the stage - it's a draggable object. I can make it so the movieClip spawns, I can make it so it drags... but i can't make it do both, once I try to attach the drag function. Any pointers please? I'm new to as3! :-)
spawner_btn.addEventListener(MouseEvent.CLICK, spawnspinkjewel);
function spawnspinkjewel(event:MouseEvent):void
{
var myChild:pink_jewel= new pink_jewel();
stage.addChild(myChild)
myChild.x=300;
myChild.y=150;
}
pink_jewel.addEventListener(MouseEvent.MOUSE_DOWN, pickupblack_pink_jewel);
pink_jewel.addEventListener(MouseEvent.MOUSE_UP, dropblack_pink_jewel);
function pickupblack_pink_jewel(event:MouseEvent):void {
event.target.startDrag(true);
}
function dropblack_pink_jewel(event:MouseEvent):void {
event.target.stopDrag();
}
You added the listeners to the incorrect object.
Change this:
pink_jewel.addEventListener(MouseEvent.MOUSE_DOWN, pickupblack_pink_jewel);
pink_jewel.addEventListener(MouseEvent.MOUSE_UP, dropblack_pink_jewel);
To this:
myChild.addEventListener(MouseEvent.MOUSE_DOWN, pickupblack_pink_jewel);
myChild.addEventListener(MouseEvent.MOUSE_UP, dropblack_pink_jewel);

Flash drag strange behavior

I have a fancy movie clip (with text and other movie clips inside it)
that I want to drag
I can only drag internal parts of the movie clip but not the whole movie clip.
My NOT working example:
http://www.internetmotors.lv/temp/testgame_5.swf
http://www.internetmotors.lv/temp/testgame_5.fla (cs5)
Code:
drag_1.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag);
function clickToDrag(event:MouseEvent):void
{
event.target.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
function releaseToDrop(event:MouseEvent):void
{
event.target.stopDrag();
}
answering my own question:
it seems i need to use
event.currentTarget.startDrag();
instead of:
event.target.startDrag();

Startdrag is not a function

this.addEventListener(MouseEvent.MOUSE_DOWN,function(e:MouseEvent){this.startDrag(false,null);});
Hi I was wondering why the above doesnt work? Im trying to drag a sprite around screen.
create a sprite on stage, add instance name box, add code to frame one:
box.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
function startMove(evt:MouseEvent):void {
box.startDrag();
}
box.addEventListener(MouseEvent.MOUSE_UP, stopMove);
function stopMove(e:MouseEvent):void {
box.stopDrag();
}
I think your example doesn't work because of the scope of "this" in the event listener handler.
If you remove this.; it will work. It's a scope issue since you use an anonymous function.
You could use the currentTarget of the event, this allows you to make other boxes draggable too, if you add the same listeners.
Note: It is hard to remove an anonymous function as event listener and could cause memory leaks, so the best way is to use a reference to a named function:
box.addEventListener(MouseEvent.MOUSE_DOWN, handleMouseEvent);
box.addEventListener(MouseEvent.MOUSE_UP, handleMouseEvent);
function handleMouseEvent(event:MouseEvent):void
{
switch(event.type)
{
case MouseEvent.MOUSE_DOWN:
{
DisplayObject(event.currentTarget).startDrag();
break;
}
case MouseEvent.MOUSE_UP:
{
DisplayObject(event.currentTarget).stopDrag();
break;
}
}
}