|
|||
| week 03b - project 1 assignment, count selections and go game | |||
exercise : |
create a Flash movie like this (countSelections_1.fla simple, countSelections_2.fla extras)
This Flash example counts the number of orange buttons the user clicks on, and when that number reaches 4, it moves the timeline to the "Win" frame. If the user clicks on any blue button, the software immediately sends the timeline to the "Lose" frame.
|
|
| set up : | the timeline and graphics Before coding, you need to create the timeline and graphics elements so you have something to work with and a place to put the code.
|
|
| creating the script : |
step by step The first step in creating this small game is defining the basic logic of the interaction. Something like this:
|
|
| solve each problem : | and build up the script Now that we have the basic logic established, we can start solving each part one at a time.
|
|
| initialize : | the counter Usually, initializations are put in the first frame of the movie, in an Actions layer. First, the code needs to stop the main timeline so we stay on the frame where the game it. Then you need define the counter variable, and create a statement that initializes it to zero.
|
|
| increment : | the counter We want the counter to be incremented every time the user clicks on an orange button. The click is an event, so this means we need an event handler on the button to react to the user's click. The code to do this looks like this:
There are simpler ways to do this increment:
Where should this code go? Well, what object(s) do we want to increment the counter? That's where the code should go. I.e. in each orange button.
|
|
| test : | to see if the counter is greater than or equal to 4 After the counter is incremented, we want to check to see if the counter is 4 or greater. This is done with a conditional.
|
|
| go to : | the WIN frame If the counter is >= 4, we want to move the timeline to a frame with the wining text on it. The best way to do this is by labeling the winning frame. We'll label it "win". So now the conditional statement becomes:
|
|
| put the code : | together We need to put the above code fragments into one set of statements:
All of this code together would look like this:
|
|
| or go to: | the LOSE frame, if the user clicks on a blue button We need to handle what happens when the user clicks on a blue button, which is to send them directly to the LOSE frame. So on each blue button, our event handler looks like
|
|
| start over : | by returning to the start frame On the WIN and LOSE frames, we need a button that returns the user to first frame where the game starts. This looks like:
|
|
| centralize : | the code with functions Rather than putting the same code on every button, it would be better to centralize the code in one place. This is especially helpful if we want to make a change in how things work (e.g. change the number of buttons to click to win). To do this, we would use a function, and call the function from each button. Like variable initialization, a common place to put the function definitions is in the first frame of the movie. So we could create a function like this:
This takes the same code we had in each button, and collects it inside a function. Now the only code we need to put in the orange buttons is the following:
|
| all materials on this web site © copyright 2004, Philip van Allen |
top |