Welcome, Guest. Please login or register.
Did you miss your activation email?
05/22/12, 17:13
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  General
| |-+  Flash and AS 3 (Moderators: papachan, kofi addaquay)
| | |-+  Differences about root and this in Class Interaction
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: Differences about root and this in Class Interaction  (Read 1134 times)
hinomotoblade
Seasoned Programmer
***
Posts: 131


Flash Never Die :D


View Profile
« on: 03/18/10, 21:12 »

Hello i have got some confusing problem here, about the differences about root / this in Class Interaction

Code:
trace(root.parent);
trace(this.parent);
trace(this.parent.parent);

and the result is
Quote
Stage
Main
Stage

I know that the Main is the AS3 files but how can be trace Main?..

Because sometimes the output is different and sometimes null, sometimes Loader, i dont know why,
If we can make some object with parent, can we make the object with a Child?..

because when i tried to do root.child / this.child its contains error..

Whats actually about that means?..

Thank you very much, i didnt get the clear clue in google.. Smiley
Logged

Huihihi~ Cheesy
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #1 on: 03/18/10, 21:40 »

i think it would be good if you invested in an actionscript 3 book. it will really help you. instead of relying on google

parent -- lets say you have a movieclip within another movieclip.... the nested movieclip is the parent of the outer movieclip. so if B is inside A. then A is the parent. understand?

root... is a reference to the main timeline. you can also think of it as the highest level timeline.

this...is a reference the class its self OR a movieclip.


might be hard to understand. but a book would explain it better

Logged
hinomotoblade
Seasoned Programmer
***
Posts: 131


Flash Never Die :D


View Profile
« Reply #2 on: 03/19/10, 07:46 »

Ahh yes2 kofi dont worry, your explanation are really clear to me Smiley

Yes i have find in the help of AS3 Script, but still not clear because its the interaction between 2 as files or 2 swf files.. Smiley

For example in my case is like this

swfA is publish with A.as
swfB is publish with B.as

then in swfA i inserted Loader that Load the swfB
when i compiled swfB the trace result is like this

Quote
this.parent : [object Stage]
this.parent.parent : null
root.parent : [object Stage]
root.parent.parent : null

then when i compile the swfA

Quote
this.parent : [object Loader]
this.parent.parent : [object A]
root.parent : [object Loader]
root.parent.parent : [object A]

And all of the trace function i inserted in the B.as
it seems they have some interaction between 2 Class, whereas i didnt use Inheritance or whatever.

Look into the first result, thats null right?.. why didnt trace B?..
in the second result the root/this have the same output..

Its really confusing, have any idea?.. Smiley

Thanks kofi.. Cheesy
« Last Edit: 03/19/10, 07:49 by hinomotoblade » Logged

Huihihi~ Cheesy
hinomotoblade
Seasoned Programmer
***
Posts: 131


Flash Never Die :D


View Profile
« Reply #3 on: 03/19/10, 08:10 »

Ups done.. Smiley

Actually, why null because i forgot that if some swf files compile together with as
we can call the method without make the new object of that class first.. Smiley

about the root/this why same, because some of swf files loaded the other swf files,
so its can be like 1 swf files that combine from 2 swf files.. Smiley

[Loader] Because the gateway is the loader to make two swf Interaction in AS3..
Thats my conclusion.. Smiley

Anyway Thanks Kofi.. Smiley
Logged

Huihihi~ Cheesy
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #4 on: 03/19/10, 08:45 »

let me see your entire code...for ClassA and ClassB
Logged
hinomotoblade
Seasoned Programmer
***
Posts: 131


Flash Never Die :D


View Profile
« Reply #5 on: 03/19/10, 09:17 »

Sure Kofi Smiley

This is the Classes A

Code:
package
{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;

public class Mains extends MovieClip
{
private var loader:Loader;

public function Mains()
{
bg_mc.stop();
stage.frameRate=31;
loadSWF();
}

private function loadSWF():void
{
var swf:URLRequest=new URLRequest();
swf.url='external.swf';

loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,done);
loader.load(swf);
}

private function done(evt:Event):void
{
addChild(loader);
trace("FROM MAINS AS3");
trace("this.parent : "+this.parent);
trace("this.parent.parent : "+this.parent.parent);
trace("root.parent : "+root.parent);
trace("root.parent.parent : "+root.parent.parent);
}
}
}

And this one is Classes B Smiley

Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;

public class External extends MovieClip
{
public function External()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}

private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
trace("FROM EXTERNAL AS3");
trace("this.parent : "+this.parent);
trace("this.parent.parent : "+this.parent.parent);
trace("this.clip_mc : "+this.clip_mc);
trace("root.parent : "+root.parent);
trace("root.parent.parent : "+root.parent.parent);
var m:MovieClip=root.parent.parent as MovieClip;
trace("m.bg_mc : "+m.bg_mc);
m.bg_mc.play();
clip_mc.addEventListener(Event.ENTER_FRAME,go);
}

private function go(evt:Event):void
{
evt.target.rotation++;
}
}
}

I just have one question in that case.. Smiley
How to initialize the Child from the root?..
When i do, this.child / this.child.child its containts error Smiley
Logged

Huihihi~ Cheesy
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #6 on: 03/19/10, 21:39 »

ok...here is how i did mine. i have two flash files

Loading.fla ( ClassA )
external.fla ( ClassB )

then i load external.swf INTO Loading.swf

here is my code

ClassA
Code:
package
{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;

public class ClassA extends MovieClip
{
private var loader:Loader;

public function ClassA()
{
trace("Class A");
loadSWF();
}

private function loadSWF():void
{
var swf:URLRequest=new URLRequest();
swf.url = 'external.swf';

loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,done);
loader.load(swf);
}

private function done(evt:Event):void
{
addChild(loader);
}
}
}


then here is Class B

Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;

public class ClassB extends MovieClip
{
public function ClassB()
{
trace("class B is instantiated");
if(stage==null)
{
this.addEventListener(Event.ADDED_TO_STAGE, addedToList);
}
}

private function addedToList(e:Event):void
{
trace("class B is added to stage");
trace("class B this.parent:" + this.parent);
trace("class B this.parent:" + this.parent.parent);
}
}
}


then i first run external.swf.....after i run the main swf loading.swf

here is the trace i get

Class A
class B is instantiated
class B is added to stage
class B this.parent:[object Loader]
class B this.parent:[object ClassA]

so you see... first Class A is loaded. then class B is instantiated. after instantiation...class B is added to the stage. then from Class B we find out what the parent class is...it is loader..because loader called it. then when we find out who is the parent of the loader...obviously we get Class A

if(stage==null) needs to be added. its a VERY important part of the process. i hope this helps you


Kofi
Logged
hinomotoblade
Seasoned Programmer
***
Posts: 131


Flash Never Die :D


View Profile
« Reply #7 on: 03/19/10, 23:28 »

Ouw yes, thats make me more clear.. Thank you Kofi.. Smiley

Btw i like ur Explanation.. Cheesy
Logged

Huihihi~ Cheesy
kofi addaquay
Global Moderator
Senior Programmer
*****
Posts: 450



View Profile WWW Email
« Reply #8 on: 03/19/10, 23:37 »

 Tongue
Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!