|
-
vars help
so I'm making this game,
I have around 1000 lines of code,
everything's fine.
then, at some point, I start getting an error in the line that looks something like this:
Code:
eval("_root.myclip.var" add varnum).varX = X;
for almost 10 minutes I can't figure out what's wrong with this line,
then I start suspecting that maybe you can't have variable inside of variable,
so I change the code and everything works.
now it's not that important or anything it's just that I'm wondering if it's possible to have a variable inside of a variable in any way or not? if it is then how would you do it?
I know arrays might be an answer, but is there any other way?
-
383,890,620 polygons
erm everything?
nah, serious, this is a very odd way to do it. i guess you have an object called var1 (or something like that), with the number stored in "varnum"? and you wanted to access a propperty called varX? (if it is a movieclip it behaves the same way).
so why not use:
Code:
_root.myclip["var" + String(varnum)].varX = X;
this should work - and it doesn't use the depriciated eval command anymore.
nGFX
-
the thing is that var1 and varX are both vaiables.
so, basically, I'm trying to put the variable inside of a variable.
here's an example:
let's say there's a movie clip called "section" and then there's a variable "type_of_building". Then I need to add some more variables to variable "type_of_building" like "date_build" or "cost" or whatever. It'd be simple if "tpe_of_building" was a movie clip, but it's a variable.
and what's wrong with "eval"?
-
Senior Member
Maybe its because variable is undefined. You must create this variable
"_root.myclip.var" add varnum
before you can add another variable inside it.
This works:
myclip = {};
varnum = 1;
X = 2;
eval("_root.myclip.var" add varnum) = {};
eval("_root.myclip.var" add varnum).varX = X;
-
383,890,620 polygons
k.
then you have to make "var1" an object, then you can "attach" variables to it.
Code:
// the long way ...
_root.myclip["var" + String(varnum)] = new Object();
_root.myclip["var" + String(varnum)].varX = X;
_root.myclip["var" + String(varnum)].varY = Y;
// the short way
_root.myclip["var" + String(varnum)] = {varX: X, varY: Y};
and what's wrong with "eval"?
hm. thinking about it, nothing technically. it just looks odd (like "tellTarget" or "set"). (and [] is way easier to read when combined, imho)
nGFX
-
 Originally Posted by tonypa
Maybe its because variable is undefined. You must create this variable
"_root.myclip.var" add varnum
before you can add another variable inside it.
This works:
myclip = {};
varnum = 1;
X = 2;
eval("_root.myclip.var" add varnum) = {};
eval("_root.myclip.var" add varnum).varX = X;
thanks Tony,
I'll try it as soon as I get home. But untill then can you explain 1 thing (I'm not too familiar with advanced coding):
myclip = {}; - what it does? is it something like myclip = "";?
and nGFX,
I use 'tellTarget' and 'set' all the time 
there are situations you can't do without them.
-
383,890,620 polygons
the {} is the shorthand for Object();
so
Code:
var myObject = new Object();
// is the same as:
var myObject = {};
as for set and tellTarget ... you *can* do without them 
nGFX
-
 Originally Posted by nGFX
as for set and tellTarget ... you *can* do without them  nGFX
i have an example at home,
i'll post it tomorrow
-
SaphuA
Would love to see it then 
I've always hated tellTarget, and never had to use it...
-
Hype over content...
I still use it, quite a lot actually, although I must admit I don't even know the syntax for "set".
( The Flash4 tuts on this site have a lot to answer for ).
Squize.
-
ism
i don't think there's any alternative for geting the typeof() for a variable outta a for loop without using eval(). i hate it when developers remove functionality to satisfy their own vision for a product.
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
-
Senior Member
 Originally Posted by MechaPiano
thanks Tony,
I'll try it as soon as I get home. But untill then can you explain 1 thing (I'm not too familiar with advanced coding):
myclip = {}; - what it does? is it something like myclip = "";?
I didnt have movie clip in this example code, so I created object with name "myclip". If you have movie clip already then you dont need this line because in Flash movie clips are objects too, only they have more properties then normal objects.
Think about objects as movie clips without graphical representation, you cant see them and they dont have coordinates or width, they only exist virtually in the memory. But you can create, modify and access variables inside the objects just like you can in the movie clips.
-
SaphuA
So.. where's the cool Flash 4 syntax code that's can't be done in Flash 5 syntax?
Still wondering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|