Quiz and Quick Assignment Results

 

Check scores

Pretty good, everybody!

Emails Thanks!

Got everybody, so now maybe I should send something...

I Got Caught!

Ms. Batista was good enough to point out an error I made in class two's notes. 25 bonus points!

Loop d' Loop

 

Timeline Loops

Framescript:

gotoAndPlay(10);

gotoAndPlay("some label");

ActionScript Loops

"while" loops

while(condition){

doSomething;

conditionChanger;

}

 

NEW: "for" loops

for(intialCounterValue;
conditionToTestCounterFor;
nextCounterValue) {

doSomething;

}

GOTCHA: it's a semicolon, not a comma!

 

Simplified:

for(init; condition; next) {

doSomething;

}

Doesn't translate very well into natural language like "with" and "if then".

Like settings on a machine with three dials. -- The "for" machine.

 

Examples

for (counter = 0; counter< 10; counter++){

trace(counter);

}

 

for(testVal = 10;
testVal > 0;
testVal --){

trace(testVal);

}

 

for (i = 10; i > 0; i + 2){

trace(i);

}

 

myArray=["apple",
"orange",
"lemon",
"avocado"];

 

for(myCounter = 0;
myCounter < myArray.length;
myCounter++){

trace(myArray[myCounter]);

}

Search An Array

myArray=["apple",
"orange",
"lemon",
"avocado"];

 

searchString="lemon"

 

for(myCounter = 0;
myCounter < myArray.length;
myCounter++){

if(myArray[myCounter] == searchString) {

trace(searchString + " is in element " + myCounter + " of the array")

}

}

Cross Indexing Arrays

example01.fla

fruitNames = ["apple",
"orange",
"lemon",
"avocado"];

 

fruitColors = ["red",
"orange",
"yellow",
"green"];

 

counter=0

searchString="lemon"

 

// look for searchString

while(fruitNames[counter]
!= searchString
&& fruitNames[counter]
< fruitNames.length) {

counter++;

}

 

// respond with searchString's color

if (fruitNames[counter]
== searchString) {

trace(searchString + "color is: "
+ fruitColor[counter]);

}else{

trace("there is no match for "
+ searchString);

}

 

Let's Play Hangman! The Graphics

 

Build The Core Visual Elements of the Movie

Source .fla

The hanging man clip

Create a step by step/frame by frame animation of a hanging, a "lose" screen and a "win" screen

 

The interface clip

Single frame, layer for each graphic

display_txt with its Var set to "display"
input_txt with its Var set to "input"
instance of "enter_btn"

 

_root movie timeline

Single frame, layer for each graphic

Instances of:

Hangman title
Hanging Man movie clip - "hangman_mc"
Interface movie clip - "interface_mc"
Play Game button - "playGame_mc"

Let's Play Hangman! The Code

 

Three Conceptual Chunks

Source .fla

Movie setup - Initializes the arrays and variables we'll need in the movie

Initializes the list of possible mystery words

Creates two empty arrays to hold letters that are needed and letters that are guessed

Turns off the interface_mc

 

Game setup - Initializes the game input interface in the playGame_btn

Turns on the interface_mc

Turns off the playGame_btn

Picks a random word

Initializes the number of letters left to go for the right answer

Breaks the mystery word down into an array of letters

Creates another array filled with question marks to cross-index with the mystery word's array

Sets the display field to a to the same array

 

Game play - in the enter_btn in the interface_mc

Checks the input fields value to see if it's in the mystery word's array

If it is, sets the display text to include all the correct positions of the letter

If it isn't, advances the hangman_mc one step closer to death.

If you win, go to the "win" label in hangman_mc and turn on the play button

If you die, you're already in the "lose" frame.<

The Movie Setup Code - A Practical Use For Arrays That I Promised Last Time...

//Movie setup

 

// Initialize array of words

wordList = ["computer", "apple", "coffee", "flash","animal", "america", "panel", "window", "import", "number", "string", "cake", "object", "movie", "aardvark", "kettle"];

 

// Initialize an empty array to store letters from our mystery word from wordList

lettersNeeded = new Array(10);

 

//Initialize an empty array to store the letters guessed through the input_txt field

lettersGuessed = new Array(10);

 

//Stop the timelines

stop();

hangman_mc.stop();

 

//Hide the interface

interface_mc._visible = false;

 

//Test...

trace("wordList is: "
+ wordList.toString());

