PDA

View Full Version : [AS3] drag drop woes-->pls help me



pixeldude
October 9th, 2008, 12:35 AM
Hi there...

I've been decide to go with OOP since on my thought my code will be more managable..and it's more easier to update later on....but since Im still new with AS3 so really need help and also need some clearify on my pitfall...hope OOP guru's here can help me out..or at least give me some shed on light...

1ST QUESTIONS
1.enough babbling ....my problem is my dragger object still stick to my mouse eventhough on Drop and I already removeEvent for the mouseEvent and also my dragger object dissapear...perplexing and clueless???..url below here are illustrating the problems I'm facing now..u guys can play with it..

http://www.franzacollections.net/fifin04/dragdrop/fla.html

this is the code Im currently using...
in fla..call "fla.fla"


stop();

var dragger:Array = new Array(d_1,d_2,d_3,d_4,d_5,d_6,d_7,d_8,d_9);
var target:Array = new Array(t_1,t_2,t_3,t_4,t_5,t_6,t_7,t_8,t_9);

var gameBegin:Main = new Main(dragger,target);


//document class "Main.as"


package{

import flash.display.Sprite;
import flash.events.Event;
import DragDrop;


public class Main extends Sprite{

private var startGame:DragDrop;
public var draggerObj:Array = new Array();
public var targetObj:Array = new Array();

//constructor for document class
public function Main(_dragObj:Array,_targetObj:Array){
draggerObj = _dragObj;
targetObj = _targetObj;
startGame = new DragDrop(draggerObj,targetObj);
startGame.addEventListener("dragEvent", onStartDrag);
addChild(startGame);
}

private function onStartDrag(event:Event):void{
trace ("Dragging status is: " + startGame.onDragStatus);
}
}

}




and here are my class.."DragDrop.as"


package{

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

public class DragDrop extends MovieClip{
private var len:Number;
public var dragObj:Array = new Array();
public var targetObj:Array = new Array();
private var startX:Number;
private var startY:Number;
private var _onDrag:Boolean = false;
//private var startTimer:Timer;
//constructor
public function DragDrop(_dragObj:Array,_tarObj:Array){
dragObj = _dragObj;
targetObj = _tarObj;
init();
}

private function init():void{
len = dragObj.length;
for(var i:int=0;i<len;i++){
dragObj[i].id = i;
dragObj[i].buttonMode = true;
startX = dragObj[i].x;
startY = dragObj[i].y;
dragObj[i].addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
dragObj[i].addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
}

private function onDrag(event:MouseEvent):void{
_onDrag = true;
trace("onDrag true");
event.target.addEventListener(MouseEvent.MOUSE_MOV E, MouseMove);
event.currentTarget.startDrag();
}

private function onDrop(event:MouseEvent):void{
_onDrag = false;
trace("onDrag false");
event.target.removeEventListener(MouseEvent.MOUSE_ UP, onDrop);
event.target.removeEventListener(MouseEvent.MOUSE_ MOVE, MouseMove);
event.currentTarget.stopDrag();
dispatchEvent(new Event("dragEvent"));
//////portions checking the right target
for(var j:int = 0;j<targetObj.length;j++){
if(event.currentTarget.hitTestObject(targetObj[j])){
event.currentTarget.x = targetObj[j].x;
event.currentTarget.y = targetObj[j].y;
}else{
trace("wrong target");
event.currentTarget.x = startX;
event.currentTarget.y = startY;
}
}//end for
}

private function MouseMove(event:MouseEvent):void{
event.updateAfterEvent();
}
//just for tracing dragging actions purposing only
public function get onDragStatus():Boolean {
return _onDrag;
}

}//end class
}


So I really need tips or maybe some advice on how to go about it I mean the proper ways ...since Im still neww with AS3...at least point me where my mistake exactly...

here's the source

http://www.franzacollections.net/fifin04/dragdrop/dragdropwoes.rar

2ND QUESTIONS
2.How to collect movir properties on root in AS3 since in previous version I just initerate roots...like so..



for(var i in _root){
trace(_root[i]._x+":"+_root[i]._y);
}

or someMC as backgroud

for(var i in someMC){
trace(someMC[i]._x+":"+someMC[i]._y);
}



How to accomplish it in AS3 through document class...so that I can push in array easily....

ANy help are really appreciate...thanks again

aBnest
October 9th, 2008, 02:00 AM
for your dragging issue, try adding the mouseUp event to the stage instead of the object.

pixeldude
October 9th, 2008, 02:21 AM
for your dragging issue, try adding the mouseUp event to the stage instead of the object.
Okay..then how to determine whether I've drag the exact object if Im adding mouseVent stage....coz what I have now I can easily check the current objest dragging....
like so..

trace(event.target.id);

correct me if Im wrong....???

aBnest
October 9th, 2008, 02:29 AM
you are absolutely right, but consider the fact that if you release the mouseButton when the cursor is not over the object you are dragging, the mouseUp event won't trigger.
In this scenario what i would do is have a private variable on witch I would store the reference to the dragging object.
you can set the reference on mouseDown and the use it when the mouseUp event from stag triggers.

brndn
October 9th, 2008, 03:22 AM
i dont think your hit test is working either.

or(var j:int = 0;j<targetObj.length;j++){
if(event.currentTarget.hitTestObject(targetObj[j])){
event.currentTarget.x = targetObj[j].x;
event.currentTarget.y = targetObj[j].y;
}else{
trace("wrong target");
event.currentTarget.x = startX;
event.currentTarget.y = startY;
}


that is going to create an if statment for all targetObj's regardless of the event.currentTarget

unless I am wrong

pixeldude
October 9th, 2008, 07:55 PM
you are absolutely right, but consider the fact that if you release the mouseButton when the cursor is not over the object you are dragging, the mouseUp event won't trigger.
In this scenario what i would do is have a private variable on witch I would store the reference to the dragging object.
you can set the reference on mouseDown and the use it when the mouseUp event from stag triggers.

Erm....U mean I have to set referrences in mousedown ....frankly speaking still confuse actually...can u slightly show a simple sample just for this portion in order to illustrate for what are u trying to explain.....I'm missing my sleep over this zzzzzzzzzz...tq

brndn -->Thanks for the point me brndn...didnt notice it..no wonder I even cant match the exact dragger and droptarget......

Tq again...

brndn
October 9th, 2008, 09:19 PM
hey there
I am currently working on a more accurate method for targeting hit objects.
I will post the result when I get there. just need to tweak my loops and it should be all good.
share the love


brndn

pixeldude
October 10th, 2008, 12:18 AM
hey there
I am currently working on a more accurate method for targeting hit objects.
I will post the result when I get there. just need to tweak my loops and it should be all good.
share the love


brndn

Sounds interesting.....I will look forward and for the time beings I will digging mine too..:)

aBnest
October 10th, 2008, 11:24 AM
I attached a sample code. Hope it will help you.

pixeldude
October 12th, 2008, 02:20 AM
I attached a sample code. Hope it will help you.

Thanks...for ur sample mate...i will have take a look at it and will update here...Tq again mate..