actionscript-3flashactionscriptadobe-animate

how to get instance name on object boolean which value is true


i may have gone further than what i could because i just learned actionscript 3 days ago.

I'm making a simple quiz where I made the answer choices manually, not using the components provided by Adobe Animate. because I can decorate the appearance however I want.

I made movieclip button named answer1 and answer2, then to check if the answer was selected I added a boolean.

e.currentTarget.selected = true;
e.currentTarget.selected = false;

this is my code for the answer button

function clicked(e:MouseEvent): void {
    if (e.currentTarget == answer1) {
        if (e.currentTarget.currentFrame == 1 ) {
        e.currentTarget.nextFrame(); // hover/active button
        e.currentTarget.selected = true;
        } else {
        e.currentTarget.prevFrame(); // hover/active button
        e.currentTarget.selected = false;
        }
        answer2.selected = false;
        answer2.prevFrame();
    } else if (e.currentTarget == answer2) {
        if (e.currentTarget.currentFrame == 1 ) {
        e.currentTarget.nextFrame(); // hover/active button
        e.currentTarget.selected = true;
        } else {
        e.currentTarget.prevFrame(); // hover/active button
        e.currentTarget.selected = false;
        }
        answer1.selected = false;
        answer1.prevFrame();
    }
}

then I made a button to check the answer. but until here I don't know how to check the object that has been selected

function checkAnswer(e: MouseEvent): void {
trace(answer1.selected.name)
}

i want only how can i get the instance name of the boolean object which has the value true.


Solution

  • BTW, if you intend these selectors work as radiogroup (a group of radiobuttons that lets only one of them be selected as the same time), you'd better devise it that way:

    function clicked(e:MouseEvent): void
    {
        // Define a radiogroup.
        var aGroup:Array = [answer1, answer2];
        
        // Iterate over the group elements and process the click.
        for each (var anAnswer:MovieClip in aGroup)
        {
            if (anAnswer == e.currentTarget)
            {
                anAnswer.selected = !anAnswer.selected;
            }
            else
            {
                anAnswer.selected = false;
            }
            
            // Adjust the current frame according to "selected" value.
            if (anAnswer.selected)
            {
                anAnswer.nextFrame();
            }
            else
            {
                anAnswer.prevFrame();
            }
        }
    }
    

    Why? First, it is still shorter than yours even at 2 options. Second, think about adding more than 2 options, like, 10 of them? My code will only need extending the Array of options, yours will get 5 times longer than now.