Wa2s class 4

esse quam videri
Jump to: navigation, search

Review

Datatypes

We store data in variables, right? But there's lots of different types of data we can store. At some point in time you'll be trying to feed a variable that has a alphabetical value to a function that is expecting numeric data. The function is not going to work. To avoid problems like this in the planning phase, you should declare what type of data can be contained in your variable when you declare it.

 var myVar:Number;

The above statement says I'm declaring a new variable and it's going to hold numbers. I could set the value of the variable in the same line and it would look like this:

 var myVar:Number = 5;

Some common data types are:

  • array is a type of variable that holds several other vairables
  • date is a type of variable for holding dates. Your calendar apps will use this a lot.
  • loadVars is really an object, that has methods for loading data into it from sources that are external to flash.
  • Number is for variables that hold numbers, such as 1, or 2, or -3 or 57 or 94.3
  • String is for variables that hold strings of text, "Hello world" or "I'm sorry Dave..."

conditional statements

Conditional statements are used to logically control have some parts of your code get executed, and other parts skipped. For example, a simple password check could look like this:

 var secretPassword:String = "not4u";
 if (textBox_txt.Text == secretPassword) {
     trace("password accepted");
} else {
     trace("no dice");
 }

Two equals symbols are used in the second line of the code because one equals is how variables are assigned their value. Two equals is the operator to compare the value between variables. If I typed not4u into my the text box of my app, it would let me in. if I typed anything other than not4u then no dice.

  • == means equal to
  •  != means not equal to
  • > means grater than
  • < means less than
  • >= means greater than or equal to
  • <= take a wild guess

arrays

I already said arrays are variables that contain other variables. How does that work?

 var myAry:Array = ["val1", "val2", "val3"];

The above line is declaring a new variable that's an array. It's then assigning the values val1, val2, val3, to it. if you traced the array

 trace(myAry);

it would output val1,val2,val3. To get it to only output the second value you would type

 trace(myAry[1]);

Why is it myAry1 and not myAry2? Because the array starts counting at 0. So the first value is 0, the second is 1, the third is 2, and so on.

you could use the brackets to but a value in a specific spot of an array too, like this:

 myAry[63] = "sixtyFour";

Complex conditionals

nested if statements

If Then Else statements can be nested, like so:

 if (x >= y) {
      //do something
 } else if (y >= 42) {
      if ( x == "cheese" ) {
           //say cheese
      else{
           //don't say cheese
      }
 else{
      //x is really big, and not equal to cheese
 }

So that says, is x equal to or grater than y? If it is, do something. If it is not, is it y greater than or equal to 42? If it is, check and see if X is equal to cheese. If x is equal to cheese say cheese. If it is not equal to cheese, don't say cheese. But the whole cheese thing never would have come up if y was less than 42. In that case, it would have just said x is really big, and not equal to cheese.

switch statements

 var answer:String;
 
 answer = "1";
 
 switch (answer) {
 case "1" :
 	trace("wrong");
 	break;
 case "2" :
 	trace("wrong");
 	break;
 case "7" :
 	trace("right");
 	break;
 }

Loops

 var anArray:Array = ["val1", "val2", "val3"];
 
 for (i=0; i<3; i++) {
 	trace(anArray[i]);
 }

In class

Hangman!

build the example from http://lukamaras.com/tutorials/actionscript/flash-hangman-game-tutorial.html

Steps 57 and 58 can be replaced with the following code if the XML is too scary.

 words = ["val1", "val2", "val3"];

val1, val2, and val3 would all be replaced by words you want in your game, of course.

The variable name words comes from step 56 of the tutorial, where you declaired an array called words, like this:

var words:Array = new Array();

Homework

Read Foundation AS Chapters 5 & 6

Keep working on Project 1.

Bring in storyboards next week because I forgot to look at them this week.