A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 26

Thread: How to use addChild from other class

  1. #1
    Junior Member
    Join Date
    Nov 2006
    Posts
    9

    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.

  2. #2
    Actionscript 3, postproduction georgecalgary's Avatar
    Join Date
    Jun 2005
    Location
    Toronto
    Posts
    229
    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); 
           }


  3. #3
    Junior Member
    Join Date
    Nov 2006
    Posts
    9
    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()

  4. #4
    imagination through stupidity
    Join Date
    Apr 2001
    Location
    P3X-3113
    Posts
    1,238
    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.

  5. #5
    Junior Member
    Join Date
    Nov 2006
    Posts
    9
    Can you rewrite the code? to make it works correctly? thanks
    Last edited by kitmaster; 11-27-2006 at 10:57 AM.

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  7. #7
    Junior Member
    Join Date
    Nov 2006
    Posts
    9
    Thank you very much!!!! I didn't know that I can use internal!

  8. #8
    Actionscript 3, postproduction georgecalgary's Avatar
    Join Date
    Jun 2005
    Location
    Toronto
    Posts
    229
    Sorry for writing bad code, supposed to be 'public'.

    Mostly I wouldn't like 'internal' for quick code...

  9. #9
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  10. #10
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    is internal AS 3 version of protected? (just curios)
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  11. #11
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  12. #12
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    ok so internal is package level. Thanks for the clarification.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  13. #13
    Junior Member
    Join Date
    Nov 2006
    Posts
    9
    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.

  14. #14
    Actionscript 3, postproduction georgecalgary's Avatar
    Join Date
    Jun 2005
    Location
    Toronto
    Posts
    229
    Quote 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.
    PHP Code:
    mov al061h 

  15. #15
    Junior Member
    Join Date
    Nov 2006
    Posts
    9
    It will be good if you write a little example where we use links to objects.

  16. #16
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    how would you approach this problem if "Class_2" were in a separate .as file? there has to be a way, right?

  17. #17
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813

    Question 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.

  18. #18
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    yeah, your error message is exactly what I'm encountering...would a more advanced AS3'er care to chime in on this one? thx!

  19. #19
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Are you extending the class?
    - The right of the People to create Flash movies shall not be infringed. -

  20. #20
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center