Skip to main content
Known Participant
May 29, 2011
Answered

instance name problem breaks my brain

  • May 29, 2011
  • 1 reply
  • 595 views

how can i create a instance name for a MovieClip Child?

cuz i want 2 make a hittest with a MovieClip Child heres my code:

Var ID = setInterval( createCopy, 1000 );

var counter = 900;


function createCopy(){

       var c:MovieClip = new bulb();

       addChild(c);

       c.x = Math.random() * stage.stageWidth;

       counter--;

       if( counter <= 0 ){

             clearInterval( ID );

      }

}

i want to make a hittest i have tried this:

if(c.hitTestObject(player))

{

    lives--;

}

but i doent work it says theres nothing that has a instance name of c

but what i dont understand is how dit the c.x = Math.random()*stage.stageWidth; code worked?

please answer i ive been stuck on this the hole day

This topic has been closed for replies.
Correct answer blazejewicz

Hello,

#1

your "c" named variable is local - so if you are trying to access it outside of "createCopy" method you would get error.

#2

there is a "name" property on MovieClip (from DisplayObject to be strict):
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#name

that you could use to give instance unique name and then retrieve it by that name using getChildByName(...) method:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#getChildByName()

see for example that thread for usage:

http://forums.adobe.com/thread/854911

hth,

regards,

Peter

1 reply

blazejewiczCorrect answer
Participating Frequently
May 29, 2011

Hello,

#1

your "c" named variable is local - so if you are trying to access it outside of "createCopy" method you would get error.

#2

there is a "name" property on MovieClip (from DisplayObject to be strict):
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#name

that you could use to give instance unique name and then retrieve it by that name using getChildByName(...) method:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#getChildByName()

see for example that thread for usage:

http://forums.adobe.com/thread/854911

hth,

regards,

Peter

Known Participant
May 30, 2011

canyou tell me how to do it in my code?

because the code in the thread you gave me is to advanced for me   

thanks

Participating Frequently
May 30, 2011

hi,

there are different ways to access movies (sprites) once they are created and added to display list. The way we are discussing here is to give unique name to each created bulb - so it could be later queried by that name in parent display list.

If you create bulbs that way:

private function createCopy():void

{

     var c:MovieClip = new Bulb();

     // name will be e.g. "c_1";

     c.name = "c_"+counter;

     addChild(c);

}

Then later you could at any time query given parent display list for that bulb using name property, constructed like:

private function findBulb():void

{

     var bulbName:String = "c_"+counter;

     var bulb:DisplayObject = getChildByName(bulbName);

     if(bulb)

     {

          // bulb found

          bulb.x += 50;

     }

}

getChildByName(...) return DisplayObject - but if you want to use it as instance of Bulb you would cast it that way:

var bulb:Bulb = getChildByName(bulbName) as Bulb

if(bulb) bulb.switchOn();

There are other ways to access runtime created clips at any time in future. For example people usually store reference to just created clip (c) in array. That array could be later traversed to work with each stored reference to clip, etc.

regards,

Peter