PDA

View Full Version : how to write 'for' loop for this?



lunatic
January 9th, 2004, 04:55 PM
And in the meantime here's something else . . .

I have an image that I have sliced into squares of equal size (200 x 200 pixels) using the slice tool in Fireworks. I would like to load each square ("tile") into it's own movie clip. The MCs are to be dynamically created on the fly. Basically I've got 3 rows and four columns. This is the code I have so far but I'm thinking there must be a way to write a for loop for this:


// Create tile MCs
this.createEmptyMovieClip("r1c1", 1);
r1c1._x = 0;
r1c1._y = 0;
r1c1.loadMovie("test_slices/r1_c1.jpg");
this.createEmptyMovieClip("r1c2", 2);
r1c2._x = 200;
r1c2._y = 0
r1c2.loadMovie("test_slices/r1_c2.jpg");
this.createEmptyMovieClip("r1c3", 3);
r1c3._x = 400;
r1c3._y = 0
r1c3.loadMovie("test_slices/r1_c3.jpg");
this.createEmptyMovieClip("r1c4", 4);
r1c4._x = 600;
r1c4._y = 0
r1c4.loadMovie("test_slices/r1_c4.jpg");
this.createEmptyMovieClip("r2c1", 5);
r2c1.loadMovie("test_slices/r2_c1.jpg");
r2c1._x = 0;
r2c1._y = 200;
this.createEmptyMovieClip("r2c2", 6);
r2c2._x = 200;
r2c2._y = 200;
r2c2.loadMovie("test_slices/r2_c2.jpg");
this.createEmptyMovieClip("r2c3", 7);
r2c3._x = 400;
r2c3._y = 200
r2c3.loadMovie("test_slices/r2_c3.jpg");
this.createEmptyMovieClip("r2c4", 8);
r2c4._x = 600;
r2c4._y = 200
r2c4.loadMovie("test_slices/r2_c4.jpg");
this.createEmptyMovieClip("r3c1", 9);
r3c1.loadMovie("test_slices/r3_c1.jpg");
r3c1._x = 0;
r3c1._y = 400;
this.createEmptyMovieClip("r3c2", 10);
r3c2._x = 200;
r3c2._y = 400;
r3c2.loadMovie("test_slices/r3_c2.jpg");
this.createEmptyMovieClip("r3c3", 11);
r3c3._x = 400;
r3c3._y = 400
r3c3.loadMovie("test_slices/r3_c3.jpg");
this.createEmptyMovieClip("r3c4", 12);
r3c4._x = 600;
r3c4._y = 400
r3c4.loadMovie("test_slices/r3_c4.jpg");


I tried:

x=1
for (i = 1; i<4; i++) {
for (j = 1; j<5; j++) {
this.createEmptyMovieClip("r"+i+"c"+j, x)
x++;
}
}


but I have NO idead how to position the MCs or load them once they are created.

Any coding experts wanna take a stab at this one? :puzzled:

:bu:

mojoNYC
January 9th, 2004, 06:05 PM
i just did a project that i created a loadGrid() function that should do what you want it to do:



loadAlienGrid=function(){
this.colNum=9; this.row=0;
for(i=0; i<_global.alienNum; i++){
myName="alien"+i;
attachMovie("alien", myName, i); //load and dynamically name alien MCs
this.myName=myName;
myName=eval(myName);
this.myW=myName._width+10;
this.myH=myName._height;
with (myName){
if (i!=0 and i%this.colNum==0){ this.row++;}
myV=i%this.colNum; //
myName._x=(this.myW*(i%this.colNum))+20;
myName._y=(this.myH*this.row) }}}

this code gets the width of the MC to set the placement, and then 'returns' to the 0x position using a mod function based on the colNum variable. you can play around with this and get it to work--feel free to play with it and ask questions if needed...

:)mojo

lunatic
January 9th, 2004, 06:13 PM
I didn't even think about grid functions! Thanks, I'll play around with this and see what I can come up with.

norie
January 9th, 2004, 06:26 PM
x = 1;
perRow = 4;
currentRow = 0;
currentCol = 0;
for (i = 1; i < 4; i++) {
for (j = 1; j < 5; j++) {
if (!((x - 1) % perRow)) {
currentRow++;
currentCol = 0;
}
currentCol++;
this.createEmptyMovieClip("r" + i + "c" + j, x);
this["r" + i + "c" + j]._x = currentCol * 200 - 200;
this["r" + i + "c" + j]._y = currentRow * 200 - 200;
x++;
}
}

lunatic
January 9th, 2004, 06:31 PM
Ah Norie, my hero, to the rescue! :love:



but




it didn't work . . .


maybe I should try to Yuneek up on it . . . ;)

norie
January 9th, 2004, 06:33 PM
lmao, what didn't work about it?, it worked for me. gave me all the right values for x and y. Trace them and see what you get, i'll check it again.

lunatic
January 9th, 2004, 06:39 PM
oops, yeah, I just did the trace too and it IS working - my bad.

Any suggestions on how to load the mcs? All the jpgs are named similarly - "r1_c1.jpg", "r1_c2.jpg" etc.

Thanks again!

norie
January 9th, 2004, 06:44 PM
x = 1;
perRow = 4;
currentRow = 0;
currentCol = 0;
for (i = 1; i < 4; i++) {
for (j = 1; j < 5; j++) {
if (!((x - 1) % perRow)) {
currentRow++;
currentCol = 0;
}
currentCol++;
this.createEmptyMovieClip("r" + i + "c" + j, x);
this["r" + i + "c" + j].loadMovie("test_slices/r" + i + "_c" + j+".jpg");
this["r" + i + "c" + j]._x = currentCol * 200 - 200;
this["r" + i + "c" + j]._y = currentRow * 200 - 200;
//trace("r" + i + "c" + j+"\nx: "+this["r" + i + "c" + j]._x + "\ny: " + this["r" + i + "c" + j]._y);
x++;
}
}

lunatic
January 9th, 2004, 06:48 PM
How do you DO that! :stunned:

My most humble gratitude . . . once again . . . and probably not for the last time! :P

Would you mind throwing a few comments in? I get most of it except the use of 'x' and the part with the remainder operator.

Thanks!

sergwizard
January 9th, 2004, 07:03 PM
for (i = 0; i<3; i++) {
for (j = 0; j<4; j++) {
x++;
this.createEmptyMovieClip("r"+i+"c"+j, x)
_root["r"+i+"c"+j]._x = 200*i;
_root["r"+i+"c"+j]._y = 200*j;
_root["r"+i+"c"+j].loadMovie("test_slices/r"+(i+1)+"_c"+(j+1)+".jpg");
}
}


Ups, it seems I am late. :)

norie
January 9th, 2004, 07:05 PM
modulo operator (%)-
returns the divising remaider.

1%2 //returns 1
4%2 //returns 0
so every time the remainder returns 0 (false) use ! not make it true and increment the col Number and reset row number.


(!((x - 1) % perRow))

so everytime the row number is 4, it resets....
1, 2 , 3, 4
1, 2, 3, 4
and everytime the number 4 is reset.. the col num is incremented..
col 1
1, 2 , 3 , 4
col 2
1, 2, 3, 4
.......

and the rest is just strategic string placement and object array referencing :)

lunatic
January 9th, 2004, 07:14 PM
A truly magnificent piece of code - it looks so complicated until you explain it!

Hey Sergwizard thanks for your input! Never hurts to have more help!

mucho aloha to all ya'll :beam: