View Full Version : Display objects parent child relationship !
otuatail
09-22-2009, 01:57 PM
Confused over actionscript children. Reading a book on this shows the following code:
var firstContainer:Sprite = new Sprite();
var secondContainer:Sprite = new Sprite();
secondContainer.addChild(firstContainer);
addChild(secondContainer);
var thirdContainer:Sprite = new Sprite();
firstContainer.addChild(thirdContainer);
trace(firstContainer.numChildren); // =2 WHY should be 1
// Second has 1 child, first has 1 child (Second has a grand child maybe)
// Can't understand how first can have 2
playpianolikewoah
09-22-2009, 02:02 PM
This can be confusing.
But this is most certainly a mistake. unless there is other parts of the code we aren't seeing.
firstSprite should have 1 child
secondSprite should have 1
and third should have 0.. You are right
playpianolikewoah
09-22-2009, 02:03 PM
Didn't you trace it out?? I did and got 1 for the firstContainer.
otuatail
09-22-2009, 02:47 PM
Just getting started to this. This book is clearly full of errors. Not sure how to trace this out. I asumed just putting the code between the package section as in
package
{
var firstContainer:Sprite = new Sprite();
var secondContainer:Sprite = new Sprite();
secondContainer.addChild(firstContainer);
addChild(secondContainer);
var thirdContainer:Sprite = new Sprite();
firstContainer.addChild(thirdContainer);
trace(firstContainer.numChildren); // =2 WHY should be 1
}
playpianolikewoah
09-22-2009, 03:02 PM
well the easiest way to trace it out is to go right into flash itself. Not an external script. Right into the actions panel.. This stuff you pasted in is not object oriented.
If you wanted to do it in a package ie external script you would write.
package
{
import flash.display.Sprite;
public class Example extends Sprite
{
public function Example()
{
var firstContainer:Sprite=new Sprite ;
var secondContainer:Sprite=new Sprite ;
secondContainer.addChild(firstContainer);
addChild(secondContainer);
var thirdContainer:Sprite=new Sprite ;
firstContainer.addChild(thirdContainer);
trace(firstContainer.numChildren);
}
}
}
save that as Example.as
Go into a new flash document with no script in the actions panel and change the document class to Example
Make sure you save it in the same place as your Example.as
and run the movie.. that should trace it.
That might be a round about way to get there but since you are trying to learn OOP anyway..
otuatail
09-22-2009, 03:23 PM
I'm Ok with oop as I have a c++ and .net background. I want to learn this to add to website stuff.
I have in the same folder
Example1.as +
Example1.fla (created as new Flash File (Actionscript 3.0)
Ecample1.as ->
package
{
import flash.display.Sprite;
public class Example1 extends Sprite
{
public function Example1()
{
var firstContainer:Sprite=new Sprite ;
var secondContainer:Sprite=new Sprite ;
secondContainer.addChild(firstContainer);
addChild(secondContainer);
var thirdContainer:Sprite=new Sprite ;
firstContainer.addChild(thirdContainer);
trace(firstContainer.numChildren);
trace("Hi there");
}
}
}
No error when run as Example1 has been added to Class: on right.
Example1.swf opens but blank Bottom window Errors etc shows
1
Hi there
otuatail
09-22-2009, 03:26 PM
Ok sorry about that it makes sence now Thanks.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.