Saturday, September 1, 2007

Do You Need To Use Bait With A Swedish Pimple

ActionScript Basics - Loops.

Loop is a structure to implement the loop body down number of times, where the number of times depends on the parameters "transferred" loop. The
difficult? After reading this tutorial will provide a simple and logical because the loops are designed to automate your work. Use them whenever you need to manage similar properties similar objects, when you want to duplicate MovieClip'y which will serve a menu to assign the MovieClip'om onRelease events, etc, etc. Use a loop to view, content objects, strings and arrays. After some time you will see that the knowledge of your programming loop was the way by the passion, where he probably wrote the
  array = new Array    () 
array [0] = this [" klip0_mc "]
array [1] = this [" klip1_mc "]
array [2] = this [" klip2_mc "]
array [3] = this [" klip3_mc "]
array [4] = this [" klip4_mc "]
array [5] = this [" klip5_mc "]
/ / etc
array [0]. onRelease = function () {trace
( this . _name)}

array [1]. onRelease = function () {trace
( this . _name)}

array [2]. onRelease = function () {trace
( this . _name)

} / / etc, etc, etc. ..
... and you were proud to have written "tyyyle" code;)
And do not be simpler?
  var  ileKlipow = 10 
var array = new Array ();

for ( var j = 0, j <=ileKlipow; j++){ tablica[j] = this [" clip " + j + " _mc "]
array [j]. onRelease = function () {trace
( this . _name)
}}

}


Let's get to the point:)

loop
Description loop structure for .
  for  (initialization, warunek_ilości, iteration) {
/ / loop body}


The use of the loop for looks like this ...
  / / loop in action ... 
for (i = 0; and <10; style="color: rgb(0, 153, 255);"> trace (i)}


If this code is executed is a description of its operation (in a simplified) will look something like this: first
Loop is initiated on the value of i = 0, (remember that the variable name and it really is free).
second Then the condition if and <10> true (that is, if and is less than 10) the loop body is executed.
third Then the value and is incremented by 1 (with notation i + +).
4th Then everything begins again and continues until the test condition and <10> and loop is decremented (reduced), with the difference that you can use a different record structure of the loop.
  / / eg instead 
for (i = 10, and> 2, i -) {
trace (i)}

/ / you can write to shorten
for (i = 10; -; ) {
trace (i)}

As you can see, in the latter case you do not need to declare when the loop has to stop because when a variable and will be zero this value will be converted to a boolean value false the same loop will be aborted.
Please note that if a condition is not met for the first time, body of the loop is not executed even once.

Remember that if you construct a loop in which the condition never returns false , the publication of your movie will start an infinite loop, causing a compile error or even worse things.)
Example:
  / / Do not try this at home;] 
for (i = 0; and <3; color="#0099ff"> trace (i)}

look ... In every repetition of the loop condition is checked whether the variable and is less than 3, therefore, that the variable and was initiated with a value of 0 and during each cycle of its value is reduced by 1, this condition will never be satisfied, thus a compile error.

worth mentioning, before continuing the theme that, in each loop, we can use the increment or decrement is not based on the change in value by 1, but with any value.
eg
   for (i = 30, and> 3;  i-= 3 ) {
trace (i)}



while loop
Another example loops are loops while and to ... while (which a little below), basically the way they work is almost identical to the for .
   with var = value  / / equivalent initialization 
while (condition) {
/ body condition, and iteration
}
What can be translated into human language as
 until ( This condition is true) {
perform what is here
}

Generally in the while loop checks the condition that if it returns true (or can be converted to true) is executed body condition, and then loops, and so until the condition is not met.
  / / while in practice 
var c = 0
while (c <= 8){ trace (c)
c + +
}

/ / We can also shorten a little the record
var c = 0
while (c + + <= 8){ trace (c)
}
Another approach to the while loop can also be an example
   var x = 8 
while (x -) {
trace (x)}


/ / or
var x = 8
while (x) {trace
(x)
x -
}
As you can see, thanks knowledge of the initial value of the variable x we \u200b\u200bcan dekrementować directly in the loop condition while or outside of being certain that the loop is not infinite, because the reduced value of the variable will eventually reach 0, which will be converted to a boolean value false.
As for loop if the condition is not met for the first time, body of the loop is not executed even once.

Loop ... while
To prevent non-execution of the loop body we can use to ... while loop. Construction of the loop while to ... looks like
  to  
{/ / loop body}
while (condition)
which can be translated "the Polish" follows
 execute {
everything is here
} until (this condition is true)

using this type of loop, we are sure that the body loop is executed at least once.

loop ... in
loop type for ... in is used to calculate the properties (methods, etc) associated with the object, and it is so much interesting construction that does not require knowledge of where these properties really are.
construction of such a loop is as follows
  for  (property of  in  jakisObiekt) {
do it
}
Przykładzik:
   var person = new     Object () 
osoba.nazwisko = " Copernicus'
osoba.imie = "Nicholas "
osoba.wiek = " near pięćsetki "
osoba.zawod = " baker "

for (j in person) trace (" property: "+ j +" \\ nma value: "+ person [j] +" \\ n )

/ / Another example
array = new Array ( cat "" dog "," parrot "," Copernicus )
for (j in array) {trace
( index "+ j +" \\ nma value: "+ array [j] +" \\ n )
}

If this is not for you to completely clear this easy ... if a little pokombinujesz on their examples, similar to the above, you understand what's going on in the iteration object properties.

command break and continue
Two specific keywords that are associated with the use of loops are break and continue.

break
placement in the body of the loop break keyword causes immediately leave the loop and stop the further execution. For this reason, use this command has a justification in case of a specific condition.
Example of command break:
   for (i = 0; and <6; style="color: rgb(0, 153, 255);">  trace (i) 
if (i == 3) {break
;
trace ("this is not appears )
}}


As you can see the keyword break is similar in its action to the command return to functionality. You can add, moreover, that the use of return for the use of loop the function body will interrupt the performance of the loop, which in principle is clear because the immediate discontinuation of return will perform the same function .... Here is a moment to reflect on "what I just read?" and read this paragraph again;]

Example:
   check function () {
var i = 4
while (i -) {
if (i == 2) return;
trace (i)

}}
check ()


continue
Keyword continue used in the body of the loop causes an immediate exit from the body of the loop, the difference (in relation to the break) is that the loop is not stopped, but resumed the next natural cycle. Here
continue the use of command should also be dependent on the occurrence of a particular condition.
   var i = 5 
while (i -) {
if (i == 2) {continue
;
trace (" is not displayed ");
}
trace (i)}


Nesting loops.
very easy solution to save your job is to use nested loops.
Here's an example:
  for  ( var  i = 1; and <5; style="color: rgb(0, 153, 255);"> trace  (i + " CYCLE main loop. \\ N ) 
for (j = 1, j <5; style="color: rgb(0, 153, 255);"> trace ( 5 * (i-1) + j + "a series of nested )
if (j == 4) trace (" *************** \\ n ")

}}


Finally, it should be added that:
Remember that loops is not a tool to animate MovieClipów or engaging in activities that are based on time "class =" BlueLnk "> Remember that loops is not a tool to animate MovieClipów or engaging in activities that are based on time, so the thought of the loops as if they were executed "immediately." With the loop does not change the "smooth" the color of the clip, its location, size, transparency, or as loops not to serve . The manipulation of the properties MovieClipów Use setInterval and listener event handler MovieClip.onEnterFrame () which was widely and repeatedly discussed at the forum:)

So much the topic of the loop. If you have any questions regarding this tutorial, click on "comment on the forum" at the top right side:).

0 comments:

Post a Comment