Unit 18. ActionScript Examples (II)
Although the object idea usually does not make us to think about a sound, in Flash MX 2004 the sounds are also objects and we can handle them using ActionScript properly. Let's see some very common codes and one complete example of how to use them:
/* Code 1 */
= new Sound();
The first line specifies to Flash that we plan to create a new object of Sound type and that it will be called "music".
The second line loads a sound from our hard disk called "sound.mp3" and loads it inside of the music object. The second parameter "false" indicates that it is an "event" sound, and, therefore, it will be completely loaded before beginning to playback.
/* Code 2 */
This code stops the "music" sound at once.
/* Code 3 */
The first line as we have already seen stops "the music" sound.
The second line causes that "the music" sound begins to playback (start) from its initial position (the 0 indicates the time from the start in seconds) and it repeats 99 times (this is called loop or circle)
/* Code 4 */
This code stops "the music" sound and reinitiates it later, reproducing it only one time.
Now as we already learn to control the sounds by ActionScript, see an example that join all seen previously.
In this example:
- We have only one frame with 3 buttons. In this frame we've inserted the Code 1.
- The 3 buttons have different functionalities:
- In the red button, it is inserted the Code 2
- In the blue button, it is inserted the Code 3
- In the green button, it is inserted the Code 4
As we already know, the nonvisible objects also are controlled by ActionScript. We plan to see some examples of the Math object operation and how to take advantage of it.
x = Math.random();
The "random" Math object Method generates a random number between 0 and 1. In this case, we store the result in variable x to be able to use it later...
There are many applications of this methods to generate secret keys, passwords, lottery numbers etc...
x = Math.round(4,3);
The "round" Method ROUNDS OFF the introduced parameter eliminating its decimal part.
In this example x takes a value 4.
x = Math.max(5 , 2);
The Method "max" takes the maximum value between 2 numbers.
In this example x takes a value 5.
The Math object is very useful and saves much work, because there is many operations that respond to some of their methods and we do not have to implement them. It is enough to look for it in the manual and use them.
We plan to analyze a loader or preloader code to finish strengthening our ActionScript knowledge:
The loaders or preloaders are only necessary when the movies acquire a considerable size and it is impossible to display the movie without having it totally loaded (because it blocks itself, incomplete parts appear etc...). We suppose then, that we’ve a movie of 150 frames. We will reserve the 3 first to create our loader. The movie begins from the Frame 4 ...
NOTE: Next to each line we will insert comments (text between the symbols /* and */) that are lines that Flash recognizes as such and that it ignores at the time of code executing (as though if they did not exist). They are used to clarify and explicate the code. We will change their color so that they stand out still more. Evidently they are dispensable in the code that we will insert finally in our movie.
Frame 1 */
Frame 2 */
If we've not yet
loaded all the movie */
Frame 3 */
Frame 1: The total number of bytes taken by the movie is calculated in the Frame 1. Then we go to the Frame 2.
Frame 2: Every time that we accede to the Frame 2, our ActionScript code finds out the bytes that we have loaded in memory and compares them with the total number of bytes (that were in the Frame 1 and they do not appear because they do not vary). lf all the movie is already loaded we go to frame 4 and begin to play the movie, if not go to the frame 3
Frame 3: The Frame 3 will repeat to send the reading head to the frame 2. Making this cycle, we give time for the computer to load the movie bit by bit, until the moment when it is completely loaded and we go to the Frame 4. The calculation of the percentage is a "garment" that we afford ourselves, because we easily find out how much movie is loaded and show it on the screen in an elegant way (i.e. in percentage) making the delay to be less boring for the user.
the movie begins... (It will never come back to any of the previous frames).
We show the result at the right. The movie will be started to load when pressing the button. The inserted code is EXACTLY one of the codes shown above, it is NOTHING MORE. So only some texts and images have been added to increase the size of the movie, otherwise the load would be too fast and it would not be visible.
If the loader isn’t visible, it is the most probable that it is already loaded in the cash memory of your computer or that you are seeing this tutorial from CD-Rom or from your own Hard disk, where the speed of downloading is so fast that a movie had to be of some Mbytes to need a loader.
Prove the code in a movie that you place on a Web server and you could see the results without problems.
You can test your knowledge by doing the:
Legal warning: Authorised on-line use only. It is not allowed the use of these courses in companies or private teaching centres.