-
How to use addChild from other class
I have two classes
Code:
package{
import flash.display.Sprite;
public var cell:Sprite;
public class Class_1 extends Sprite{
public function Class_1() {
cell=new Cell();
cell.x=x;
cell.y=y;
addChild(cell);
getChildAt(0);//works
}
}
public class Class_2 extends Sprite{
public function Class_2() {
getChildAt(0); // doesn't work
}
}
What should I do to have access to my display objects from other class, not from main? Please write a code if you know how to solve this problem.
Last edited by kitmaster; 11-27-2006 at 08:27 AM.
-
Actionscript 3, postproduction
Try this.
PHP Code:
package{
import flash.display.Sprite;
public var cell:Sprite;
public class Class_1 extends Sprite{
//
private static var _class1;
public function Class_1() {
//
Class_1._class1=this;
cell=new Cell();
cell.x=x;
cell.y=y;
addChild(cell);
getChildAt(0);//works
}
}
public class Class_2 extends Sprite{
public function Class_2() {
Class_1._class1.getChildAt(0);
}
}
-
Do you mean this?
Code:
{
import flash.display.Sprite;
public var cell:Sprite;
public class Class_1 extends Sprite{
//
private static var _class1;
public function Class_1() {
//
Class_1._class1=this;
cell=new Cell();
cell.x=10;
cell.y=10;
addChild(cell);
getChildAt(0);//works
}
}
public class Class_2 extends Sprite{
public function Class_2() {
Class_1._class1.getChildAt(0);
}
}
public class myClass3 extends Sprite{//main class
public function myClass3(){
var a:Class_1=new Class_1();
var b:Class_2=new Class_2();//doesn't work
}
}
}
Error:
A term is undefined and has no properties.
at Class_2$iinit()
at myClass3$iinit()
-
imagination through stupidity
im guessing addChild and getChildAt are methods of the super class Sprite? Normally, access to inherited methods are directly accessable. (ie, Class_1.addChild or Class_1.getChildAt() .. it is a good idea tho, when accessing super methods internally to use the 'super' operator, super.addChild )
Nothing to see here, move along.
-
Can you rewrite the code? to make it works correctly? thanks
Last edited by kitmaster; 11-27-2006 at 10:57 AM.
-
Senior Member
Your _class1 variable is private. Make it internal if the other class is in the same .as file or public if it is in a different .as file.
for example this works:
PHP Code:
package{
import flash.display.Sprite;
public var cell:Sprite;
public class Class_1 extends Sprite{
//
internal static var _class1;
public function Class_1() {
Class_1._class1=this;
cell=new Cell();
cell.x=x;
cell.y=y;
this.addChild(cell);
getChildAt(0);//works
var b:Class_2 = new Class_2();
}
}
public class Class_2 extends Sprite{
public function Class_2() {
trace(Class_1._class1.getChildAt(0));
}
}
}
- The right of the People to create Flash movies shall not be infringed. -
-
Thank you very much!!!! I didn't know that I can use internal!
-
Actionscript 3, postproduction
-
Senior Member
It's not bad code. It is an alternative to public and will restrict access from outside the AS file.
- The right of the People to create Flash movies shall not be infringed. -
-
OOP is one letter from OOPS
is internal AS 3 version of protected? (just curios)
-
Senior Member
protected is available only to the class and to subclasses of the class.
internal are all variables and classes by default and are only available in the package. However, i usually like to add the statement even it is default. So protected and internal are different statements. They are both AS3.
- The right of the People to create Flash movies shall not be infringed. -
-
OOP is one letter from OOPS
ok so internal is package level. Thanks for the clarification.
-
Some people believe that it is better to use links to objects in this code. Can you show how to use them? they don't want to write code because they think that real professional shouldn't write simple code.
Last edited by kitmaster; 11-27-2006 at 05:37 PM.
-
Actionscript 3, postproduction
Originally Posted by kitmaster
Some people believe that it is better to use links to objects in this code. Can you show how to use them? they don't want to write code because they think that real professional shouldn't write simple code.
It all depends.
I could even code assembly.
-
It will be good if you write a little example where we use links to objects.
-
how would you approach this problem if "Class_2" were in a separate .as file? there has to be a way, right?
-
Flash/Flex Developer
Still don't understand the whole addChild thing
I have a similar situation but with a twist. I hope I can explain this clearly.
I have a Main Document Class file which is executed when the FLA begins. In that I need to place buttons (MCs) on the stage. My MCs are already in the library. Now AS 3.0 is referring to the MCs as "classes" and that is now I must get to them using addChild.
Now my problem. I need to attach another class to the MCs but my class is in a totally different file. This is what I have so far.
In my Main.as package I have:
Code:
var btn_help:NavButton = new NavButton("btn_help");
My NavButton class with give all the buttons a similar functionality but I'm going to extend the individual buttons with another class (a problem I haven't started to work on yet).
In my NavButton.as package I have:
Code:
public function NavButton(btnName:String){
var MCname:Object = getDefinitionByName(btnName);
var instanceName:Class = new MCname();
addChild(MCname(instanceName));
}
When I run it I get the following error:
1180: Call to a possibly undefined method addChild.
I cannot figure out way, basically I don't understand the error message. I've been programming in AS 2.0 for a few years, so I understand how to program, but this change for AS 3.0 is killing me. Any help would be appreciated.
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
yeah, your error message is exactly what I'm encountering...would a more advanced AS3'er care to chime in on this one? thx!
-
Senior Member
Are you extending the class?
- The right of the People to create Flash movies shall not be infringed. -
-
Samac, here's the answer I got from another forum...it works!!!
Thx Jaga:
in main you would declare your example instance and add it to the display list
var example:Example = new Example();
addChild(example);
and then anything you add to the example's display list will be seen..
Stage->example->someValidLocal_mc
so to answer the question Samac, inside the Main class, do this:
Code:
var example:Example = new Example();
addChild(example);
...where Example is your "other" class, then inside the Example class you would just say:
Code:
addChild(someLocalValid_mc);
it works!!!
Last edited by absolutezero342; 12-14-2007 at 01:41 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|