instance name in a for loop

  • susancbk
  • Proficient
  • Proficient
  • susancbk
  • Posts: 293
  • Loc: New York City

Post 3+ Months Ago

i have the following for loop which goes through an array to display images listed in the array.

my problem comes in with the instance name used in the createEmptyMovieClip-- this instance name needs to be unique each time so as to not override the previous image-- i created it to be unique by creating a variable which is changed each time based on what i is.... for some reason this dosent work as an instanceName though for the createEmptyMovie clip

Code: [ Select ]
for (i=0; i<pics.length;i++) {
var img = 'myImg' + i;
trace (img);
_root.createEmptyMovieClip(img, i + 6);
img._x = i * 2;
img._y = i * 2;
img._xscale = 16;
img._yscale = 16;
_root.img.loadMovie('images/' + pics[i]);

}
  1. for (i=0; i<pics.length;i++) {
  2. var img = 'myImg' + i;
  3. trace (img);
  4. _root.createEmptyMovieClip(img, i + 6);
  5. img._x = i * 2;
  6. img._y = i * 2;
  7. img._xscale = 16;
  8. img._yscale = 16;
  9. _root.img.loadMovie('images/' + pics[i]);
  10. }


any suggestions?
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

I believe it's the way you are addressing it after creation. The variable 'img' only houses a string, not a target clip. So when you use img._x its not targeting the propert clip, it should instead be something like _root[img]._x. But there is a shorthand way to do this... and that is housing the clip creation in a variable name.

Code: [ Select ]
for (i=0; i<pics.length;i++) {

img = _root.createEmptyMovieClip('myImg'+i, i + 6);
img._x = i * 2;
img._y = i * 2;
img._xscale = 16;
img._yscale = 16;
_root.img.loadMovie('images/' + pics[i]);

}
  1. for (i=0; i<pics.length;i++) {
  2. img = _root.createEmptyMovieClip('myImg'+i, i + 6);
  3. img._x = i * 2;
  4. img._y = i * 2;
  5. img._xscale = 16;
  6. img._yscale = 16;
  7. _root.img.loadMovie('images/' + pics[i]);
  8. }
  • susancbk
  • Proficient
  • Proficient
  • susancbk
  • Posts: 293
  • Loc: New York City

Post 3+ Months Ago

smart solution. i couldnt wrap my brain around it at the time. Works like a charm.

right now these mc's are being created on the main timeline.. but I want them to all be within one movie clip so I can put that mc in a scroll pane. How would i go about making this code work in a mc? I tried changing _root but that didnt work.

You've been most helpful by the way, answering quite a few of my questions- so thank you very much
  • roarmeow
  • Professor
  • Professor
  • User avatar
  • Posts: 861
  • Loc: BKNY

Post 3+ Months Ago

susancbk wrote:
You've been most helpful by the way, answering quite a few of my questions- so thank you very much


yo... f'real... i think lost in beta deserves a big cake or something...
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Create an empty movieclip symbol, then add the actions inside that, but make sure to change _root to this
Code: [ Select ]
for (i=0; i<pics.length;i++) {

img = this.createEmptyMovieClip('myImg'+i, i + 6);
img._x = i * 2;
img._y = i * 2;
img._xscale = 16;
img._yscale = 16;
img.loadMovie('images/' + pics[i]);

}
  1. for (i=0; i<pics.length;i++) {
  2. img = this.createEmptyMovieClip('myImg'+i, i + 6);
  3. img._x = i * 2;
  4. img._y = i * 2;
  5. img._xscale = 16;
  6. img._yscale = 16;
  7. img.loadMovie('images/' + pics[i]);
  8. }


Or if you didn't want to physically create the new empty movieclip symbol there is the actionscript, in which case that would go something like this...
Code: [ Select ]
this.createEmptyMovieClip("container", 1000);
for (i=0; i<pics.length;i++) {

img = container.createEmptyMovieClip('myImg'+i, i + 6);
img._x = i * 2;
img._y = i * 2;
img._xscale = 16;
img._yscale = 16;
img.loadMovie('images/' + pics[i]);
}
  1. this.createEmptyMovieClip("container", 1000);
  2. for (i=0; i<pics.length;i++) {
  3. img = container.createEmptyMovieClip('myImg'+i, i + 6);
  4. img._x = i * 2;
  5. img._y = i * 2;
  6. img._xscale = 16;
  7. img._yscale = 16;
  8. img.loadMovie('images/' + pics[i]);
  9. }



@roarmeow: Mmmm... cake...lol.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.