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

Thursday, January 13, 2011

Week 1 Demo 2

Make a star with a stroke
When you convert it to a symbol you have to select both the fill and the stroke.


name all the instances: star01_mc, star02_mc, star03_mc

//PROPERTIES
// all properties use the period . syntax

//reposition
star03_mc.x = 400;   //this repositions star03_mc over 400 pixels
star03_mc.y = 322;


//it moves absolutely from the upper left hand corner, it is not based on where it started on the stage.



//transparency
star02_mc.alpha = .5;  //transparency; uses decimals as percentages

//rotation
star03_mc.rotation = 180;
//you can use a number greater than 360, it just keeps going. A negative number makes it go counter clockwise.

//scale
star03_mc.scaleX=.2; //1.2 would scale it up 120%
star03_mc.scaleY=.2;  //have to scale both x and y to be proportional

//or you can combine it like this:
star02_mc.scaleX = star02_mc.scaleY =1.2; //

//not there - invisible
star01_mc.visible = false;
// it is not there, as opposed to alpha 0 where it is still there be not visible. The object is technically still on the stage but would not be click-able if it were a button for example.




Homework : 
Read Chapters 1 and 2

Week One Notes

Reviewing Flash
We are not using the time-line.
Properties Tab - click on stuff and get info like position and stroke. All of the properties in the properties palette is also accessible via action scripting.
Library Pallete - when we create shapes they go into the library.
     ie. make a square with the tool, click on Modify (at the top), select convert to symbol

     Shapes
     Button has interactivity built into it.
     Movie clip like a graphic but has its own time, independant of the main timeline.
     Registration point  (you can set it, usually we pick the top left corner or the center)


After creating the square into the library, anytime it appears on the stage it will be an instance of that original square in the library.We have to name that instance so we can call it up in the AS.


We are going to be camelCasing to nameStuff.
For Movieclips we are going to name are instances like this:   blueSquare_mc
       Text:  someWords_txt
       Buttons:   clickyThing_btn

Scripting window : F9 for the Actions Palette
In AS3 all the script is in one place, as opposed to tied to objects like AS2

    (if you get an error message when you open of the actions palette, you probably have an object selected on the stage. You have to unselect the object or you can click on the Layer1 : Frame 1

---------------

Variables: they are like a container that holds data. They can hold lots of different types of data.
var myFirstVariable = 255;

the numbers can be decimals

Data Classing = telling it what kind of data the variable contains
   var myFirstVariable:Number = 255;
  (all classes following the naming convention that they are capitalized)
  once you class you a variable it can only hold that kind of information, so if you class it as Number it can
  only hold numbers.

Comments:
// blah blah Single Line comment
/* blah blah blah Multi Line Comment */

Commonly used Data Classes
 Number
 int
 uint
String
Array


var myFirstVariable:Number = 255; //Any number including decimals
var myInteger:int = -35; // Any whole number including positive or negative
var myUnsignedInteger:uint // any non-negative whole number 
var myString:String = "hello";  //just plain text
var myArray: Array = [2,3,4,5]; // list of more than one value in a single variable

Conditionals
A structure in AS that allows flash to evaluate a condition and take a certain path. This is what separates programming language from mark-up language.

// IF STATEMENT

var a:Number = 0;
var b:String = "Ross";
var c:Boolean = false;

if ( a == 1 )   /* you use 2 equal signs because it is an operator, used for conditions to distinguish it from a single = which is like setting the value of something */

if ( a == 1 ) {
    //code goes here  
}


//   TRACE COMMAND
trace("Ross");  // traces to the output panel
trace ("Higgins");
trace (c);

//Control > Test Movie > Test to check this command in the Output Panel

----------------------New page of Code----------------------------
var a:Number = 0;
var b:String = "Ross";
var c:Boolean = false;

if (a==1) {
    //code goes here
    trace("if statement #1 was true");
}

if ( b=="george") {
    trace("Hello George");
} else {
    trace("Hello");
}
/* when you test this code the Output should say Hello, because b = "Ross". IF b ="george" it would say Hello George. */
------------------------Added more to the above code----------------------------


var a:Number = 0;
var b:String = "Ross";
var c:Boolean = false;

if (a == 1) {
    //code goes here
    trace("if statement #1 was true");
}

if (b == "george") {
    trace("Hello George");
} else {
    trace("Hello");
}

if (a==0) {
    trace ("number one is true");
} else if (b=="Ross") {
    trace("number two is true");
} else if (c==true) {
    trace("number three is true");
} else {
    trace("none of these are true");
}

//in this, if a=true then you will see "number one is true" in the output. if a and b are true you will still only get "number one is true" because with this code once it evaluates something and finds it to be true it stops there.


-------------------------------Class Break----------------------------
// OPERATORS

== equality operator
> greater than
< less than
>= greater than or equal to
<= less than or equal to

! not operator
!= not equal to

+ addition
- subtraction
* multiplication
/ division
&& and operator
|| or operator  // this looks weird on blogger, hold shift and hit \ two times


 if (a==1 && b=="Ross") {
     trace("both are true");
 }
 if (b=="Ross" || c==false) {
     trace ("at least one of these are true");
 }

 if (!c) {  //shorthand for (c==false) because c is boolean
    trace("the variable is false");
}
if (a!=1) {
    trace("the variable does not equal 1");
}
if (b!="ross") {
    trace("the varaible does not equal the string ross");
}

------------------New Code---------------
var a:Number = 0;
var b:String = "Ross";
var c:Boolean = false;

//SWITCH

/*
switch ("George") {
    case "ross":
        trace("one");
       
    case "George":
        trace("two");
       
    case "Ross":
        trace("three");
} */

/* switch - in the case that the value = "ross" it will trace "one"
      once it finds one that is true then it continues to run all the code aftre it
      so, in this case you will get  "two" and "three"
      even though "George" is not case "Ross"
      --You have to add BREAK to stop it from running*/

switch ("George") {
    case "ross":
        trace("one");
       
    case "George":
        trace("two");
        break;
       
    case "Ross":
        trace("three");
        break;
    default:
         trace ("other");
}