lundi 22 novembre 2010

Flash: how to get used to scritping

This blog will show simple scripting in the script window using a few variables and the trace function. It illustrates using a function and some programming techniques.

Open Flash
Open a Flash AS.3 movie.

The reason we use AS3 and not AS2 is that AS3 is a language which is like other programming languages. SO what you learn for AS3 you can also use in JAVA, ARDUINO scripting etc.

The go to WINDOW Actions, the Actions window opens.

Start typing:
trace ("Hello World of Frogs");
look at the colors of the script, blue is a system word, the text is in green.

then we make a variable:

var theNumberOfFrogs : Number = 555;

This line prepares the memory for a Number.
The way of writing is a bit strange: it is called Elephants notation, it is like this because spaces are not allowed and we need to get the name descriptive anyway.

There can be other variables made eg a MovieClip:

var thisIsAMovieClip : MovieClip = new MovieClip();

Look at the notation:
conventions are to start a variable name with a small character and a class (Number, MovieClip) with a capital.
also:
choose the name of the variable carefully so that after 20 pages of script you know what the variable is about by reading the name.
this is a bad name:
var f : Number = 0;
because you cannot see what this variable is supposed to contain.

now continue with:
trace("the number of frogs is:", theNumberOfFrogs );

compile the movie using CMD ENTER of CTRL ENTER

then the output window opens en you get a result.

continue with:
var theNumberOfFrogsEatenEachNight : Number = 123;
theNumberOfFrogs = theNumberOfFrogs - theNumberOfFrogsEatenEachNight;
trace( "the number of Frogs left is:", theNumberOfFrogs);

this means: theNumberOfFrogs becomes what is was minus theNumberOfFrogsEatenEachNight

now you see the variable in action, it can be changed!
repeat, copy paste:

theNumberOfFrogs = theNumberOfFrogs - theNumberOfFrogsEatenEachNight;
trace( "the number of Frogs left is:", theNumberOfFrogs);
theNumberOfFrogs = theNumberOfFrogs - theNumberOfFrogsEatenEachNight;
trace( "the number of Frogs left is:", theNumberOfFrogs);
theNumberOfFrogs = theNumberOfFrogs - theNumberOfFrogsEatenEachNight;
trace( "the number of Frogs left is:", theNumberOfFrogs);

you see the number of theNumberOfFrogs diminishing, it can even become negative.
What is a negative frog???
So we want to avoid that!

we type: and now I use the special code blocks because I want to use a character smaller than, used in html....

  if (theNumberOfFrogs < 0)
theNumberOfFrogs=0;
//which becomes:
if (theNumberOfFrogs < 0)  {
theNumberOfFrogs=0;
trace(" you have eaten all frogs!");
}
//if I want to be noticed that there are no frogs left.
I am using // to indicate a comment in the code, that line is not seen as code, its color is light grey.

But now I want to get rid of the repetitions.

First of all we make a machine called function which can be called each night:

 function eatingOfFrogsEachNight() {
theNumberOfFrogs=theNumberOfFrogs-theNumberOfFrogsEatenEachNight;
if (theNumberOfFrogs < 0) {
theNumberOfFrogs=0;
trace(" you have eaten all frogs!");
}
trace( "the number of Frogs left is:", theNumberOfFrogs);
}
This function can be called every time we want to use it, even if we don't know how it functions exactly! The name should be obvious like loadImage(), or loadAndPlaySound().

Then we make a loop, to avoid repeating the function call:

 for (var i: Number = 0; i < 1000; i=i+1) {
eatingOfFrogsEachNight();
}

Now the poor frogs are eaten a 1000 times!
But that is far too much, we want to stop the loop if there are no frogs left:
So we need another if statement:

 for (var i: Number = 0; i < 1000; i=i+1) {
eatingOfFrogsEachNight();
if (theNumberOfFrogs<=0) {
i=1000;
}
}

It is placed inside the loop and it will finish the loop when the number of frogs left is 0.

So know we have seen a lot of things, variables, if statements, for loops, and functions.
The whole script is like this:

 trace("Hello World of Frogs");
var theNumberOfFrogs:Number=555;
var theNumberOfFrogsEatenEachNight:Number=23;
for (var i: Number = 0; i < 1000; i=i+1) {
eatingOfFrogsEachNight();
if (theNumberOfFrogs<=0) {
i=1000;
}
}
function eatingOfFrogsEachNight() {
theNumberOfFrogs=theNumberOfFrogs-theNumberOfFrogsEatenEachNight;
if (theNumberOfFrogs<0) {
theNumberOfFrogs=0;
trace(" you have eaten all frogs!");
}
trace( "the number of Frogs left is:", theNumberOfFrogs);
}
 














The output is this:



Aucun commentaire:

Enregistrer un commentaire