A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: addChild problem

  1. #1
    infidel! 0vidiu's Avatar
    Join Date
    Mar 2003
    Location
    ROmania
    Posts
    267

    addChild problem

    Hello!

    Here is mai document class:

    Code:
    package  
    {
    	import flash.display.MovieClip;
    	
    	/**
    	 * ...
    	 * @author infidel - aka Ovidiu Gheorghe
    	 */
    	public class mainTest extends MovieClip
    	{
    		
    		public function mainTest() 
    		{
    			addBg();
    			addSquare();
    		}
    		
    		private function addBg():void
    		{
    			var myBg:bg = new bg();
    			myBg.x = 0;
    			myBg.y = 0;
    			myBg.alpha = 0.15;
    			
    			addChild(myBg);
    		}
    		
    		private function addSquare():void
    		{
    			var redSquare:square = new square();
    			redSquare.x = stage.stageWidth / 2;
    			redSquare.y = stage.stageHeight / 2;
    			
    			myBg.addChild(redSquare);
    		}
    		
    	}
    	
    }
    The addSquare() function returns this error:
    "1120: Access of undefined property myBg."

    But if I move all the lines from addSquare() function into the addBg() function, everything goes well!

    Can somebody please explain why?... and what is the correct way to implement this (basically I want the redSquare mc to be attached in myBg)?

    Tnx a bunch,
    Ovidiu

  2. #2
    Junior Member
    Join Date
    Oct 2008
    Posts
    26
    The variable myBg:bg that you create only has scope within the addBg() method, this is why addSquare has no knowledge of it, and this is why the code works when you move the code to the addBg() method. You should define myBg:bg as a class variable, and then all methods of that class will have access to that variable.
    //peace.*

  3. #3
    infidel! 0vidiu's Avatar
    Join Date
    Mar 2003
    Location
    ROmania
    Posts
    267
    thank you! here is a workin' version
    kind regards!

    Code:
    package  
    {
    	import flash.display.MovieClip;
    	
    	/**
    	 * ...
    	 * @author infidel - aka Ovidiu Gheorghe
    	 */
    	public class mainTest extends MovieClip
    	{
    		var myBg:bg = new bg();
    		
    		public function mainTest() 
    		{
    			addBg();
    			addSquare();
    		}
    		
    		private function addBg():void
    		{
    			//var myBg:bg = new bg();
    			myBg.x = 0;
    			myBg.y = 0;
    			myBg.alpha = 0.15;
    			
    			addChild(myBg);
    		}
    		
    		private function addSquare():void
    		{
    			var redSquare:square = new square();
    			redSquare.x = stage.stageWidth / 2;
    			redSquare.y = stage.stageHeight / 2;
    			
    			myBg.addChild(redSquare);
    		}
    		
    	}
    	
    }

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