PDA

View Full Version : Using with statement w/ dynamic object names


StupidRalph
07-22-2007, 10:43 PM
I have been unable to use the with statement with dynamic object names. I've even tried storing the object name in a variable and placing the variable in the with statement. Currently, I'm using tellTarget which of course is deprecated.

Here is sample code


//this code will work and will tell objects "item_1","item_2","item_3","item_4", and "item_5" to gotoAndStop on frame 3

for (i = 1; i <= 5; i++)
{
tellTarget("item_" + i)
{
gotoAndStop(3);
}
}


//this code will not work however
for (i = 1; i <= 5; i++)
{
with("item_" + i)
{
gotoAndStop(3);
}
}


//even this wont work for me.

var obj = '';
for (i = 1; i <= 5; i++)
{
obj = "item_" + i);
with(obj)
{
gotoAndStop(3);
}
}


Any suggestions? I'm using AS2 in flash player 8.

_Aerospace_Eng_
07-22-2007, 11:32 PM
I've always used the dot notation as an alternative.
for (i = 1; i <= 5; i++)
{
var target:Object = "item_"+i;
target.gotoAndStop(3);
}
Try that.

StupidRalph
07-26-2007, 01:15 AM
Sorry for the delay...

Yes I'm sure that'll work but I was planning on using it like


with(_root.movieClip){
_alpha = 100;
_xscale = _yscale = 80;
_x = 100;
_y = 200;
myVariable = "hello world";
}

//equivalent to writing

_root.movieClip._alpha = 100;
_root.movieClip._xscale = _yscale = 80;
_root.movieClip._x = 100;
_root.movieClip._y = 200;
_root.movieClip.myVariable = "hello world";
}



Which isn't really a big deal more of a style concern. In Adobe'stellTarget documentation (http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary725.html) it states that you should use dot notation and the with statement. But the with statement doesn't appear to work for me. I'll do somemore testing and see what I can get.

Omnis
07-27-2007, 06:58 PM
Obviously the tellTarget evaluates what's in the () whilst with does not, so all you have to do is use the eval() function. For references see the ActionScript Dictionary (http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary200.html) or LiveDocs (http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00005702.html) at Adobe.

for (i=1; i<=5; i++) {
with (eval("item_" + i)) {
gotoAndStop(3);
}
}



In reply to your previous post:


with(_root.movieClip){
_alpha = 100;
_xscale = _yscale = 80;
_x = 100;
_y = 200;
myVariable = "hello world";
}


What I've highlighted in bold above does not work in Flash as far as I know, if you want to use the value of _yscale your going to have to have _yscale equal to 80 first and then use the _xscale equal to _yscale like so:


_yscale = 80;
_xscale = _yscale;

_Aerospace_Eng_
07-27-2007, 07:08 PM
This actually might work
_xscale = _yscale = 80;
Because you can do something similar with event handlers like so
instance.onRelease = instance.onReleaseOutside = function()
{
// do something
}

StupidRalph
07-28-2007, 05:15 AM
_xscale = _yscale = 80;

Yes, it works...I actually picked that up from Lee Brimelow. www.gotoAndLearn.com :thumbsup:

Omnis
07-28-2007, 06:15 AM
Yeah, your right it does work ;)


_xscale = _yscale = 80;


instance.onRelease = instance.onReleaseOutside = function()
{
// do something
}

_Aerospace_Eng_ is there a doc at adobe talking about using the equals sign twice? I actually haven't encountered much of that code yet.

_Aerospace_Eng_
07-28-2007, 06:27 AM
I saw it on one of Lee's tutorials at gotoandlearn.com

I believe it was the mp3 player tutorial.