Skip to main content
Participant
February 5, 2015
Answered

Can i create a function to tell a button to open a movie clip of the same name

  • February 5, 2015
  • 1 reply
  • 470 views

I am new to the actionscript side of flash,

I am working on a map that has say 20 popups(movieclips) and the countries are the buttons, i have just been informed i need to add 60 more.

Below is an example of the code i have been using

english_movie.visible=french_movie.visible=turkish_movie.visible=false 

english_btn
.addEventListener(MouseEvent.CLICK, englishButtonClick);
french_btn
.addEventListener(MouseEvent.CLICK, frenchButtonClick);
turkish_btn
.addEventListener(MouseEvent.CLICK, turkishButtonClick) 

function englishButtonClick(event:MouseEvent😞void {
     english_movie
.visible=true;

     english_movie.play();     
french_movie
.visible=turkish_movie.visible=false } 


function frenchButtonClick(event:MouseEvent😞void {

     french_movie.visible=true;          
     french_movie
.play();

english_movie.visible=turkish_movie.visible=false } 


function turkishButtonClick(event:MouseEvent😞void {

     turkish_movie.visible=true;

     turkish_movie.play();
english_movie
.visible=french_movie.visible=false }

Im thinking there must be an easier way to do this than replicating the code over and over.

Any help would be much appreciated.

This topic has been closed for replies.
Correct answer kglad

:


var tl:MovieClip=this;

var mc:MovieClip;

var i:int;

var buttonA:Array=[english_btn,french_btn,turkish_btn,...];


for(i=0;i<buttonA.length;i++){

buttonA.addEventListener(MouseEvent.CLICK,buttonF);

tl[buttonA.name.split("_")[0]+"_movie"].visible = false;

}

function buttonF(e:MouseEvent):void{

for(i=0;i<buttonA.length;i++){

tl[buttonA.name.split("_")[0]+"_movie"].visible = false;

}

tl[e.currentTarget.name.split("_")[0]+"_movie"].visible=true;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 5, 2015

:


var tl:MovieClip=this;

var mc:MovieClip;

var i:int;

var buttonA:Array=[english_btn,french_btn,turkish_btn,...];


for(i=0;i<buttonA.length;i++){

buttonA.addEventListener(MouseEvent.CLICK,buttonF);

tl[buttonA.name.split("_")[0]+"_movie"].visible = false;

}

function buttonF(e:MouseEvent):void{

for(i=0;i<buttonA.length;i++){

tl[buttonA.name.split("_")[0]+"_movie"].visible = false;

}

tl[e.currentTarget.name.split("_")[0]+"_movie"].visible=true;

}

Participant
February 6, 2015

Hi thank you so much for taking your time to help me with this , im getting an error

TypeError: Error #1006: addEventListener is not a function.

kglad
Community Expert
Community Expert
February 6, 2015

oops, that should be:

for(i=0;i<buttonA.length;i++){

buttonA.addEventListener(MouseEvent.CLICK,buttonF);

tl[buttonA.name.split("_")[0]+"_movie"].visible = false;

}