-
[RESOLVED] trouble adding event listeners in an array
I am trying to create a drag and drop activity, and I would like to use an array for easy reusability.
I'm new to as3 so I've been adjusting my old as2 code. I am using a component for each of my drags and most of it is working well.
My problem is with the event handlers. I use the array to populate all of my 'drags' with the information added in their parameters and this works, but when I create my event listeners it seems to only want to move the last item in the array. Can anyone help?
My drag components are called drag1, drag2, and drag3.
Here is my code (it's far from complete, but i'd like this issue resolved before building the rest of the activity):
PHP Code:
var numDrags:Number = new Number(3);
for (var i:int = 1; i <=numDrags; i++) {
//change the instance names to variables
var dragName:String = new String("");
this.dragName = "drag" + i;
var $dropped:Boolean = new Boolean(false);
this[dragName].$dropped = false;
//grab the component drag text and populate the text field for each drag
this[dragName].drag_text.text = this[dragName].__text;
//find out the target for each drag
var $target:Number = new Number();
this[dragName].$target = this[dragName].__target;
//find the starting co-ordinates of the drags
var $originX:Number = new Number(0);
var $originY:Number = new Number(0);
this[dragName].$originX = this[dragName].x;
this[dragName].$originY = this[dragName].y;
//add my event listeners
this[dragName].addEventListener(MouseEvent.MOUSE_DOWN, moveDrop);
this[dragName].addEventListener(MouseEvent.MOUSE_UP, stopMove);
this[dragName].buttonMode = true;
this[dragName].mouseChildren = false;
//start dragging
function moveDrop(e:MouseEvent):void {
this[dragName].startDrag();
}
//stop the drag
function stopMove(e:Event):void {
this[dragName].stopDrag();
}
}
-
I've realized I was wrong about where the trouble is. All the eventListeners are loading, the problem is that it's not determined which drag should be moved.
when I changed
Code:
this[dragName].startDrag();
to a specific instance name, like:
it works fine. So that is the trouble area - how does flash know which object to drag?
-
Maybe try this...
Code:
function moveDrop(e:MouseEvent):void {
e.currentTarget.startDrag();
}
-
dawsonk = genius!
thanks - works like a charm.
-
He is always a genius.
Nice to see you again Dawsonk, after a long time.
marlopax
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|