Skip to main content
Participating Frequently
November 9, 2013
Question

Generating Random multiple Instances From the library AS3

  • November 9, 2013
  • 1 reply
  • 649 views

If someone can help me out with this problem I would greatly apprciate it.  In AS3 I have 1 ball being exported out in actionscript in layer 1 from the library then gets placed on stage when the .swf publishes. here is the code:

The coin movieclip represents the "ball" and its the same movieclip over and over again. (works great no errors)

----code------------

// generates new Coin - Coin is the name of MovieClip in Library

          // remember to export for Actionscript

          var c1:Coin = new Coin();

          var chosenNumsIndex1:uint = chosenNumsIndex;

          trace("chosenNumsIndex1 outside " +chosenNumsIndex1);

          // coin is placed at appropriate x and y coords

          c1.x = kNum[chosenNums[chosenNumsIndex]].x;

          c1.y = kNum[chosenNums[chosenNumsIndex]].y;

          // addChild tells main timeline to display coin once it's generated.

          this.addChild(c1);

What I want to achieve is to have 8+  random movieclips created and linked to the "coin"  so when it gets exported it is not the same ball all the time so this is what I have for that:

var ballsArray:Array = [ball00,ball01,ball02,ball03,ball04,ball05,ball06,ball07,ball08];------- movieclips in the library

Question:  how do I get the coin to read this array  randomly so it doesnt go to "coin movieclip"and just play the one ball i would like it go play the ball of arrays?

thank you very much in advance

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
November 9, 2013

assign the class names to an array:

var ballClassA:Array=["Ball00","Ball01",...];

function createBallF():void{

var C:Class=Class(getDefinitionByName(ballClassA[Math.floor(ballClassA.length*Math.random())]));

var c:MovieClip=new C();

addChild(c);

}

Participating Frequently
November 9, 2013

kglad, I had pasted the code above given me errors..

Scene 1, Layer 'actions', Frame 1, Line 1861084: Syntax error: expecting rightparen before ngth.
Scene 1, Layer 'actions', Frame 1, Line 1861084: Syntax error: expecting rightbracket before rightparen.

updated & then more errors...  But where whould I paste it to replace the "coin"

// generates new Coin - Coin is the name of MovieClip in Library

          // remember to export for Actionscript

var c1:Coin = new Coin();

          var chosenNumsIndex1:uint = chosenNumsIndex;

          trace("chosenNumsIndex1 outside " +chosenNumsIndex1);

 

          // coin is placed at appropriate x and y coords

          c1.x = kNum[chosenNums[chosenNumsIndex]].x;

          c1.y = kNum[chosenNums[chosenNumsIndex]].y;

 

          // addChild tells main timeline to display coin once it's generated.

          this.addChild(c1);

          trace(chosenNums[chosenNumsIndex])

kglad
Community Expert
Community Expert
November 9, 2013

this forum distorts code.  remove errant white space in the code.

what you called coin i call c in the code.