A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [AS3] Super Simple Custom Component in CS4

  1. #1

    [AS3] Super Simple Custom Component in CS4

    In AS2, you could simply add a component definition to a clip in the library, then add a variable, and it would be instantly accessible from within that clip. In AS3, this doesn't work.

    I have a variable myNum in the component definition, but my class file can't get to it. I added an ADDED_TO_STAGE listener thinking that maybe it couldn't get to the variable because it wasn't declared yet, but still no dice. And if I don't put var myNum:Number; I get an error. So I'm likely just overwriting the myNum in the component inspector with the myNum in the class definition.

    I looked at the Adobe Tutorial on Creating ActionScript 3.0 Components, but if there is a simple solution in the many pages there I can't seem to find it.

    Any ideas? All I need to do is access a single variable that is defined via the component inspector.

    Code:
    package {
    
    	import flash.display.MovieClip;
    	import flash.events.Event;
    
    	public class Component extends MovieClip {
    
    		public var myNum:Number;
    
    		public function Component() {
    			trace(this.myNum);
    			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    		}
    		
    		private function onAddedToStage(event:Event):void{
    			trace(this.myNum);
    			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
    		}
    
    	}
    	
    }

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    First of all you need to declare your variable in a different way. here is an example:
    [Inspectable(name = "myBlur",type = int,defaultValue = 0)]
    public function set myBlur (mb:int):void
    {
    _myBlur = mb;
    }
    public function get myBlur ():int
    {
    return _myBlur;
    }
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Thanks so much! This works great!

    Code:
    package {
    
    	import flash.display.MovieClip;
    
    	[Inspectable(name = "myNum",type = uint,defaultValue = 0)]
    
    	public class Component extends MovieClip {
    
    		private var _myNum:uint;
    
    		public function Component() {
    			trace(myNum);
    			_myNum = myNum;
    			trace(_myNum);
    		}
    
    		public function set myNum(num:uint):void {
    			_myNum = num;
    		}
    		
    		public function get myNum():uint {
    			return _myNum;
    		}
    
    	}
    
    }
    Note to others if using this for reference: You may need to use int, Number, String... instead of uint for your purposes.
    Last edited by sirzooass; 11-21-2009 at 11:37 AM.

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