Skip to main content
Participant
January 10, 2021
Question

Put movie clips in an array and then place them using JavaScript

  • January 10, 2021
  • 1 reply
  • 332 views

I used to do AS3 and am new to JavaScript.  I want to put my movie clips into an array (so later I can randomize their order) and choose some of them to display.  In AS3 I’d put them in an “Assets” layer so they aren’t on the screen, instantiate them, put them in an array, and then place them with “addChild.”  How do I do this in Animate using JavaScript?  All I have so far is:

var answer1Array = ["up1", "down1", "left1", "right1"];

This topic has been closed for replies.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
January 11, 2021

Hi.

 

In HTML5 Canvas documents you need to assign a linkage name to the symbol in the Library you want to add at runtime.

 

Then you can write your code like this:

var root = this;
var i;

root.platforms = []; // the array is a property of the main timeline so you can reference it in another frames

for (i = 0; i < 7; i++)
{
	var platform = new lib.Platform();
	
	platform.x = Math.random() * lib.properties.width;
	platform.y = Math.random() * lib.properties.height;
	root.addChild(platform);
	root.platforms[i] = platform;
}

 

I hope it helps.

 

Regards,

JC

SlishdfAuthor
Participant
January 14, 2021

Thank you for your reply!  I'm not sure this answers my question, though. 

 

In my noob reading of this code it looks like it adds instance names to a symbol in my library and then puts 7 of them in an array - is this right?

 

In my Canvas document, I have given the symbols instance names and put them in an array.  What I need to know is how do I then use this array to place the symbols on the stage?