0

I am trying to create multiple addEventListener but I don't know how. As you can see in the code below - I don't understand what I need to write where I wrote ???????? in order to produce multiple functions (such as onClick1,onClick2,onClick3, etc...)

for (i=0; i < numberOfResults; i++)
{
    videoResults[i] = new Object();
    videoResults[i].movie = new MovieClip();
    stage.addChild(videoResults[i].movie);
    videoResults[i].movie.addEventListener("click",?????????);
    function ?????????(event)
    {

    }

}

What do I need to do?

1
  • In this case I think you should explain what your end goal is (ie. why are you wanting to create all these listener functions) so that we can explain a better way of achieving that goal. Creating functions inside a loop is a bad idea, so we need a higher level understanding of what you're trying to do. Commented Jun 10, 2011 at 17:34

1 Answer 1

2

You don't want to write a function inside of a for loop. Do something like this:

for (i=0; i < numberOfResults; i++)
{
    videoResults[i] = new Object();
    videoResults[i].movie = new MovieClip();
    stage.addChild(videoResults[i].movie);
    videoResults[i].movie.addEventListener(MouseEvent.MOUSE_DOWN, myMadeUpCallbackEvent);    
}


function myMadeUpCallbackEvent(evt:MouseEvent):void
{
   //In order to be able to tell which clip has called this callback, you can compare the properties of evt.currentTarget. The evt is the Event object cast into a reference. evt.currentTarget is the target or object that called the event. So you can do something like this:
   trace(MovieClip(evt.currentTarget).name); to get the unique name of the caller
}

You may be interested in this free video tutorial website on flash:

http://gotoandlearn.com/

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot! but I still don't understand how to connect the dots.. let's say that if I click the Nth movie clip - I want to add this line: videoResults[N].isItclicked = true; what do I need to do and what am I missing here?
That question could call for a complicated answer and a lot of discussion here that would lead away from the original question topic. What I think would be best is for me to provide a link to a tutorial on how to create a video player with a playlist in flash AS3. Any new questions can be posted as independent questions here just to keep the questions and answers clear. republicofcode.com/tutorials/flash/as3xmlvideoplayer
Use a Dictionary instead of an Array to store the click results. A Dictionary can use any object (for example, a MovieClip) as the key, rather than just numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.