How to correctly control one MC with another - custom listeners, classes or strings?

This is a discussion on "How to correctly control one MC with another - custom listeners, classes or strings?" within the Flash & Multimedia Forum section. This forum, and the thread "How to correctly control one MC with another - custom listeners, classes or strings? are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Flash & Multimedia Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 25th, 2007, 19:04
New Member
Join Date: Nov 2007
Location: B'ham, AL, USA
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
How to correctly control one MC with another - custom listeners, classes or strings?

I run into this all the time, so I assume it's a common and quite basic functionality people use - I'm still not sure how to approach it correctly.

Say I have a large number of MovieClips acting as controller buttons, placed in an array: btns[]. I have a second array of controlled MovieClips that should be activated when the corresponding button is pressed: mcs[]. I don't want to make a custom listener for each clip - instead, I have one listener function that correctly plays whatever MC is assigned to it.

Now, I want to simply loop through the array of buttons and assign each button a MouseEvent.CLICK listener that would activate the correct clip. Originally I just wanted to pass the index of the array I am on to the listener, so the MC with the corresponding index would be affected. But of course, you can't pass parameters to a listener...

So, what's the best way to get this done? I can think of three:

1) Create a custom event that would pass the index. Haven't tried and have no idea how to do this.

2) Create a custom class that has two MCs as members, the controller and the controlled, and a function that plays the controlled clip. Then simply add the eventListener in the constructor of the class.

3) Name each controller and controlled MC in a predetermined way. Then use strings to activate the correct the correct controlled MC.

I've been using method 2, but I get the feeling that I am simply missing something and there must be a much easier way to accomplish this. Any suggestions?
Reply With Quote

  #2 (permalink)  
Old Dec 29th, 2007, 03:42
New Member
Join Date: Dec 2007
Location: Maryland
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to correctly control one MC with another - custom listeners, classes or strin

I've done this before. Using AS 3.0, you would add a event listener for the MC. For example:

Code: Select all
myMovieName.addEventListener(MouseEvent.MOUSE_DOWN, buttonPressed);
Now you'll add a function to handle mouse down events, all of them.

Code: Select all
function buttonPressed(event:MouseEvent):void
{
   var thisButton = event.target;
   
   switch(getQualifiedClassName(thisButton)){
     case "button1":
        //Do this if this button is pressed
        break;
     case "button2":
        //Do this if button 2 is pressed
        break;
     default:
        trace("Button not identified....Houston, we have a problem.");
        break;
   }
}
With that you only need the provide a loop to apply the listener to the button and only need one function to address what to do when a button is pressed.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old Dec 29th, 2007, 07:41
New Member
Join Date: Nov 2007
Location: B'ham, AL, USA
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to correctly control one MC with another - custom listeners, classes or strin

Thanks! I got a few other tips in another forum; I'll post them here for future reference. Seems like there is no one "right" way of doing this:

Ok, the other forum seems to be down for some reason, so here's what I remember:

1) Create a variable in each MC and set it to that ms's index in the array. So, if you have an array of button MCs and an array of animated MCs, for each one do something like:

Code: Select all
buttons[0]=exampleMC;
exampleMC.indexVar = 0;
Now, you can loop through the array and assign listeners:

Code: Select all
for (i=0; i<=buttons.length; i++) 
    buttons[i].addEventListener(MouseEvents.CLICK, playMC);

function playMC(ev:MouseEvent):void{
    movies[ev.target.indexVar].play();
}
Hope I got that right.

2) Declare a custom class with a helper function. The class has two variables, Btn and MC, and a function with both vars passed as parameters. When the function is called, it assigns a listener to Btn that animates the MC. Sorry - I'm too lazy to write the code out now...

For me, option 1 ended up being easiest to implement; not sure which is actually "better".
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Classes tox0tes Flash & Multimedia Forum 1 Nov 9th, 2007 14:12
Classes in PHP pas2gether PHP Forum 11 Oct 13th, 2007 17:24
Using listeners with dynamic textBoxes DrGonzo Flash & Multimedia Forum 0 Aug 8th, 2007 10:24
Classes pa007 PHP Forum 6 Jul 12th, 2007 15:22
Classes Gee Bee JavaScript Forum 1 Mar 8th, 2006 08:31


All times are GMT. The time now is 01:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43