user972043
user972043

Reputation: 11

AS3 many buttons with boolean function - less verbose?

I have twenty eight instances of a two-frame MovieClip (frame1 = off - frame 2 = on) to select PDFs to send. The following code works fine, but I am looking to tighten it up and make it less verbose and easier to read. I include only one reference to an instance for space and sanity sake.

function PDFClick(e:MouseEvent):void {
    targetPDF = e.target.ID;
    trace("targetPDF " +targetPDF);

    if (targetPDF == "PDF1")
    if (pdf.pcconnectionPDF1.currentFrame == 1)
    {
        pdf.pcconnectionPDF1.gotoAndPlay(2);
        PDF1 = 1;
        trace("PDF1 is "+PDF1);
    }else{
        pdf.pcconnectionPDF1.gotoAndPlay(1);
        PDF1 = 0;
        trace("PDF1 is "+PDF1);
    }

Thanks! trying to learn

Upvotes: 0

Views: 61

Answers (2)

Atriace
Atriace

Reputation: 2558

You'll want to generalize your calls to your ID, that way you don't need special code for each condition.

function PDFClick(e:MouseEvent):void {
    var ID:String = e.target.ID;
    var mc = pdf["pcconnection" + ID];

    if (mc.currentframe == 1) {
        mc.gotoAndPlay(2);
        this[ID] = 1;
    } else {
        mc.gotoAndPlay(1);
        this[ID] = 0;
    }
}

Upvotes: 1

Bennett Keller
Bennett Keller

Reputation: 1714

How about this:

function PDFClick(e:MouseEvent):void {
    targetPDF = e.target.ID;
    trace("targetPDF " +targetPDF);

    if (targetPDF == "PDF1") {
        var frame:int = pdf.pconnectionPDF1.currentFrame;
        pdf.pconnectionPDF1.gotoAndPlay( frame == 1 ? (PDF1 = 1)+1 : (PDF1 = 0)+1 );
    }
}

I think that's about what you are looking for.

Upvotes: 0

Related Questions