Skip to main content
karthic_vkn
Inspiring
February 23, 2012
Answered

Can't create a movieclip inside a movieclip as3

  • February 23, 2012
  • 3 replies
  • 3264 views

Hi All,

I am not able to create a empty movie clip inside a empty movieClip.

Here the code..

 

var photoHolder:MovieClip=new MovieClip();

addChild(photoHolder);

var photo:MovieClip=new MovieClip();

photoHolder.addChild(photo);

trace(photoHolder.photo.x)

// the message i got is ---------------------------------

TypeError: Error #1010: A term is undefined and has no properties.

at Untitled_fla::MainTimeline/frame1()

//------------------------------------------------------------------

Can you correct my code if i am wrong !!!

Thanks,

Karthic

This topic has been closed for replies.
Correct answer Ned Murphy

When you create objects dynamically you can no longer use the dot method to target objects like you can when you manually create them.  To target the dynamically created child of photoHolder you need to use indirect targeting methods such as photoHolder.getChildByName("photo") or photoHolder.getChildAt(0), etc... but you don't need to go that route anyways because...

You already have a direct route to target photo (as in, photo), so you don't need to target it via photoHolder.

3 replies

karthic_vkn
Inspiring
February 23, 2012

Ned Murphy, You rocks !!!, superb !!!

Thankyou !!!

Ned Murphy
Legend
February 23, 2012

I should clarify that if you use the getChildByName() method, you need to have assigned a name property to the object.  The variable name "photo" is not its name property.  YOu have to specifically assign the name property, as in... photo.name = "whatever"; such that in the method you would then use...

photoHolder.getChildByName("whatever")

karthic_vkn
Inspiring
February 24, 2012

Yeah Ned Murphy, I was using this getchildbyName() and getChildAt() methods, last 6 month i didn't work on flash, When i ask this question i totally fotgot every think. After seeing you comment i got everythink back and i done the flash,

Here 2 way to access photo inside photoholder !!!

---------------------------------------------------------------------------------------------------------------------

import flash.display.MovieClip;

var photoHolder:MovieClip = new MovieClip();

var photo:MovieClip = new MovieClip();

photo.name = "photo";

addChild(photoHolder);

photoHolder.addChild(photo);

var method_1:MovieClip = photoHolder.getChildByName("photo") as MovieClip;

trace(method_1.x);

//or 

var method_2:MovieClip = photoHolder.getChildAt(0) as MovieClip;

trace(method_2.x);

---------------------------------------------------------------------------------------------------------------------

this is working fine,

can you tellme is this right way ?

Ned Murphy
Ned MurphyCorrect answer
Legend
February 23, 2012

When you create objects dynamically you can no longer use the dot method to target objects like you can when you manually create them.  To target the dynamically created child of photoHolder you need to use indirect targeting methods such as photoHolder.getChildByName("photo") or photoHolder.getChildAt(0), etc... but you don't need to go that route anyways because...

You already have a direct route to target photo (as in, photo), so you don't need to target it via photoHolder.

kiran1989
Inspiring
February 23, 2012

I Think why don't you use trace(photo.x). I am expecting the x position will be zero

karthic_vkn
Inspiring
February 23, 2012

if you use "trace(photo.x)" that means the movieclip "photo" is on stage, not inside "photoHolder" Movieclip,

I think, you got it !!!

Ned Murphy
Legend
February 23, 2012

photo is inside photoHolder, but as I said, you cannot target it like you normally would (and you don't have to).  YOu can test that it is by checking the value of.... trace(photo.parent);