A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] trouble adding event listeners in an array

  1. #1
    Member
    Join Date
    Sep 2003
    Location
    canada
    Posts
    70

    resolved [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<=numDragsi++) {
        
        
    //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_DOWNmoveDrop);
        
    this[dragName].addEventListener(MouseEvent.MOUSE_UPstopMove);
        
    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();
    }
        


  2. #2
    Member
    Join Date
    Sep 2003
    Location
    canada
    Posts
    70
    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:

    Code:
    drag1.startDrag();
    it works fine. So that is the trouble area - how does flash know which object to drag?

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe try this...

    Code:
    function moveDrop(e:MouseEvent):void {
            e.currentTarget.startDrag();
    }

  4. #4
    Member
    Join Date
    Sep 2003
    Location
    canada
    Posts
    70

    Thumbs up

    dawsonk = genius!

    thanks - works like a charm.

  5. #5
    Senior Member
    Join Date
    Aug 2006
    Posts
    322
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center