Skip to main content
Inspiring
October 17, 2012
Answered

Get All instance names in the stage

  • October 17, 2012
  • 1 reply
  • 993 views

hi,

is there any way to get all instance name of the objects presents on the stage ? trace them for example

thank you

This topic has been closed for replies.
Correct answer kglad

traceF(this);

function traceF(mc:MovieClip):Void{

for(s in mc){

if(mc._parent==mc){

trace(s+" : "+mc);

// if you want to check for children add:

if(typeof(mc)=="movieclip"){

traceF(mc);

}

}

}

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 17, 2012

traceF(this);

function traceF(mc:MovieClip):Void{

for(s in mc){

if(mc._parent==mc){

trace(s+" : "+mc);

// if you want to check for children add:

if(typeof(mc)=="movieclip"){

traceF(mc);

}

}

}

}

Inspiring
October 17, 2012

thank you, it's the correct answer but i still can't solve my problem ,

the thing is that am using a code from http://www.freeactionscript.com that make enemie follow the player, but i need to do some modification, i need to detect collision between enemies so they will not get the one into the other and the thing that i cannot found their instance name, even when i used your function i only get "player_mc : _level0.player_mc" at the output,

here is the code, i will be really greatful if you can find a way to help me solve this problem .

ps :

am trying to write my own code that make enemie follow the player, coz this one looks very complicated

thank you

the code :

  

/**

* Game Enemy AI Behavior - Run Away & Follow Player

*

* Version:           1.0

* Author:           Philip Radvan

* URL:                     http://www.freeactionscript.com

*/

var enemiesArray:Array = new Array();

var radians:Number = 180/Math.PI;

createEnemies(5, "typeA", "e1");

createEnemies(5, "typeB", "e2");

createEnemies(5, "typeC", "e3");

//

// createEnemies(number of enemies, behavior)

// use ex: createEnemies(10, "slow", "myLinkedMovieClip);

//

function createEnemies(enemyAmount:Number, enemyBehavior:String, enemyLibraryClip:String):Void

{

          //run a for loop based on the amount of enemies

          for(var i = 0; i < enemyAmount; i++)

          {

                    //set temporary variable that will hold the new enemy attributes

                    var tempEnemy:MovieClip = _root.attachMovie(enemyLibraryClip, "enemy"+_root.getNextHighestDepth(),_root.getNextHighestDepth())

 

                    //give new enemy a random x/y position based on stage width/height

                    tempEnemy._x = random(Stage.width);

                    tempEnemy._y = random(Stage.height);

                    tempEnemy._rotation = random(360);

 

                    //set enemy behavior

                    if(enemyBehavior == "typeA")

                    {

                              //define enemy characteristics

                              tempEnemy.speed = 1

                              tempEnemy.turnRate = .05

                              tempEnemy.agroRange = 200;

                              tempEnemy.mode = "follow"

                    }

                    else if(enemyBehavior == "typeB")

                    {

                              //define enemy characteristics

                              tempEnemy.speed = 4

                              tempEnemy.turnRate = .5

                              tempEnemy.agroRange = 200;

                              tempEnemy.mode = "follow"

                    }

                    else if(enemyBehavior == "typeC")

                    {

                              //define enemy characteristics

                              tempEnemy.speed = 1

                              tempEnemy.turnRate = .2

                              tempEnemy.agroRange = 100;

                              tempEnemy.mode = "run"

                    }

 

                    //define variables that are used to calculate following

                    //*don't change these*

                    tempEnemy.distanceX = 0;

                    tempEnemy.distanceY = 0;

                    tempEnemy.distanceTotal = 0;

                    tempEnemy.moveDistanceX = 0;

                    tempEnemy.moveDistanceY = 0;

                    tempEnemy.moveX = 0;

                    tempEnemy.moveY = 0;

                    tempEnemy.totalmove = 0;

 

                    //add new enemy to array

                    enemiesArray.push(tempEnemy)

          }

}

//Update enemies function

function updateEnemies():Void {

          //run a for loop based on the amount of enemies

          for(var i = 0; i < enemiesArray.length; i++)

          {

                    //set temporary variable that will hold the new enemy attributes

                    var tempEnemy:MovieClip = enemiesArray;

                    //run follow function with temporary enemy as the follower

                    updatePosition(tempEnemy, player_mc);

          }

}

//

// updatePosition(follower, target)

// use ex: updatePosition(myEnemyMovieClip, playerMovieClip)

//

function updatePosition(follower:MovieClip, target:MovieClip) {

 

          //calculate distance between follower and target

          follower.distanceX = target._x-follower._x;

          follower.distanceY = target._y-follower._y;

 

          //get total distance as one number

          follower.distanceTotal = Math.sqrt(follower.distanceX * follower.distanceX + follower.distanceY * follower.distanceY);

 

          //check if target is within agro range

          if(follower.distanceTotal <= follower.agroRange){

                    //calculate how much to move

                    follower.moveDistanceX = follower.turnRate * follower.distanceX / follower.distanceTotal;

                    follower.moveDistanceY = follower.turnRate * follower.distanceY / follower.distanceTotal;

 

                    //increase current speed

                    follower.moveX += follower.moveDistanceX;

                    follower.moveY += follower.moveDistanceY;

 

                    //get total move distance

                    follower.totalmove = Math.sqrt(follower.moveX * follower.moveX + follower.moveY * follower.moveY);

 

                    //apply easing

                    follower.moveX = follower.speed * follower.moveX / follower.totalmove;

                    follower.moveY = follower.speed * follower.moveY / follower.totalmove;

 

                    //move & rotate follower

                    if(follower.mode == "follow")

                    {

                              follower._x += follower.moveX;

                              follower._y += follower.moveY;

                              follower._rotation = Math.atan2(follower.moveY, follower.moveX) * radians;

                    }

                    else if(follower.mode == "run")

                    {

                              follower._x -= follower.moveX;

                              follower._y -= follower.moveY;

                              follower._rotation = (Math.atan2(follower.moveY, follower.moveX) * radians)+180;

                    }

 

 

          }

}

//onEnterFrame that executes the updatePosition updateEnemies every frame

_root.onEnterFrame = function(){

          updateEnemies();

}

//start/stop drag for player_mc

player_mc.onPress = function(){

          startDrag(this);

}

player_mc.onRelease = function(){

          stopDrag();

}

kglad
Community Expert
Community Expert
October 17, 2012

the trace output before the colon IS the instance name.  the trace output after the colon is the instance's path and name.

you enemies are named enemyN where N is a non-negative integer.  all your enemies are stored in enemiesArray.  you can use the following to loop through your enemies:

for(var i:Number=0;i<enemiesArray.length;i++){

trace(enemiesArray);

}

p.s.  please mark helpful/correct responses.