Be a Supporter!

As: Turning A String Into Code

  • 1,404 Views
  • 16 Replies
New Topic Respond to this Topic
Alphabit
Alphabit
  • Member since: Feb. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
As: Turning A String Into Code Feb. 9th, 2007 @ 12:48 AM Reply

AS: Main

Ok, you might have wondered many times how to turn a string into code at runtime, or, if you're a sane person unlike me, you might not. In any case, there are situations where you pretty much have to in order to keep track of dynamically-generated objects.

For example, create a new .fla (AS2), make a new MovieClip called "b_mc" and on the frame insert this sample code:

var c:Number = 0;
b_mc.onPress=function():Void {
var clip:MovieClip = this.duplicateMovieClip("mc"+c++, _root.getNextHighestDepth());
clip.startDrag();
clip.onPress = startDrag;
clip.onMouseUp = stopDrag;
}

// Basically, it will allow you to drag a copy of your movieclip on the stage.

... Now, lets say that you have a function after this code that aims to remove the second MovieClip duplicate once any key is pressed.
You know from the duplicateMovieClip() that the second duplicate will be called "mc1" (remember the first is going to be "mc0").

Anyway, so how are you going to go about making mc1 disappear??

Well... The answer is using the an "Assotiative Array" that points to the parent of the MovieClip in evidence.
An associative array, is an array that references an elements by name(string) rather than by an index (number).

So, in our case, to access the "mc1" MovieClip, we would need to use:

_root["mc1"]

or even:

_root["mc"+1]

Flash automatically creates associative arrays for every Object and MovieClip attached to a timeline. Basically, by saying "_root[]" we are looking for an object in the _root[] that is called "mc1."

So here's the code:

var c:Number = 0;
b_mc.onPress = function():Void {
var clip:MovieClip = this.duplicateMovieClip("mc"+c++, _root.getNextHighestDepth());
clip.startDrag();
clip.onPress = startDrag;
clip.onMouseUp = stopDrag;
};
listener = new Object();
listener.onKeyDown = function():Void {
removeMovieClip(_root["mc"+1]);
};
Key.addListener(listener);

In some cases you would need to use this["childname"] or even mymovieclip["childname"].

Also, using this method, you can even call a method from a string:
e.g:

_this["methodname"]()

You need to have the "()" afterwards!
... I discovered that one by myself by the way.

THE END.
PS. This is one of the most important concepts in AS; one that defines intermediate devs from beginners.

Enjoy :)))


Bla

Alphabit
Alphabit
  • Member since: Feb. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 12:54 AM Reply

At 2/9/07 12:48 AM, LolOutLoud wrote: // Basically, it will allow you to drag a copy of your movieclip on the stage.

... Many MovieClips, In fact, you would need to drag-out more than one if you want the _root["mc1"] to do anything.


Bla

Ertyguy
Ertyguy
  • Member since: Feb. 2, 2006
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 01:00 AM Reply

honestly that looks pretty complicated

Alphabit
Alphabit
  • Member since: Feb. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 01:04 AM Reply

At 2/9/07 01:00 AM, Ertyguy wrote: honestly that looks pretty complicated

It's not.

If you have a String name of a movieclip, you can access the actual movieclip using this format:

parent_name["movieclipname"]

so if the parent (the movieclip that contains the movieclip you want to access) is the _root, just use:

_root["movieclipname"]._x=100

... to set the _x position to 100.

it's not that hard, just pretend that _root["movieclipname"] is actually just movieclipname assuming that it's on the root.


Bla

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 04:15 AM Reply

I hate evaluating strings on a basis... But you forgot to mention the global 'set' and 'eval' methods.
Anyway, it's pretty important because it's stupid when people do...
this[id + '_piece' + i + '::' + j]._x
+= this[id + '_piece' + i + '::' + j]._x - this[id + '_piece' + i + '::' + j]._parent._x >> 1;
this[id + '_piece' + i + '::' + j]._y
+= this[id + '_piece' + i + '::' + j]._y - this[id + '_piece' + i + '::' + j]._parent._y >> 1;

