0

I'm new to using actionscript 3 and I was wondering if it was possible to simplify this code and create a loop of some sort, so I could easily replicate the function whenever I'm importing a new button.

The code is this:

videobutton.addEventListener(MouseEvent.CLICK, fl_OpenVideo);
gallerybutton.addEventListener(MouseEvent.CLICK, fl_OpenGallery);
contactbutton.addEventListener(MouseEvent.CLICK, fl_OpenContact);

function fl_OpenVideo(event:MouseEvent):void{
if (!(currentClip is Vids))
   {
       currentClip.play();
        var newClip:Vids = new Vids;
        addChild(newClip);
        newClip.play();
        newClip.y =140;
        currentClip =  newClip;
   }
}
function fl_OpenGallery(event:MouseEvent):void
{   
    if (!(currentClip is Gal))
   {
    currentClip.play();
    var newClip:Gal = new Gal;
    addChild(newClip);
    newClip.play();
    newClip.y =140;
    currentClip =  newClip; 
   }
}
function fl_OpenContact(event:MouseEvent):void
{   
    if (!(currentClip is Con))
   {
    currentClip.play();
    var newClip:Con = new Con;
    addChild(newClip);
    newClip.play();
    newClip.y =140;
    currentClip =  newClip; 
   }
}

1 Answer 1

1

You can do like that:

videobutton.addEventListener(MouseEvent.CLICK, fl_Open);
gallerybutton.addEventListener(MouseEvent.CLICK, fl_Open);
contactbutton.addEventListener(MouseEvent.CLICK, fl_Open);

const S:Sprite = new Sprite();
S.y = 140;
this.addChild(S);
var __last:*;

function fl_Open(event:MouseEvent):void
{
    while(S.numChildren > 0) S.removeChildAt(0);
    if(event.target == videobutton) __last = new Vids;
    if(event.target == gallerybutton) __last = new Gal;
    if(event.target == contactbutton) __last = new Con;
    __last.play();
    S.addChild(__last);
}
Sign up to request clarification or add additional context in comments.

Comments

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.