trace(""lettersNeeded is: "
+ lettersNeeded.toString());

trace("lettersGuessed is: "
+ lettersGuessed.toString());

The Game Setup Code in the playGame_btn event handler callback

//playGame button callback, which sets up the game

playGame_btn.onRelease = function() {

hangman_mc.gotoAndStop("play");

playgame_btn._visible = false;

interface_mc._visible = true;

 

//select a word at random from the wordlist array

randomNumber = Math.round(Math.random()
*(wordlist.length-1));

selectedWord = wordList[randomNumber];

 

//Test...

trace("selectedWord is: "
+ selectedWord);

 

//Track how many letters there are left to guess.

lettersLeftToGo = selectedWord.length;

trace("lettersLeftToGo is: "
+ lettersLeftToGo);

 

//Break the word into array of individual letters and put it into the empty lettersNeeded array.

for (counter=0; counter < selectedWord.length; counter++) {

lettersNeeded[counter] = selectedWord.charAt(counter);

}

 

//Test...

trace("lettersNeeded is: "
+ lettersNeeded.toString());

 

//Fill the lettersGuessed array with the same number of questionmarks as there are letters in selectedWord.

for(counter=0;
counter < selectedWord.length;
counter++) {

lettersGuessed[counter] = "?";

}

 

//Initialize the display of lettersGuessed in the display field of the interface_mc movieclip.

interface_mc.display = lettersGuessed.join("");

 

}

 

Damn! That's a lot of code and new stuff for one event handler callback.

Once done, command-enter the movie and choose "Show Variables" from the debug menu.

Game Play Code

Were talking to the enter_btn inside the interface_mc movieclip, so we need to set the scope:

//Build a callback for the button inside of interface_mc.

interface_mc.enter_btn.onRelease = function(){

 

//Assume the player guessed wrong

var wrong = true;

 

//Loop through all the letters in the lettersNeeded array (made from selectedWord's letters) and change wrong to false as soon as we find a match.

for(counter=0;
counter < selectedWord.length;
counter++) {

 

//Check if the input is in lettersNeeded, but also not in lettersGuessed.

if (lettersNeeded[counter]
== interface_mc.input
&& lettersGuessed[counter]
!= interface_mc.input){

//test...

trace("guessed a letter correctly");

//guess matches a letter in the word, so it's not wrong.

wrong = false;

//decrement the number of letters left to go

lettersLeftToGo--;

//test...

trace("lettersLeftToGo is: "
+ lettersLeftToGo);

//put the correct answer in the lettersGuessed array at it's corresponding position

lettersGuessed[counter]
= interface_mc.input;

//test...

trace("lettersGuessed is: "
+ lettersGuessed.toString());

}

 

//Set the display text to what's in the lettersGuessed array.

interface_mc.display
= lettersGuessed.join("");

 

//end of for loop

}

 

//reset the input field;

interface_mc.input = "";

 

//If the guess was no good, step to the next frame.

if(wrong){

 

//Test...

trace("guessed incorrectly");

 

hangman_mc.nextFrame();

if (hangman_mc._currentFrame == 10) {

// GAME OVER!

interface_mc._visible = false;

playGame_btn._visible = true;

}

}

 

//If all the letters have been guessed correctly, go to the Win label of the hangman_mc.

if(lettersLeftToGo == 0) {

//GAME WON!

hangman_mc.gotoAndStop("win");

interface_mc._visible=false;

playGame_mc._visible = true;

}

}

The Result

Summary

 

 

"while" loops

"for" loops

searching arrays

Cross-indexing arrays

Building a movie of movieclips

A practical application of arrays

The _visible movieclip property

Checking progress with trace();

Getting a random number with the Math object (don't worry about it yet)

Setting the scope of a callback

Using a movieclip to show the state of the game

Quiz Two

Target start time: 5PM

 

Secret URL

Announced in class

Exercises

 

"for" Loops

Build a "for" loop that traces the numbers 1-10 in the output window

Build a 5 element array and use a "for" loop to traces its elements in reverse order to the output window

Scoping Variables

Build a movie that has a button that will toggle the visibility of a movieclip within a movieclip.

"_visible" Property

Build a button that toggles a movieclips _visible property from true to false and back.

Hint, use clipName_mc._visible = !clipName_mc._visible.

Advanced Randomness

Play around with the math object to generate a random whole number such as this:

Math.round(Math.random()*6)

Use the docs in Flash. Explain why this give a random number between 0 and 6.