View Full Version : Accessing child of a MovieClip...? AS3!
Flashthinker
February 12th, 2007, 08:55 AM
My problem is this;
I have my stage and on it is one MovieClip that contains another MovieClip... Now I have a document class calledMain. Here it is
package{
import flash.display.Sprite;
import flash.display.MovieClip;
public class Main extends Sprite{
// omg is the MovieClip that is directly on the stage
public var omg:MovieClip;
// lmao is the MovieClip that is inside omg on frame two
public var lmao:Sprite;
public function Main(){
// go to frame two of omg where lmao is
omg.gotoAndStop(2);
// try to alter a property of lmao
omg.lmao.rotation+=45;
}
}
}
Well that pretty much summarizes my problem.
Personally, I prefer just having separate classes fo each object then bring them all together at compile-time, but someone I know had a problem with his game which had a lot of embeded movieclips already on the stage.
Can anyone explain to me how I could go about accessing the Sprite "lmao" which is on Frame 2 of the "omg" MovieClip?
I'm using flash 9 AS3 preview.
Dazzer
February 12th, 2007, 09:29 AM
doesn't work like that.
lmao must be Public INSIDE the omg class, before you can directly access it.
Flashthinker
February 12th, 2007, 09:39 AM
doesn't work like that.
lmao must be Public INSIDE the omg class, before you can directly access it.
But there is no omg class. omg HAS to be a MovieClip otherwise an error will be thrown. It's like a programatical dilema!
If I went public var omg:omg = new omg()
It wouldn't import the movieclip "omg" on the stage, instead it would try to create a new instance of the "omg" class which extends MovieClip.
Is it even possible to do what I'm trying to do?
... Maybe I could try creating an OMG class that holds a copy of the omg movieclip... but I don't add it to the stage, I just use it to fetch lmao and pass its reference to the Main class.
Dazzer
February 12th, 2007, 09:57 AM
try
var lmao:Sprite = omg.getChildByName('lmao');
Flashthinker
February 12th, 2007, 10:26 AM
It still doesn't work. It still comes up with the "Cannot access a property or method of a null object reference."
And what's strange is that if I go to frame 2 of omg, and then do trace(omg.numChildren) it outputs 2! I know one is a Shape and the other has to be lmao, but only if I trace (getChildAt(1)) it outputs null. and if I do getChildAt(2) it outputs an error because it is outside of the index range... So I know it's there, but it wont let me access it.
senocular
February 12th, 2007, 11:08 AM
Full initialization does not occur until after the currect script has completed. The same applies with scripts on the new frame of that child - they will not run until after the current script, prior to the next screen update.
Waiting for the ADDED event will provide you with access to the child but prior to the frame script. Using the (stage)RENDER event will give you access to the child after the frame script.
As for the null reference, I'm currently investigating its purpose and whether or not it was intended.
Flashthinker
February 13th, 2007, 03:35 AM
Full initialization does not occur until after the currect script has completed. The same applies with scripts on the new frame of that child - they will not run until after the current script, prior to the next screen update.
Waiting for the ADDED event will provide you with access to the child but prior to the frame script. Using the (stage)RENDER event will give you access to the child after the frame script.
As for the null reference, I'm currently investigating its purpose and whether or not it was intended.
Thanks, that solved part of the problem... But there is something else.
Because it is a movieclip and the child is on frame 2, if I don't call "omg.gotoAndStop(2)" BEFORE listening for the RENDER event, it will not render "lmao" as part of the movieclip. So I guess that means that I would have to call render events for EVERY frame of every movieclip. Maybe I should make a class that does this automatically... OMG I would have thought that there would be an easier way.
Quantium
January 11th, 2008, 02:19 PM
Hi, I know this is a old post, but I want to know if sombody solve this issue, because I had the same problem. I'm working with more than 1000 swf and I don't want to change everyone of them (Even with a jsfl is a very hard work) So please helpme if somebody has an answer for this o a sample of code with this issue solved.
IQpierce
August 8th, 2008, 10:37 PM
I've been reading this thread and this other thread about the same issue:
http://www.kirupa.com/forum/showthread.php?t=247040
Is there really no better workaround for this problem? I'm not even sure I understand exactly how Added or Render events could be used to solve this - could someone show a concrete example?
It's been 7 months since this thread was updated, I hope that in the meantime some more experienced AS3 developers have found ways to work around this that they can share with us noobs!!
senocular
August 8th, 2008, 11:18 PM
Render works because its an event that occurs at the end of a frame, when the rest of the scripts have completed and playheads have been updated. Added works because it confirms that the object you want to access does in fact exist and has been added to the display list. Added is the most reliable (there are situations when Render can fire prematurely through other events) but is the hardest to manage.
How you approach this really depends on the circumstance. You'll likely have to rethink how navigation is handled, though. Its not AS2 anymore.
IQpierce
November 25th, 2008, 01:45 PM
No it's definitely not AS2 anymore... I can tell... AS2 had a big set of stupid bugs and limitations that required lame workarounds... AS3, on the other hand, eliminated all of those, and replaced them with a completely DIFFERENT set of stupid bugs and limitations that require lame workarounds! And this bug is offender #1.
senocular
November 25th, 2008, 02:03 PM
Much of the timeline issues have been resolved in Flash Player 10 (with SWF 10), including child access after navigation.
aliasger
December 16th, 2009, 12:13 AM
Much of the timeline issues have been resolved in Flash Player 10 (with SWF 10), including child access after navigation.
Hi I know we have moved out to flash CS4 /player 10 but my problem still isn't solved...
Does that mean we cant fix this issue if we use flash CS3?
Also the clients need to have flash player 10?
I still haven't found a solution for this problem in flash CS3/player 9.
Any workarounds would be helpful.
senocular
December 16th, 2009, 12:16 PM
There should be a solution for whatever your problem is. If you're stuck with CS3/Flash Player 9 then you just need the correct workaround. There are a lot of problems with 9 so you'll have to be more specific as to what exactly is going wrong for you.
astrix
June 3rd, 2011, 08:49 PM
Senocular, you is an intelligent and good man, but the question is easy, imagine a simple fla with a simple text1 component in the Stage, if I am in the fla timeline, I access the movieclip text1 properties with a simple:
text1.text = "hello";
Well, if I am in class, how make the same?????????????????????????????????????????????? ?????????????? And a little code of class example!!!!!!!!!!!!
Why is so difficult????? Is simple, AS3 not is a good language, because complicated many simple things... Is a languaje for disturb, the creators know the way, but is many indocumented...
Another thing very bad of Adobe AS3 is that at now not do support for AS2, for example, in programation of android "Air for Android", not run AS2 swf's, but in web YES!!! Why??? Is simple, in web are millions of AS2 swf's in millions of webs, if Adobe no t do support for AS2 in web player, millions of web's KAPUT!!!, and Adobe have a very much bad publicity... But for developpers, not do support, but have, but not do, why??? For disturb the developpers and only the adobe persons have the possibility of create programas...
Is a very dirty actitude!!! Very dirty... Many persons lost time in change As2 codes, and this is not serious, why I or another need to lost many years of programation for Adobe have benefits in create programas??? Is criminal inclusive...
And this, how to simple reference a movieclip in the stage from a simple class??? Appear to be is the secret of holly bible!!! Bad, very bad Adobe, Microsoft, Apple, all are only dirty companies only think in your benefits... And in facebook and twitter the person begin to comment, and how this not change, is possible a revolution and this companies kaput...!!!
I know you work in adobe, and you is a good person, this comments not are for you, are for Adobe... Very bad actitude, with this actions, I not purchase more never of adobe, and work in facebook the persons know how work this companies... Sorry for my bad english!!!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.