| Again, lets go through it bit
by by bit.
for(i=1;i<5;i++){
This is a "for loop" and makes
Flash repeat all the code underneath
4 times - with our variable "i" increasing
by 1 each time.
Let's break that line down a bit more.
At the start of the for loop we set
the starting value of our variable "i" - i=1.
It doesn't have to be "i" but
it does have to be consistent throughout
the section of code (see how many times "i" is
used?). Anyway "i" does the
job just fine.
Great, so i=1 - now what? Now we tell
Flash that we want it to continue the
for loop until a certain condition is
met - i<5.
We then tell Flash what to do each time
the code within the for loop is completed
- i++. This is the same as saying "get
the current value of i and increase i
by 1.
So, Flash gets to the for loop and says "now
then let me see, it appears that i=1
which we all know is less than 5 so I
might just go ahead and run all this
funky code contained in this here for
loop". So it runs through all the
code within the for loop and when it
gets to the end it increases i by 1 and
says "hmmm it appears that i is
still less than 5....I'm gonna run through
all this code again". Flash continues
talking to itself and running the code
in our for loop until eventually i=5
and it says "hey! i isn't less than
5 anymore...I'm done with this for loop!".
As (I hope) you can see - for loops
are very powerful and a real time saver.
Right, lets look at the rest of that
code.
_root.createEmptyMovieClip("button"+i,
100+i);
This creates a blank movieclip on our
main timeline. Depending on which run
through the for loop flash is up to,
the new movieclip will have an instance
name of button1 or button2 etc and will
be placed on a depth of 101 or 102 etc.
OK so we have a blank movieclip but
we want to draw something in it - a box
for example. The next 7 lines of code
do just that. We (I) don't really have
time to go into detail right now about
the new MX drawing API. Just be aware
that the code there draws 4 lines with
a width of 2 and a colour of black (lineStyle(2,
0x000000) and then fills it
with red (beginFill(0xFF0000).
Then the last 2 lines simply position
our new movieclips on the stage.
_root["button"+i]._x=i*60;
_root["button"+i]._y=25;
So the first time Flash goes through
the for loop, it will create a new movieclip
with an instance name of button1 and
will place it on the stage at an x position
of 60 and a y position of 25. The second
time it goes through the for loop, it
will create a new movieclip with an instance
name of button2 and will place it on
the stage at an x position of 120 and
a y position of 25.
That's some pretty serious scripting.
Don't stress if it doesn't make sense
straight away.
Test your movie. You should see 4 boxes
up the top left. They should be red with
a black stroke.
So we now have 4 movieclips on the stage
with instance names of button1, button2,
button3 and ....oh yeah...button4. Something
new in Flash MX is that we can now give
movieclips button actions such as onRelease
- we'll get to that in a sec. First,
lets make a couple of new text files
for 2 of our buttons to load in. Back
in your text editor, make a new file
and paste in this text:
myText=this is
the text that will be loaded in when
the user clicks on button1.
Save the file as text1.txt into the
task2 folder and then close it.
Make a new text file and paste in this
text -
myText=I'm some
more text. I don't really want to be
here but I have to be because the user
clicked on button2 - can I go home
now?
Save the file as text2.txt into the
same folder and then close it.
Now, back into Flash, add this action
underneath all the others:
button1.onRelease=function(){
loadVariables("text1.txt", "_root");
}
button2.onRelease=function(){
loadVariables("text2.txt", "_root");
}
As you can see, we have given 2 of our
movieclips button actions. Flash kindly
responds by changing the mouse to the
finger when the user rolls over the boxes
and allows the user to click on them.
Test your movie and click on the 2 buttons
to the left. You should see the 2 text
files load in and display themselves
in our text box (because, once again,
we have used the variable name of myText
in our external text files - the same
as the variable name of our text box).
Well that's just great but I want some
images. Get 4 photos, if you don't have
any of your own then look in the samples
folder inside the photoshop folder (I'm
sure this won't be necessary though!).
Crop and resize all your images so they
are 100 pixels wide by 150 pixels high.
Choose "save for the web" from
photoshop and save them all as jpegs
at max quality - make sure the "progressive" box
is not checked. Save
all four images to the task2 folder and
name them image1.jpg, image2.jpg etc
Cool. Something else that's new to MX
is you can load in jpgs and other stuff
at run time. Let's do that now.
Paste these actions underneath the others:
_root.createEmptyMovieClip("imageHolder1",
200);
_root.createEmptyMovieClip("imageHolder2", 201);
imageHolder1._x=450;
imageHolder1._y=50;
imageHolder2._x=450;
imageHolder2._y=225;
loadMovie("image1.jpg","imageHolder1");
loadMovie("image2.jpg","imageHolder2");
Let's look at those actions. The first
2 lines create 2 new movieclips (same
as before) and gives them instance names
of "imageHolder1" and "imageHolder2".
The next 4 lines just position our new
movieclips on the stage. In the last
2 lines you will see the loadMovie action
that you have all see before. Flash MX
allows us to load in jpgs in the same
way that you could already load in swf
files. In this case, we are loading each
image into a target mc (as opposed to
a level) and Flash aligns the top left
corner of our images with the registration
point of the "imageHolder" movieclips
that we just created.
Save your movie and then test it.
|