Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding instances to array

Explorer ,
Apr 23, 2013 Apr 23, 2013

Hey guys,

I've been trying to create a simple platformer game. What I want to do is when the game starts I want to create an array that contains all the instances of the MovieClip symbol "Platform". The instances themselves on the stage don't have names, I just want to create an array with all the instances of the Platform symbol. I've seen a few examples but they aren't quite the same as what I'm trying to do. How can I push the instances as MovieClips into an array?

Thanks

TOPICS
ActionScript
529
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Apr 23, 2013 Apr 23, 2013

use the is keyword while going through the displaylist

and finding instances of certain Classes (linkage your MovieClip with Actionscript for this)

if(container.getChildAt(i) is Platform){

      myPlatforms.push(container.getChildAt(i));

}

Translate
LEGEND ,
Apr 23, 2013 Apr 23, 2013

An array holds anything you like. In this case it would just be pushing references of a potential displaylist object.

It's as ease as:

var myPlatforms:Array = new Array();

myPlayforms.push(somePlatform);

// or if named instances

myPlatforms.push(getChildByName('instancename'));

That'll just push references of that platform into the array.

How are you generating the platforms? It doesn't NEED an instance name but for ease your array should have the scope necessary to add them where you create them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 23, 2013 Apr 23, 2013

The platforms are not added to the stage dynamically, I just placed them on the stage in the FLA file. They don't have instance names, I could just go through and name them but I'd like to know how to just create an array of instances of a certain symbol that are on the stage

I dont understand the first one :

myPlatforms.push(somePlatform);

What should be in place of "somePlatform" in my script? How do I actually push the platform instances into the array?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 23, 2013 Apr 23, 2013

use the is keyword while going through the displaylist

and finding instances of certain Classes (linkage your MovieClip with Actionscript for this)

if(container.getChildAt(i) is Platform){

      myPlatforms.push(container.getChildAt(i));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 23, 2013 Apr 23, 2013
LATEST

Perfect, thanks mocca! .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines