1

I have three functions that I have listed in an array. Now I need a random function of the three to be called when pressing a button. However, when I press the button it calls all three functions and I'm not quite sure where I've gone wrong. It looks like this right now:

function Arm1function1(){
this.parent.parent.parent.Armfront1.visible = true;
this.parent.parent.parent.Armback1.visible = false;
}

function Arm1function2(){
this.parent.parent.parent.Armfront1.visible = false;
this.parent.parent.parent.Armback1.visible = true;
}

function Arm1function3(){
this.parent.parent.parent.Armfront1.visible = false;
this.parent.parent.parent.Armback1.visible = false;
}

function getRandomElementOf(Armbuttonarray1:Array):Object {
var Armbuttonarray1:Array = [Arm1function1(), Arm1function2(), Arm1function3()];
var idx:int=Math.floor(Math.random() * Armbuttonarray1.length);
return Armbuttonarray1[idx];
}

Randombutton1part1.addEventListener(MouseEvent.CLICK, Randombutton1part1Click);
function Randombutton1part1Click(e:MouseEvent):void
{   
getRandomElementOf(null);

}

Any clue of where I've gone wrong?

2
  • I see you already have an answer to your question, but please consider revising your code for the sanity of whoever has to maintain it. Instead of manipulating objects on the ancestor, simply dispatch one of three bubbling events and let the ancestor make changes. Also, consider that you could maybe make a four frame mc where the appropriate arm states are showing and just navigate to frame one, two, three, or for (or use labels that match the event names) based on which one you need. Commented Aug 31, 2015 at 15:11
  • What I have now is working (and I'm working completely on my own, I'm the only one who have to deal with the code) so I'm fine with it! Having it on different frames was my main idea, it's what I do with my other mc's, but the colortransform did not work properly when doing that. Hence that I had to solve it with visibility false/true. Commented Sep 1, 2015 at 20:38

1 Answer 1

2

Your issue is this line:

var Armbuttonarray1:Array = [Arm1function1(), Arm1function2(), Arm1function3()];

When populating that array, you are actually populating it with the results of the functions.

Should be:

var Armbuttonarray1:Array = [Arm1function1, Arm1function2, Arm1function3];

Notice the lack of parenthesis ().

You want to actually execute the function on the click handler, so you'll need to tweak that a bit too:

getRandomElementOf(null)();

or

getRandomElementOf(null).call();

As an aside, your getRandomElementOf function should probably look more like this:

function getRandomElementOf(array:Array):Object {
    return array[Math.floor(Math.random() * array.length)];
}

Then do:

getRandomElementOf([Arm1function1, Arm1function2, Arm1function3])();
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see! Thanks for your help. The second option is certainly helpful as I need to create the same thing three more times with other arrays of functions!

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.