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

Can't create a movieclip inside a movieclip as3

Participant ,
Feb 22, 2012 Feb 22, 2012

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

TOPICS
ActionScript
3.3K
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

LEGEND , Feb 23, 2012 Feb 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.

Translate
Contributor ,
Feb 23, 2012 Feb 23, 2012

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

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
Participant ,
Feb 23, 2012 Feb 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 !!!

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
LEGEND ,
Feb 23, 2012 Feb 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);

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
LEGEND ,
Feb 23, 2012 Feb 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.

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
Participant ,
Feb 23, 2012 Feb 23, 2012

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

Thankyou !!!

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
LEGEND ,
Feb 23, 2012 Feb 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")

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
Participant ,
Feb 23, 2012 Feb 23, 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 ?

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
LEGEND ,
Feb 24, 2012 Feb 24, 2012

Yes, both of those are legitimate to use, but don't forget that if you have photo declared, you can target it directly and don't need to do so indirectly, as in...

trace(photo.x);

If you have many photos to include in the holder, then you could assign them to an array and still target them directly using the array, though you need to keep track of the index values for individual photos.

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
Participant ,
Feb 26, 2012 Feb 26, 2012
LATEST

Ok, can you explain bit more about using 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