Thursday, January 20, 2011

Week Two Notes

Loops
Allows you to make code that is reusable and compact, so you don't have to write it out multiple times.

Two Types of Loops



// FOR LOOP
//it is customary to use the letter i as the variable in the for loop, if you have multiples the next would be j , k

// for variable - setting a condition - incrimentor

for (var i:int = 0; i < 3; i++ ) {
    trace("hello");
}

Output: 
hello
hello
hello
// < 3 (going to run it 3 times), ++ incriment operator, this will trace hello 3 times into the output panel
// you could do number instead of int but int is faster to write
// 0 is because you are setting the number from which it starts counting

for (var i:int = 0; i < 3; i++ ) {
    trace("hello");
}

for (var j:int = 0; j < 4; j++) {
    trace(j);
} 

Output: 
hello
hello
hello
0
1
2
3

for (var i:int = 0; i < 3; i++ ) {
    trace("hello");
}

for (var j:int = 0; j < 4; j++) {
    trace(j);
}

for (var k:int = 4; k > 0; k--) {
    trace(k);
} 

//counts backwards


Output: 
hello
hello
hello
0
1
2
3
4
3
2
1



// WHILE LOOP 
// for loops run a set number of times, while loops run while a condition is true (based on a condition)

var num:Number = 0;
while  (num < 0.5) {
   
}
// If you ran this it would go on and on because we have not set a condition for the while loop

var num:Number = 0; 
while  (num < 0.5) { 
    num = Math.random()
    trace(num)
}   

Output: 
// in this example as long as the number is greater than .5 it keeps going,


ARRAYS
//ARRAYS
//store multiple values in one single variable. An array is an object in flash, just like movieclips. So it is more than just a type it is an object.

//Arrays always start counting at 0

var myFirstArray:Array = new Array();
myFirstArray[0] = "poop";
trace(myFirstArray); 

Output: 
poop

//next we mix up the types of data we can put into the array

var myFirstArray:Array = new Array();
myFirstArray[0] = "poop";
myFirstArray[1] = "cat";
myFirstArray[2] = 35;

trace(myFirstArray);  

//another way to write it

var mySecondArray:Array = ["123 Main St.", "Pooptown", "USA", 94566]
trace(mySecondArray);  

//

Output: (of both)
poop,cat,35
123 Main St.,Pooptown,USA,94566
//you only get a line break for each trace command

// it is more common to get values out of an array vs in (like the above examples)

var mySecondArray:Array = ["123 Main St.", "Pooptown", "USA", 94566]
trace(mySecondArray[2]);  

//get the second position of the array
Output
USA

//PUSH 


//this is a way to add values into an existing array
//whatever you put inside the array it will add it onto the end of the array

mySecondArray.push("Earth");
trace(mySecondArary); 

//POP

mySecondArray.pop();
trace(mySecondArray);
//it pops of the last value of the array

Output (of both) 
123 Main St.,Pooptown,USA,94566,Earth
123 Main St.,Pooptown,USA,94566

//indexOf
//method or function (when a function is part of an object it is called a method)

var mySecondArray:Array = ["123 Main St.", "Pooptown", "USA", 94566] 
trace(mySecondArray); 


trace(myFirstArray.indexOf("cat"));
//indexOf will return a 1 if it finds the information
//if we search for something and it does not find it, it will return -1 forexample ("Cat")

Output
 poop,cat,35
1

 //FOR LOOP with an ARRAY

var mySecondArray:Array = ["123 Main St.", "Pooptown", "USA", 94566]

for (var m:int = 0; m<mySecondArray.length; m++) {
     trace(mySecondArray[m]);
     
     } 
//you could have m<4, but in case you don't remember how many values you can put the arrayname.length
//[m] you could put a number in there and it would trace out that position 4 times.

Output
123 Main St.
Pooptown
USA
94566

-----------------------------------------------------------------------------------
                                                         BREAK TIME
-------------------------------------------------------------------------------------

Three types of programming:
Sequential, Procedural, and Object Orient Programming.
Up to this point we have been using sequential coding.

Custom Functions

//FUNCTIONS
//you define the function, then you call it

function showMessage() {
    trace("hello")
}
//this will not output anything, because we have to call the function

