Groups | Blog | Home
all groups > flash actionscript > february 2005 >

flash actionscript : problem with variable instance names


(_seb_)
2/14/2005 6:32:46 PM
Hi all

Here's the piece of script I'm having problems with (explanations of the
code's context follow):

_global.mytarget = any instance name;
sbx = 2;
lbx = 2;
(...)
switch (mytarget) {
case ["small_box"+sbx]:
mytarget.duplicateMovieClip(["small_box"+sbx],0,);
sbx = sbx+1;
break;
case ["long_box"+lbx]:
long_box.duplicateMovieClip(["long_box"+lbx],0,);
lbx = lbx+1;
break;
}

Basically, 'mytarget' is a global variable, which can take different
movieClip instance names as a value.
Now, I want to be able to duplicate these MCs, and give incremental
instance names to the duplicates.
So, if the instance name of the MC is 'small_box1', I want it to
duplicate into a MC that will be called 'small_box2'. If it's
'long_box1', duplicate into 'long_box2'.
So I have these variables
sbx for small_box... MCs and
lbx for long_box... MCs.

The problem is that the above switch/case conditional does not work, and
I suspect it doesn't because Flash doesn't evaluates ["small_box"+sbx]
correctly, or doesn't evaluates it at all in the context of
case ["small_box"+sbx]:

(_seb_)
2/14/2005 6:48:54 PM
I made some mistakes when I copied the code. Here is the correct
version(explanations of the code's context follow):

_global.mytarget = any instance name;
_global.sbx = 2;
_global.lbx = 2;
(...)
switch (mytarget) {
case ["small_box"+sbx]:
mytarget.duplicateMovieClip(["small_box"+sbx],0,);
sbx = sbx+1;
break;
case ["long_box"+lbx]:
mytarget.duplicateMovieClip(["long_box"+lbx],0,);
lbx = lbx+1;
break;
}

Basically, 'mytarget' is a global variable, which can take different
movieClip instance names as a value.
Now, I want to be able to duplicate these MCs, and give incremental
instance names to the duplicates.
So, if the instance name of the MC is 'small_box1', I want it to
duplicate into a MC that will be called 'small_box2'. If it's
'long_box1', duplicate into 'long_box2'.
So I have these global variables
_global.sbx for small_box... MCs and
_global.lbx for long_box... MCs.

The problem is that the above switch/case conditional does not work, and
I suspect it doesn't because Flash doesn't evaluates ["small_box"+sbx]
correctly, or doesn't evaluates it at all in the context of
case ["small_box"+sbx]:

(_seb_)
2/14/2005 7:25:20 PM
thanks Jeckyl

but I did the changes you suggested and it still doesn't work (mytarget
doesn't duplicate):

switch (mytarget._name) {
case "small_box"+sbx:
sbx++;
mytarget.duplicateMovieClip(["small_box"+sbx], 0);
break;
case "long_box"+lbx:
lbx++;
mytarget.duplicateMovieClip(["long_box"+lbx], 0);
break;
}

I tried both with
switch (mytarget._name)
and
switch (mytarget)

It does seems that case does not evaluate, unless something else is
wrong. The rest of my code, which uses the variable 'mytarget' works, so
this switch conditional is the only thing that does not work.

Also, you say that [...] creates an array, but what about:

mytarget.duplicateMovieClip(["long_box"+lbx], 0);

I don't mean to create an array of duplicated MC, just one each time, so
is this part of the code wrong too?

Just in case, I tried to change this to:

mytarget.duplicateMovieClip("long_box"+lbx, 0);

But that doesn't work either.
Any other suggestions?

[quoted text, click to view]
(_seb_)
2/14/2005 7:54:45 PM
I'm trying to do some tracing, but I must be missing something about
what 'tracing' is!
I added
trace(mytarget._name);
in the functions that use that variable,
and, I changed the code you saw before to:
dup = mytarget.duplicateMovieClip("long_box"+lbx, 0);
trace(dup);

but when I test the movie, the ouput box doesn't trace anuything at all.
However I know that 'mytarget' is never empty, I first define it as
_global.mytarget = whatever;
then I use it via some functions, and define it via some ClipEvents that
all work, so i know for a fact that as soon as I click an MC, 'mytarget'
becomes that MC's instance name.

Yet, nothing in the output box!

Am I missing the basics of "tracing"?

[quoted text, click to view]
(_seb_)
2/14/2005 8:21:47 PM
I can't be looking at the right output panel:

I placed
trace ( 'mytaget is ' + mytarget );
trace ( 'name = "' + mytarget._name + '"' );

just about everywhere in my code, and the 'output' panel stays
completely empty.

Should I mention that I never traced something before, so I might just
be missing something enormous?

the variable mytarget is the center of all my script, and I couldn't
drag my MCs as I can, if 'mytarget' was empty, since the drag function
looks like:

function drag(mytarget) {
...
}
then the function is applied to each MC like this:

small_box.onPress = function() {
drag(this);
}

so 'mytarget' just can't be empty, even less 'mytarget._name'!

What am I missing? Why is my 'output' panel completely blank when I test
my movie?

[quoted text, click to view]
(_seb_)
2/14/2005 9:29:11 PM
thanks for the idea, but that's not it.
Omit Trace is unchecked.
I don't understand.
If I have:

_global.mytarget = "whatever";
trace ( 'mytarget is ' + mytarget );

in the first keyframe of my movie, I assume I should see:

mytarget is whatever

in the output panel, right?

The output panel is blank. However, the panel just pops up when I test
my movie (and it doesn't if I have no trace commands).
I quit Flash and restarted, just in case. No success. Might there be
another basic thing I'm doing wrong?


[quoted text, click to view]
(_seb_)
2/14/2005 10:28:47 PM
I created a new Flash movie, and left the whole movie empty, except for
this code in the first keyframe:

_global.mytarget = "whatever";
trace ( 'mytarget is ' + mytarget );

when I test it, nothing appears in the output panel.

Is that to say that my Flash application is broken?
DAMN! I just spent the last 5 days trying to build this code and that
was already slightly more than what my nerves can take

I am going to have a nervous breakdown

please tell me I am just missing something very stupid

As anyone had this problme before?........

[quoted text, click to view]
mandingo
2/14/2005 11:54:59 PM
seb,

I haven't looked at your code in depth because once I saw this line :

case ["small_box"+sbx]:

I figured there was no point going further... a case will not evaluate the
result... you switch on a variable name and each case is a DEFINED value that
will be returned in the switch.

sorry, but I hope that helps
cheers
mandingo
2/15/2005 12:15:38 AM
(_seb_)
2/15/2005 12:22:15 AM
I uninstalled (actually I didn't, I just trashed the entire flash MX
2004 folder, because I don't have an "uninstaller" anywhere), and I
downloaded the trial version, installed it.
It didn't ask me for a serial number, I guess because I have all the
other studio MX 2004 apps already installed?...

in any case, the output window is still blank.


[quoted text, click to view]
mandingo
2/15/2005 12:57:23 AM
thanks Jeckyl, I have often wanted to write some ugly code in swtich statements
to determine if a value is between two values... now I can see how... :) now
you have opened a pandora's box for me.

Okay Seb, one other thing I just thought about... your duplicateMovieClip()
action is setting each clip to be at a depth of 0 (zero)... this won't really
work because each instance needs to be at a unique depth... so if something is
already at that depth it will just disappear and nothing else will appear at
that depth because of the conflict...

so even if your switch worked (logically) it still couldn't duplicate the
clips...

cheers
mandingo
2/15/2005 1:47:05 AM
okay... there is a setting in the Publish Settings ... make sure it is
unchecked...

File >> Publish Settings

Click the Flash Tab and there are some checkBoxes... about the 3rd one down
from memory is "Omit Trace" or similar... uncheck that then publish...
mandingo
2/15/2005 3:15:29 AM
this :

_global.mytarget = "whatever";
trace ( 'mytarget is ' + mytarget );

produces an output of :

mytarget is whatever

when tested all alone... start a brand new window and just test that and see
if you get any output... if you don't, then there is something wrong with your
application ... if you do... then that narrows it down to something conflicting
in your code.

mandingo
2/15/2005 5:43:19 AM
Yeah, I remember a heap of posts on the output not outputting about 12 - 18 months ago... and I don't recall anyone coming up with an answer other than start again.

Jeckyl
2/15/2005 10:47:48 AM
[quoted text, click to view]

no [] here .. why did you use them ?????? What that is doing is creating a
single-element array with a string in it and comparing that to the taget.

[quoted text, click to view]

same here

[quoted text, click to view]

It does evaluate it correctly .. you just don't seem to know what
'correctly' means :)

As above, [ ... ] creates an array eg.

x = [10,20,30];

creates an array with the numbers 10,20,30 in it and assigns it to x.

If mytarget is the NAME of an instance (ie a String variable), then just get
rid of the [] from the case to compare strings.

If mytarget is a REFERENCE to an instance (ie a MovieClip variable), then
you can use

switch (mytarget._name) {
case "small_box"+sbx:
....
}

or use

switch (mytarget) {
case this["small_box"+sbx]:
....
}

or

switch (mytarget) {
case eval("small_box"+sbx):
....
}

Not sure of the context of the script, so 'this' may not be correct .. hard
to tell.

NOTE: You logic for creating a new name 'small_box1', 'small_box2' etc is a
little off .. you should be doing the sbx = sbx+1;BEFORE you use it in the
duplicateMovieClip. And better to use sbx++ instead of sbx = sbx+1;

Jeckyl

Jeckyl
2/15/2005 11:00:42 AM
[quoted text, click to view]

Actually it will .. you can have expressions in the 'case' quite legally.

[quoted text, click to view]

Sorry .. that is not the reaon.

Jeckyl

Jeckyl
2/15/2005 11:01:35 AM
[quoted text, click to view]

Its still got the logic wrong ... as well as the incorrect use of [ ] in the
case.
--
All the best,
Jeckyl

Jeckyl
2/15/2005 11:34:26 AM
[quoted text, click to view]

I'd say the latter .. something else is wrong.
Do some tracing on your variable and make sure you can access them and they
have the values you expect

[quoted text, click to view]

There is a big difference between
object[value]
and
[value,value,value]
the first accesses the object like an array, the second creates an array

[quoted text, click to view]

Yes .. no [] in the duplicateMovieClip .. should not be there

[quoted text, click to view]

that is correct

[quoted text, click to view]

There is some other problem then that we cannot see from what yuou've shown
us. Trace trace and more trace to find out for yourself what is going on.

Jeckyl

Jeckyl
2/15/2005 11:38:03 AM
in fully compiled languages, like C/C++, the case value has to be a
constant.

in interpreted and semi-interpreted languages like ActionScript and
JavaScript, the value after the keyword 'case' can be any expression.

One sometimes sees that rather ugly, but still valid code like this

switch (true) {
case x == 0:
...
case y == 0:
...
case x < y:
...
}

A very ugly and inappropriate way of using switch/case (I hate even writing
it here :)) .. but it works.
--
All the best,
Jeckyl

Jeckyl
2/15/2005 12:00:33 PM
if there is nothing in the output box, then your code is not being executed.
UNLESS mytarget._name is blank.

try instead (so you know)

trace ( 'mytaget is ' + mytarget );
trace ( 'name = "' + mytarget._name + '"' );

and
trace ( 'dup is ' + dup );

and for good measure, before and after you use it

trace ( 'lbx = ' + lbx);

etc
--
All the best,
Jeckyl

Jeckyl
2/15/2005 12:01:38 PM
[quoted text, click to view]

There were quite a few problems in the code .. and probably just as many in
areas we haven't seen yet :)

--
All the best,
Jeckyl

Jeckyl
2/15/2005 3:14:50 PM
see here

http://groups-beta.google.com/group/macromedia.flash/browse_thread/thread/230986488bc7302b/f9f3c2e3b80d818d?q=Flash+MX+2004+Output+blank+OR+working+OR+error+OR+problem&_done=%2Fgroups%3Fq%3DFlash+MX+2004+Output+blank+OR+working+OR+error+OR+problem%26start%3D20%26hl%3Den%26lr%3D%26&_doneTitle=Back+to+Search&&d#f9f3c2e3b80d818d
[quoted text, click to view]
I originally install Flash MX 2004 from CD, download 7.01 patch from web and
apply. Everythings going along fine until.....one day my friggin output
window stopped displaying anything. Omit trace is unchecked, double
unchecked, etc. I tried every setting, different files, even a blank file
with just a trace and stop in it. Nothing!

Finally noticed a post from a Mac user with the same problem. He uninstalled
Flash, downloaded the trial version from the web, installed & re-activated
it with his serial number and it worked fine. Well, I can now verify that it
also works on Windows.
[quoted text, click to view]

http://groups-beta.google.com/group/macromedia.flash.actionscript/browse_frm/thread/d15ce68976afc2bb/ae68758d39bd3028?q=Flash+MX+2004+Output+blank+OR+working+OR+error+OR+problem&_done=%2Fgroups%3Fq%3DFlash+MX+2004+Output+blank+OR+working+OR+error+OR+problem%26start%3D30%26hl%3Den%26lr%3D%26&_doneTitle=Back+to+Search&&d#ae68758d39bd3028
[quoted text, click to view]
After 3 days of faffing around I've finaly got it to work. I'm not sure but
I think the problem is with the installer on the CD. I downloaded the Flash
MX2004 trial software from the Macromedia site and then registered it with
my key (supplied when I bought the boxed software). Surprise surprise it
worked. So if you have installed FlashMX from the CD try downloading a trial
version and registering that instead. Now all I have to do is make up 3 days
of lost chargable time.
[quoted text, click to view]

Lots of other posts on this problem.. but all the rest have no solution for
it. Looks like uninstall reinstall may be your best bet!
--
All the best,
Jeckyl

Jeckyl
2/15/2005 4:26:36 PM
I would uninstall .. trashing a folder is not the same as uninstall as the
registry settings would still remain etc.

If that fails, buggered if I know what you can do .. except try to get
support from Macromedia ... good luck.
--
All the best,
Jeckyl

AddThis Social Bookmark Button