A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: how reference a method value

Hybrid View

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    6

    Question how reference a method value

    Hi I am trying to understnd some of the things about packages, I have expand a book exercise and I am now trying to workout how to reference a return value within this package.
    I have put together this package

    Code:
    package classes
    {
    	import flash.display.*;
    	import flash.utils.getDefinitionByName;
    	
    	public class SmileySwitch	
    	{
    	    private static function get SmileySwitch1()
    			{	
    
    		var s1:String = gotSpot("smiley" + 8);
    				
    			 function gotSpot(s1:String):MovieClip
    				{	
    				var spot1a:Class = Class(getDefinitionByName(s1));
    				var spot1:MovieClip = new spot1a();
    				return(spot1);
    				}				
    			}
                    public static function get getSmileySwitch1
                                          {
                                             var spot1 = new smiley1();
                                              return(spot1);
                                          }
                  }
    }
    What I am trying to do is use the 'return(spot1);' value where 'new smiley1(); is.

    My goal is to be able to create the library object name, (in this case smiley1 from a string variable and then display it on the stage with addChild. At this stage I am just trying to work out how to swap 'new smiley1' with the 'return' value.
    I will work on the addChild bit next, but happy to take any tips on that too.

    Thanks in advance for you help.

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

    Smile Step back and clean it up

    Bob,

    Understand package isn't too difficult, but can be confusing until the moment you click and everything becomes clear. You're version is making a simple process a little more complicated than it needs to be.

    First, let me show an example of a modified package.

    PHP Code:
    package classes
    {
        
    import flash.display.*;
        
    import flash.utils.getDefinitionByName;
        
        public class 
    SmileySwitch
        
    {
            public function 
    SmileySwitch(passedVar:String)
            {
                
    /*
                *The variable passedVar was sent when this object (class) was instantiated
                *In this case we will say 'smiley1' is the content of the variable
                
                *Unless this is a class package, we are going to assume that it is an object is in the library
                */
                
                
    var TempObjRef:Class = getDefinitionByName(passedVar) as Class;
                var 
    tempObj:Object = new TempObjRef();
                
                
    /*
                 * I can return just the new class object and have it placed on the stage by the caller using
                 * addChild
                 */
                
                 
    return tempObj;
                
                 
    /*or I can make it easy on myself and just have it added now.  If you add it now, then it
                 * will be associated with this class, and they gives you better control.
                 */
                
                 
    this.addChild(tempObj);
            }
        }

    First, you'll notice there are some differences between out code. The basic building blocks of a class package are all the same. You need the package, the class bounds and at least the class function. Of course, you can many functions with a class, but it is best to limit it to the function specific for this class.

    In your class, you are using getters to retrieve information from a class without a class function. Notice that the class function, class bound and the file name match exactly. It is a requirement.

    My version will accept a parameters, in this case a string, that represents anything you want, I guess mine is an object on the stage. Using getDefByName, I convert that object into a Class and then I can either, return the new class object to the caller or I immediately place it on the stage (class level). No need for any getters or setters, which I only use to protect variables within my class.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    6

    I am missing something

    Hi Samac,
    Your explanation is really good and helped clarify things but I still think I am missing the obvious. Can't see the wood for the trees type of thing.
    This is what I have put together

    Code:
    package classes 
    { 
        import flash.display.*; 
        import flash.utils.getDefinitionByName; 
         
        public class SmileySwitch 
        { 
            public function SmileySwitch(smiley1:String) 
            { 
                var spot1:Class = getDefinitionByName(smiley1) as Class; 
                var position1 = new spot1();
                trace(position1);
               // return spot1; 
               this.addChild(spot1); 
                spot1.x = 40;
                spot1.y = 100;
            } 
        } 
    }
    In my SmileyTest.fla file I have

    import classes.SmileySwitch

    I was hoping this would display the smiley1 object in the library on the stage at x40/y100.

    But I get nothing, the trace(position1) does not show in the Output window either. I keep looking at this thinking the bit I'm missing must be obvious but I cant see it.

    The smiley1 image in the library has the following properties.
    Class:smiley1, Base class: flash.display.Movieclip

    I really appreciate your help. I think I should be able to work this out but after referring to some books and google I still can't find the answer.

    The end goal of my self created exercise is to be able to import the numbers from 1 to 10 from a text file and add them to the word 'smiley' and then have the 10 smiley objects in the library named smiley1 to smiley10 appear on the stage. Step 1, get smiley1 to appear on the stage.

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