...because there is no damned point in allocating two strings and add them up with a bunch of numbers SIX TIMES to do a simple function. It seriously slows it down a good few dozen times.
>:(
I suggest...
var p = this[id + 'piece' + i + '::' + j];
p._x += p._x - _xmouse >> 1, p._y += p._y - _ymouse >> 1;


BBS Signature
Alphabit
Alphabit
  • Member since: Feb. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 08:05 AM Reply

At 2/9/07 04:15 AM, OldGust wrote: I hate evaluating strings on a basis... But you forgot to mention the global 'set' and 'eval' methods.

I never use the eval() method, it's probably a bit faster to use the scope["movieclip"] approach because then flash player knows straight away where to find the MovieClip.

I had never heard of the set() method before now lol. I guess that since AS3, I have become a full-on strict data-typer and never really needed something like this... It's good to know though.

I suggest...
var p = this[id + 'piece' + i + '::' + j];
p._x += p._x - _xmouse >> 1, p._y += p._y - _ymouse >> 1;

Well suggested.

P.S. Why do you use the right shift operator? I know what it does, but I never really understood it's purpose in a practical situation.


Bla

Alphabit
Alphabit
  • Member since: Feb. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 08:07 AM Reply

At 2/9/07 08:05 AM, LolOutLoud wrote: P.S. Why do you use the right shift operator? I know what it does, but I never really understood it's purpose in a practical situation.

I meant; "its purpose in a practical situation."
Lol NG is having a bad effect on my grammar.


Bla

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Turning A String Into Code Feb. 9th, 2007 @ 08:17 AM Reply

It's alot faster than division by two and it rounds to integers automatically (even in AS, Number is teh double). But ofcousre it's generally used in bit manipulation.


BBS Signature
phyconinja
phyconinja
  • Member since: Sep. 18, 2004
  • Offline.
Forum Stats
Member
Level 25
Blank Slate
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 11:44 AM Reply

I don't know if you mentioned this.. but you need to define where the object is.. like:

["MC"+1]
wont work.. while
_root["MC"+1]
will..
even if the code is in the main timeline.

Cybex
Cybex
  • Member since: Mar. 4, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 12:07 PM Reply

Whats wrong with using _root.mc1? _root["mc1"] is pointless. I always thought that method was only used when wanting to talk about more than one mc, such as:

for(i=0, i<=c, i++){
_root["mc"+i];
}
Which would remove all of the created movieclips. You made it seem like the _root[] thing was normally used for just reffering to 1 movieclip, in which case you don't even need to use it.

Good idea for a tutorial though.

Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 12:12 PM Reply

At 2/16/07 12:07 PM, Cybex wrote: Whats wrong with using _root.mc1? _root["mc1"] is pointless.

I suppose it gets you into the habit of doing it for 'for', 'while' etc loops. I wonder if its is quicker to do '_root.mc' rather than '_root["mc"]' does somone know how to do a speed test with movieclips rather than variables?

Good tut :D.

BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 12:40 PM Reply

At 2/16/07 12:12 PM, Depredation wrote: I wonder if its is quicker to do '_root.mc' rather than '_root["mc"]'

AHEM. Why yes, it is indeed faster grabbing a pointer instead of allocating a string and then looping through the names of all the instances comparing all the strings.


BBS Signature
Cybex
Cybex
  • Member since: Mar. 4, 2005
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 12:48 PM Reply

If this was a maths/science forum i'd totally pwn Gust. Sadly, it's flash so he wins always :(

GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 12:51 PM Reply

It's basic computing, not much to do with Flash. And I do have better things to learn about than math that can only be applied to more complex math.


BBS Signature
Depredation
Depredation
  • Member since: Sep. 5, 2005
  • Offline.
Forum Stats
Member
Level 17
Game Developer
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 01:28 PM Reply

At 2/16/07 12:40 PM, GustTheASGuy wrote: AHEM. Why yes, it is indeed faster grabbing a pointer instead of allocating a string and then looping through the names of all the instances comparing all the strings.

Well that was obvious :(. I really need to try things before asking questions don't i :P.


BBS Signature
Alphabit
Alphabit
  • Member since: Feb. 14, 2006
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 08:28 PM Reply

At 2/16/07 11:44 AM, phyconinja wrote: I don't know if you mentioned this.. but you need to define where the object is.. like:

["MC"+1]
wont work.. while
_root["MC"+1]
will..
even if the code is in the main timeline.

Yeah, I mentioned that.

Side note: _root is not recommended, I just put it because it is used by many people. The reason is that if you import another swf inside your movie, the root of that swf will not be the root of your stage, but its own root. In most cases _parent[] is recommended.

At 2/16/07 12:07 PM, Cybex wrote: Whats wrong with using _root.mc1? _root["mc1"] is pointless. I always thought that method was only used when wanting to talk about more than one mc, such as:

Well, I guess you're right, it is most often used to loop though movieclips. So I should probably have made mention to that. But it is also useful if, for example, you have a textbox and the user of your application writes the name of a movieclip inside it and presses a button... Now the text is a string, so thanks to this method, you can turn it into a movieclip or in fact, any object in your movie. (as shown by my last example ere I turned a string into a method call.)

Good idea for a tutorial though.

Thanks :)
A lot of people know how to do it but it isn't documented very well... Even on the net it's hard to get info on it. So it's hard to find out about.


Bla

Skeik-Sprite
Skeik-Sprite
  • Member since: Jun. 19, 2005
  • Offline.
Forum Stats
Supporter
Level 16
Game Developer
Response to As: Turning A String Into Code Feb. 16th, 2007 @ 09:09 PM Reply

So I can use [] to call functions and methods?

Sweet.