A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: addChild from class

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    5

    addChild from class

    Hi, I'm trying to 'addchild' (a circle shape) to the stage from a class that's not the main project class. Maybe this is a bad idea... I'm not sure. Anyway, my code isn't working, yet there are no errors. Here's it is below. Any suggestions would be appreciated.

    Code:
    package  {
    	
    	import flash.display.MovieClip;
    	import flash.display.Shape;
    	
    	
    	public class main extends MovieClip {
    		
    		var circle3:Shape = new Shape();
    		
    		public function main() {
    			// constructor code
    			
    			circle3.graphics.beginFill(0xefefef, 1);
    			circle3.graphics.drawCircle(100,100,50);
    			circle3.graphics.endFill();
    			addChild(circle3);
    		}
    	}
    	
    }

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,
    It does work, you probably can't see the circle, changes colours to test
    Last edited by fruitbeard; 06-02-2014 at 09:09 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    5

    I gave you the wrong code

    Quote Originally Posted by fruitbeard View Post
    Hi,
    It does work, you probably can't see the circle, changes colours to test
    Hi Fruitbeard, I accidentally posted the wrong code. Here's the actual code in error. It's a class outside of the main project class attempting to draw a circle on the stage. No circle is drawn and no error messages are generated.

    Code:
    package  {
    	
    	import flash.display.Shape;	
    	
    	public class makeCirc extends Shape {
    		
    		var circle3:Shape = new Shape();
    
    		public function makeCirc() {
    			// constructor code
    			circle3.graphics.beginFill(0xff0000, 1);
    			circle3.graphics.drawCircle(200,200,50);
    			circle3.graphics.endFill();
    			stage.addChild(circle3);
    		}
    
    	}
    	
    }

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I'm not sure how you have your other files??

    Main.as
    PHP Code:
    package 
    {
        
    import flash.display.Sprite;
        public class 
    Main extends Sprite
        
    {
            public var 
    circle3:makeCirc = new makeCirc();
            public function 
    Main()
            {
                
    addChild(circle3);
            }
        }

    makeCirc.as
    PHP Code:
    package 
    {
        
    import flash.display.Sprite;
        public class 
    makeCirc extends Sprite
        
    {
            function 
    makeCirc():void
            
    {
                var 
    circle:Sprite = new Sprite();
                
    circle.graphics.beginFill(0xff0000,1);
                
    circle.graphics.drawCircle(200,200,50);
                
    circle.graphics.endFill();
                
    addChild(circle);
            }
        }


  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks Fruitbeard, This seems like your suggestion would work. I just wanted to have the class that wasn't the main class, addchild, to keep classes as encapsulated as possible. Maybe what I was trying to do it bad practice. Not sure.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Yeah... I'd really like to learn how to encapsulate more to take advantage of the OOP aspect of as3. Doesn't seem like most tutorials or developer do this.

  7. #7
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    I think this is what you're asking...

    Flash movieclips are oop. So when you work with movieclips you're doing oop.

    The main movieclip is an instance of the main class. So you can have your main mc (the main class) load other classes but they'll be children of main mc.

    So, everything that you draw, code, or add to the "stage" in authoring, is a child of main. You're referencing everything from Main so you don't need to add main. to everything.

    In oop, you're not supposed to reference the parents - no encapsulation. You use events - this button has been pressed. When learning or developing, you need to do it. You can use MovieClip(parent)

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