function showMessage() {
    trace("hello dogface")


showMessage();
showMessage()
//it will output how ever many times you call it

Output 
hello dogface
hello dogface


//now we are going to write it in a more dynamic way, we are going to tell it (read more down below)

function showMessage(theMessage:String):void { 
    trace(theMessage); 
} 

showMessage("hello");
showMessage("Buttface");

Output
hello
Buttface


//passing a parameter from the call into a variable that will catch that parameter
//the parameter "hello" gets stored in the variable theMessage
//it only exists in that function, it is called a local variable
//with in that function we can use that variable anytime we want

//now we can also have it run and return a value back to us for later use
function celciusToFarenheit(theCelciusNumber:Number):Number {
    return (9/5)*theCelciusNumber +32;
   
}
var theFarenheitNumber:Number = celciusToFarenheit(28);
trace(theFarenheitNumber);

//creating variable theCelciusNumber that is a Number
//:Number):Number since we are passing something as opposed to passing nothing like  :String):void
//since it is returning a value to use we need to create something to store that value into
//create a new variable theFarenheitNumber to store the value




----------------------------------------------------------------------------------
-----------------------NEW FILE-----------------------------------------------
---------------------------------------------------------------------------------
//MOUSE EVENTS
make a pentagon on the stage
make it a movieclip
give it an instance name: pentagon01_mc

//in the AS we need to create an even listener
//when the event happens/is triggered then the event handler occurs

pentagon01_mc.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void {
    trace("click");
   
}

//the eventlistener sends an  event to the handler, we named it event but we could have named it whatever we wanted such as (evt:mouseEvent)

OUTPUT 
run the script
when we click on pentagon01_mc the output panel shows click



import flash.events.MouseEvent;

pentagon01_mc.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void {
pentagon01_mc.rotation += 10;

}  


OUTPUT  
when you click on the pentagon, it rotates on the registration point


//if you need to change the registration point, you go to the library, select it, cut and paste and the point will move to the middle.
//if you need to move it back the corner you can copy and paste it and use the guides to eyeball the position, or you can set the x and y values in the properties.


//when you create a new listener you have to create a new handler to handle it
//this changes the alpha down to 80 percent 


import flash.events.MouseEvent;

pentagon01_mc.addEventListener(MouseEvent.CLICK, onClick); 
pentagon01_mc.addEventListener(MouseEvent.ROLL_OVER, onRoll); 

function onClick(event:MouseEvent):void {
    pentagon01_mc.rotation += 10;
   
}

function onRoll(event:MouseEvent):void {
    pentagon01_mc.alpha = .8;
}


//next create a third event to make the transparency come back


import flash.events.MouseEvent;

pentagon01_mc.addEventListener(MouseEvent.CLICK, onClick);
pentagon01_mc.addEventListener(MouseEvent.ROLL_OVER, onRoll);
pentagon01_mc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut); 

function onClick(event:MouseEvent):void {
    pentagon01_mc.rotation += 10;
   
} 

function onRoll(event:MouseEvent):void {
    pentagon01_mc.alpha = .8;
} 

function onMouseOut(event:MouseEvent):void {
    pentagon01_mc.alpha = 1;
}





//KEYBOARD BOARD EVENT

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
function onKeyPress(event:KeyboardEvent):void {
    trace("key pressed: " + event.keyCode);

//you can have a key up or a key down, they are basically the same except you might want key up if you are making a game and you have the user hold a key and do stuff

OUTPUT (depends on what keys you pressed)
key pressed: 83
key pressed: 68
key pressed: 70
key pressed: 65
key pressed: 83


stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
function onKeyPress(event:KeyboardEvent):void {
    trace("key pressed: " + event.keyCode);
    if  (event.keyCode == 40) {
        trace("down");
    }
}

Output
key pressed: 40
down

// next add else if for the other arrow keys and else for everything else

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
function onKeyPress(event:KeyboardEvent):void {
    //trace("key pressed: " + event.keyCode);
    if  (event.keyCode == 40) {
        trace("down");
    } else if (event.keyCode == Keyboard.UP) {
        trace("up");
    } else if (event.keyCode == Keyboard.LEFT) {
        trace("left");
    } else if (event.keyCode == Keyboard.RIGHT) {
        trace("right");
    } else {
        trace("you suck");
    }
}

OUTPUT
up
right
left
down
you suck

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
function onKeyPress(event:KeyboardEvent):void {
    //trace("key pressed: " + event.keyCode);
    if  (event.keyCode == 40) {
        //trace("down");
        pentagon01_mc.y +=20;
    } else if (event.keyCode == Keyboard.UP) {
        //trace("up");
        pentagon01_mc.y -=20;
    } else if (event.keyCode == Keyboard.LEFT) {
        //trace("left");
        pentagon01_mc.x -=20;
    } else if (event.keyCode == Keyboard.RIGHT) {
        //trace("right");
        pentagon01_mc.x +=20;
    } else {
        trace("you suck");
    }
}


OUTPUT
You can use the arrow keys to move the shape around



HOMEWORK : Read Chapter 3

No comments:

Post a Comment