|
|
|
#1 |
|
Junior Member
Join Date: Nov 2008
Posts: 5
|
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);
}
}
}
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 |
|
Flash/Flex Developer
Join Date: Apr 2007
Location: Maryland
Posts: 1,152
|
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:
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.
__________________
SamacFascinating. |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Nov 2008
Posts: 5
|
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;
}
}
}
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.
|
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|