Skip navigation
witherton
Currently Being Moderated

Why does Event.ENTER_FRAME cause this code to stop working? Is there a better alternative?

Jun 28, 2012 3:28 AM

I programmed the following code so that an array of buttons

can be dragged across the screen when either on :

 

var buttons:Array=[button1, button2, button3];

 

for (var i:uint=0; i<buttons.length; i++)

buttons[i].addEventListener(MouseEvent.CLICK, alignButtons )

 

function alignButtons (e:Event)

{

     for (var i2:uint=0; i2<buttons.length; i2++)

      if(buttons[i2]!=e.target){buttons[i2].x=e.target.x;}

}

 

     for (var i3:uint=0; i3<buttons.length; i3++)

     buttons[i3].addEventListener(MouseEvent.MOUSE_DOWN, buttonDrag);

     function buttonDrag (e:Event)

{

    e.target.startDrag();

   

    e.target.addEventListener(MouseEvent.MOUSE_UP, buttonDrop);

    function buttonDrop(e:Event)

          {

          e.target.stopDrag();    

          }

}

 

It works pretty much how I'd like it to work, except for the fact that I have to click the mouse to update the screen. However, when I change the first event listener from "MouseEvent.CLICK" to "Event.ENTER_FRAME" the code goes haywire. Is there another event listener I should be using here?

 
Replies
  • Currently Being Moderated
    Jun 28, 2012 4:53 AM   in reply to witherton

    Due to what appears to be an incomplete set of curly braces, it is hard to tell how much nesting you have there, but there should be none.  Each listener and event handler function should be able to stand on its own.  You should not have to put any functions inside a loop.

     

    Having the CLICK event listener is redundant if you have a MOUSE_DOWN and a MOUSE_UP because a MOUSE_DOWN followed by a MOUSE_UP is a CLICK. 

     

    If your goal is to have the buttons all follow each other around x-wise, then what you can do is assign a MOUSE_MOVE event listener at the same time you execute the startDrag and use its event handler for adjust button positions.  When you stopDrag, you also remove the MOUSE_MOVE listener.

     

    As far as why things go haywire when you change to using an ENTER_FRAME, you should show the code when it is in that form so that whatever error is evident.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 28, 2012 9:15 AM   in reply to witherton

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points