Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

878940 Posts in 32946 Topics- by 24353 Members - Latest Member: kanki

May 23, 2013, 01:23:51 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Actionscript 3: Assistance required
Pages: 1 ... 3 4 [5]
Print
Author Topic: Actionscript 3: Assistance required  (Read 11854 times)
isaac
Level 2
**



View Profile WWW
« Reply #60 on: October 30, 2008, 03:28:02 PM »

Anyone know if there is a way to convert a string into an object reference? Say, something like:

Code:
"tile" + number.toString()

To get references to objects named 'tile0' through 'tile9' in a for loop, for instance.

Yes, but that's pretty much the worst way of managing your tiles. Just use an array!

You'll need to create the variable in an Object:

Code:
var myOb:OBject = new Object();
myOb["tile1"] = 23;

trace( myOb.tile1 );   // traces "23"
trace( myOb["tile1"] );   // also traces "23"


Buuuuutttt.... that's a technique I only use for very specific reasons, and storing an array of tiles is not one of them. Just using:

Code:
var myArray:Array = new Array();
myArray.push( new Tile() );
myArray.push( new Tile() );

for each ( var tile:Tile in myArray ){
    // dothings with yer tiles.
}

.. is much safer and much faster.
Logged

bateleur
Level 10
*****



View Profile
« Reply #61 on: October 30, 2008, 03:47:23 PM »

Anyone know if there is a way to convert a string into an object reference?

Isaac's advice is good, but just in case you're curious... In the specific case of tiles defined in your SWF's Library, you can do stuff like this:

Code:
var classObject:Class = getDefinitionByName("Tile" + number) as Class;
var myTile:DisplayObject = (new classObject() as DisplayObject);

That assumes your classes for the tile symbols in your library are called "Tile1", "Tile2" etc. and that they have zero-argument constructors.

This is sometimes the neatest way to pull large quantities of stuff out of the Library in a purely programmatic way.

(Disclaimer: Code section above written from memory and not tested!)
Logged

agj
Level 10
*****



View Profile WWW
« Reply #62 on: October 31, 2008, 09:47:54 AM »

Thanks, you guys, a combination of the square bracket syntax and getDefinitionByName() should do. And just for the record, I'm not really doing anything with tiles, it was just as an example. :)
Logged

Don Andy
Level 10
*****


Andreas Kämper, Dandy, Tophat Andy

tophat@live.de
View Profile Email
« Reply #63 on: November 20, 2008, 04:58:43 AM »

And here I am again :D

I've got a little problem with defining a constant (or generally just a variable) that every class in the project is supposed to access.

But I don't really know how I'd do that. I've tried a C++-esque attempt, by defining the variable in a "Global.as" and then importing it everywhere I need. Sadly, it isn't able to read the variable, so I probably did something wrong.
Logged
bateleur
Level 10
*****



View Profile
« Reply #64 on: November 20, 2008, 09:09:10 AM »

I've got a little problem with defining a constant (or generally just a variable) that every class in the project is supposed to access.

The approach you seem to be attempting works like this:

* Make your constant a public static field of some class (say "Global") and then access it as "some.package.path.Global.myFieldName".
* As above, but use "import some.package.path.Global" to allow you to use the shorter "Global.myFieldName" to refer to it.
Logged

Selben Coirlo
Level 8
***


totally awesp,e/

selbencoirlo
View Profile WWW
« Reply #65 on: November 20, 2008, 09:37:11 AM »

The key word here is static. I'm guessing you didn't declare it as such, and if you don't then you need to instantiate the class (and with it the const) before you can access it. Usually I define consts as static, they tend to be more useful that way and take up less memory.

Also, make sure it's public. Just in case. Tongue
Logged

agj
Level 10
*****



View Profile WWW
« Reply #66 on: November 23, 2008, 07:59:31 PM »

Umm, just in case:

Code:
// Declare as:
public static const myConstant:String = "This is my constant";

// Access with:
private var myVar:String = Class.myConstant;
Logged

Pages: 1 ... 3 4 [5]
Print
Jump to:  

Theme orange-lt created by panic