A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Class item not showing

  1. #1
    Junior Member
    Join Date
    Nov 2007
    Posts
    7

    Question Class item not showing

    Hi guys im trying to get my head around classes. Ive manged to import a class into AS3 I know it works as the trace shows but the shape im making is not showing. Heres the copde im using below. Is the this.addchild incorrect? Please help as im getting close to working this class business out. Thanks

    Heres my main .as file that imports my class. As you can see ive simply draw a rectangle

    Code:
    package classes{
     
       import flash.display.MovieClip;
       
       import classes.bats.Leftbat;
    
       public class Main extends MovieClip{
    		
    		// Variables
            public var net:MovieClip = new MovieClip();
    		public var myleftbat = new Leftbat();
    		
    		public function Main(){
    			//
    			net.graphics.lineStyle(1);
    			net.graphics.beginFill(0xff0000);
    			net.graphics.drawRect(225,0,50,400);
    			this.addChild(net);
    }
    }
    }
    And heres my other class .as file that is imported. as you can see ive simply draw another rectangle, this is not being shown

    Code:
    package classes.bats{
    	
       import flash.display.MovieClip;
    
       public class Leftbat extends MovieClip{
    	
    		// Variables
            public var leftbat:MovieClip = new MovieClip();
    		
    		public function Leftbat(){
    			//
    			leftbat.graphics.lineStyle(1);
    			leftbat.graphics.beginFill(0xffff00);
    			leftbat.graphics.drawRect(0,0,50,400);
    			this.addChild(leftbat);
    			
    			trace ("Hello");
    }
    }
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The major issue is that you never added myleftbat to the stage via addchild.

    The minor issues are:
    1) you should instantiate in the constructor not up where you declare the variables
    2) you don't really need the "this" in "this.addChild", but it doesn't harm anything.
    3) Since LeftBat itself extends MovieClip, you don't need an internal leftbat MovieClip to draw on.
    4) These don't have frames so they should be Sprites.

    Code:
    package classes{
     
       import flash.display.Sprite;
       
       import classes.bats.Leftbat;
    
       public class Main extends Sprite{
    		
    		// Variables
            public var net:Sprite;
    	public var myleftbat;
    		
    	public function Main(){
               net = new Sprite();
               myleftbat = new Leftbat();
    	   //
    	   net.graphics.lineStyle(1);
    	   net.graphics.beginFill(0xff0000);
      	   net.graphics.drawRect(225,0,50,400);
      	   addChild(net);
               addChild(myleftbat);
            }
        }
    }
    Code:
    package classes.bats{
    	
       import flash.display.Sprite;
    
       public class Leftbat extends Sprite{
    	
         public function Leftbat(){
    	graphics.lineStyle(1);
    	graphics.beginFill(0xffff00);
    	graphics.drawRect(0,0,50,400);
    	trace ("Hello");
         }
      }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2007
    Posts
    7
    Thats works great, thanks alot

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