Skip to main content
Inspiring
June 16, 2010
Answered

how to choose movieclip name dynamically

  • June 16, 2010
  • 1 reply
  • 620 views

I hv 5 movieclips on stage mc1,mc2,mc3,mc4,mc5. I m trying this code to run random movieclip on stage

function Randvalue()
{
var randVal=Math.round(Math.random()*4)
this[mc+randVal].gotoAndPlay("over")
}

Randvalue()

showing error 1120: Access of undefined property mc.

what is the right way to choose movieclip dynamically in as3??

This topic has been closed for replies.
Correct answer Ned Murphy

You need to use quotes around "mc" and for good measure you should convert randVal to a String ( as in String(randVal)  )

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
June 16, 2010

You need to use quotes around "mc" and for good measure you should convert randVal to a String ( as in String(randVal)  )

Inspiring
June 16, 2010

again showing error

1137: Incorrect number of arguments.  Expected no more than 0.

Ned Murphy
Legend
June 16, 2010

I don't see where that code would have that error.  Can you show what you did?  Is there other code in the file?  AS3 often sends out errors in sequence, meaning once you complete fixing some, others that also existed show up?  The code should be...

function Randvalue()
{
     var randVal=Math.round(Math.random()*4);
     this["mc"+String(randVal)].gotoAndPlay("over");
}

Randvalue();

Also, go into your Publish Settings and select the option to Permit Debugging in the Flash section.  This can add some details to your error message such as line numbers.