Accessibility

Table of Contents

Creating iRiver U10 Games for Beginners

Understanding basic ActionScript

I'm going to create a simple baseball game that two people can enjoy. Two players have their own role in the game: one to throw the ball and the other to hit it. The first task is to think about what you need to do to create the game:

  • When a pitcher throws a ball, the ball should move. The coordinates of the ball must change.
  • The batter hits the ball upon its arrival. The result can be either a hit or miss. You need to create the batter's motion upon hitting the ball, and you have to register the appropriate point in time when the batter hits the ball.

First, I will concentrate on the two elements mentioned above to make it simple. After addressing these basic structures, I will explore the topic further in the following sections.

Before I begin, however, let me touch briefly on ActionScript syntax for Flash Lite 1.1. All you need to know are the four simple elements listed below; once you grasp the grammar, you can create any simple baseball game with ease:

  • gotoAndPlay(), gotoAndStop(), and stop()
  • tellTarget()
  • Using variables
  • if statement

gotoAndPlay(), gotoAndStop(), and stop()

The gotoAndPlay() function sends the Timeline's playhead to the frame specified in a scene and plays from that frame. In the parentheses (), you can enter the specified frame number or label where you want to move the playhead:

//Go to Frame 5 and play it. 		
   gotoAndPlay(5); 
//Go to the menu labeled frame and play it.
   gotoAndPlay("menu");
		

The frame label you want to go to can be specified in the Property inspector (see Figure 1) when an appropriate frame is selected.

Frame label

Figure 1. Frame label

The gotoAndStop() function sends the playhead to the frame specified in a scene and stops it. In the parentheses (), you can enter the specified frame number or label where you want to move the playhead:

//Go to Frame 5 and stop playing it.
   gotoAndStop(5);
//Go to the menu labeled frame and stop playing it.
   gotoAndStop("menu");

The stop() function stops the playhead at the current frame in the SWF file that is currently playing:

//Stop playing on the current frame. 
stop();

tellTarget()

In Flash Lite 1.1, the tellTarget() function is useful when you want to select and control a movie clip located on a different path than the movie clip on which you are writing the script. For the argument in parentheses (), enter the path of the movie clip that is a target of your script. Use / (slash) notation to indicate the path to the targeted movie clip:

//Select the ball_mc movie clip on the main Timeline.
tellTarget("_level0/ball_mc7q"){
   //Go to the home run frame and play it from that frame.
      gotoAndPlay("homerun");
	}

Using variables

A variable is a container that can hold any number or string. If you're working with a value that is not always fixed, save it in a variable—typically named with lowercase letters. Flash Lite 1.1 does not declare separate variables. When developing for Flash Lite, use the trace() function to test and display the value of a variable:

	 //Enter number 30 in the data variable.
data = 30;
//Type hello in the str variable. 
str = "hello";
//When you output data and str with the trace function, the value is displayed.
trace(data);
trace(str);

When you refer to variables in Flash Lite 1.1, use dot (.), colon (:), and slash (/) syntax. The following code shows two ways of indicating the data variable on the main Timeline:

   /:data
_root.data

The following code shows two ways of indicating the data variable in a movie clip instance on the main Timeline:

   /mc1/mc2/:data
_root.mc1.mc2.data

The following code shows two ways of indicating the data variable in a movie clip instance on the mc2 Timeline:

mc2/:data
mc2.data

if statement

The if statement syntax tells Flash Lite to execute a certain statement if it meets a certain condition. For example, a condition can have either true or false as a value, and the statement is executed only when the value is true:

if (condition){
   statement(s)
}

The following code example shows a statement that will be executed because the result resolves to true. The value of the data variable (10) is greater than 5:

   data = 10;
if(data >= 5){
   trace("statement has been run.");
}

In the following example, I simply entered the value true instead of creating a comparison operation:

   condition = true;
if(condition){
   trace("statement has been run.");
}