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. :)
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.