Thursday, July 28, 2011

Timer


import flash.utils.Timer;

//TIMER EVENTS
//You have one handler for every listener.

//The enter frame event is based on the frame rate of the movie.
//Timer event is not connected to the rate of the movie, it is based on time.

//Start by creating an instance of our timer and store it in a variable.
var theTimer:Timer = new Timer(50);  //Timer class takes 2 parameters.
 //delay, repeat count (optional)
 //If you don't include the count the default is 0. 0= forever.
theTimer.start();

//event listeners
theTimer.addEventListener(TimerEvent.TIMER, onTimer); //onTimer is the name of the event listener

// event handlers
function onTimer(event:TimerEvent):void {  //inside the parenthesis is the variable that passes the event handler.
 //.TimerEvent is the data class (we use the same as above)
trace("timer");
//myRectangle01_mc.x +=10; //moves it every second to the right
hourHand01_mc.rotation +=360/(60*60*12);
minuteHand01_mc.rotation +=360/(60*60);
secondHand01_mc.rotation +=360/60;  // = 6 degrees

}



Review


import flash.events.KeyboardEvent;
import flash.utils.Timer;

/*
INTERMEDIATE AUTHORING
WEEK 2
*/


// CUSTOM OBJECTS  
var plane:Object = new Object(); // creates a new object

plane.x = 200;
plane.y = 300;

plane.pitch = 0;
plane.roll = 5;
plane.yaw = 5;

trace(plane.roll);


// PROPERTIES   

// POSITION
square01_mc.x = 100;
square01_mc.y = 300;

// SIZE (#1)
//square01_mc.width = 40;
//square01_mc.height = 40;

// SIZE (#2)
square01_mc.scaleX = .5; // scales using decimals between 0 and 1
square01_mc.scaleY = .5;

//square01_mc.scaleX = square01_mc.scaleY = .5; // one-line shortcut for setting
 // the same value to multiple properties   

// TRANSPARENCY
square01_mc.alpha = .75;

// ROTATION
square01_mc.rotation = 45; // sets the rotation to 45 degrees
square01_mc.rotation += 15; // adds 15 degrees to the current value
square01_mc.rotation -= 45; // rotate 45 degrees CCW


// EVENT LISTENERS

// MOUSE EVENTS

square01_mc.addEventListener(MouseEvent.CLICK, onClick);
square02_mc.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void {
//square01_mc.rotation += 15;
event.target.rotation -= 15;
}


// KEYBOARD EVENTS
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeypress);

function onKeypress(event:KeyboardEvent):void {
trace(event.keyCode);

switch(event.keyCode) {

case 38:
trace("up")
square02_mc.y -= 10;
break;
case 37:
trace("left");
square02_mc.x -= 10;
break;
case Keyboard.DOWN:
trace("down")
square02_mc.y += 10;
break;
case Keyboard.RIGHT:
trace("right");
square02_mc.x += 10;
break;

}

}


// TIMER EVENTS
var theTimer:Timer = new Timer(1000,5); // delay in milliseconds, repeat count (zero = forever)

theTimer.addEventListener(TimerEvent.TIMER,onTimer);

theTimer.start();

function onTimer(event:TimerEvent):void {
trace("Timer");
}


// ENTER FRAME EVENT
stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);

function onFrameLoop(event:Event):void {
square01_mc.rotation += 20;
}









Thursday, July 14, 2011

Review

Variables:
Containers for data.

Cannot start with a number.
Camel casing.
Cannot be protected words.
Give it a unique name.

Tell it what class of data you want it to store:
Numbers, String, Boolean, Integer, Unsigned Integer, Arrays

var myFirstVariable:Number = 250.5     //any number including decimals 
var myInteger:int = 36;   //any whole number that is positive or negative 
var myUnsignedInteger:uint = 2000;    //any positive whole number  
var myString:String = "Hello  World";    //just plain text
var myBoolean:Boolean = true;  //true or false; 1 or 0;
 
Arrays:
var myFirstArray:Array = new Array();    //creates a new empty array
myFirstArray[0] = "Ross";   
myFirstArray[1] = "Higgins";  
myFirstArray[2] = 36;  

trace(myFirstArray);   // send the values of the array to the output panel

Condensed Way to Make an Array:
var mySecondArray:Array = ["Ross", "Higgins", 36]; 

.push 
mySecondArray.push("Married");  //PUSH adds a value to the end of existing array
.pop 
mySecondArray.pop();   //POP takes the last one off



.splice
public splice(startIndex:Number, [deleteCount:Number], [value:Object]) : Array

mySecondArray.splice(1,0,"California");     //SPLICE adds a value to the array in a specified position
mySecondArray.splice(1,1);    //the position, number of items we are removing


.length
trace("length:" + mySecondArray.length); 

OUT PUT:    
length:3





HELP:
highlight one of the blue words, like Array, and right click then select View Help



Operators:
==   equality operator
>     greater than
<     less than
>=  greater than or equal to
<=  less than or equal to
!     not operator
!=   not equal to
!>  greater than
%  modulus operator (returns the remainder of the divsion of 2 numbers)
  10 % 3 = 1 //10 divide by 3 over and over until it can no longer divide and it gives you the remainder
  25 % 5 = 0  //you can use this to check if your array has even values
&&  and operator
||      operator
++   increment operator
--    decrement operator

Conditional Statements:
//IF STATEMENTS
if ( myInteger == 362) {
    trace("the first statement is true");
} else if (myFirstVariable = 300) {
    trace("the second statement is true");
} else {
    trace("everything else is false");
}

//AND OPERATOR
if (myInteger == 36 && myString == "Hello") {
    trace("both conditions are true");
}
//OR OPERATOR
if (myInteger == 36 || myString == "Hello") {
    trace("at least one of the conditions are true");
}

//SWITCH STATEMENTS
var a:Number = 35;

switch(a) {
    case 0:
        trace("#1");
        break; //if you don't have break then it will continue tracing everything below the match
       
    case "Hello World":
        trace("#2");
        break;
       
    case 35:
        trace("#3");
        break;
   
    default:
        trace("nothing else was a match");
}

Loops:
//FOR LOOPS 
//For loops are finite in their execution
//There are 3 parts in constructing Loops
:
  set up a  variable, condition, incrimentor


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

OUT PUT: 
Hello
Hello
Hello
Hello
Hello



//counting up

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

OUT PUT:
0
1
2
3
4
5
6
7
8
9



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

OUT PUT:
4
3
2
1



//WHILE LOOPS
//ends when the condition is met (can create an infinite loop)

var theNumber:Number = 0;
while (theNumber < .5) {
    theNumber = Math.random();  //u do this so you don't get infinite loop.
                                // Math.random will pick numbers above and below .5
    trace(theNumber);
}





Random Numbers:
Math.random
Math.random(); //picks a number between 0 - 1; never 0 or 1;

var theRandomNumber:Number = Math.random();
trace(theRandomNumber);

theRandomNumber = Math.random() *5;
trace(theRandomNumber);



rounding

Math.round();   //like elementary school; >= .5 rounds up; < .5 rounds down 
Math.ceil();   //always rounds up 
Math.floor();   //always rounds down
//(you have to pass a number into the parenthesis to make the above work) Math.round(33);

theRandomNumber = Math.ceil(Math.random() * 100); //pick a random number between 1-100



Functions
1.
function showMessage():void {
   
 }

//void is it's data class. Because we are not returning a value out of the function.

2. 
 function showMessage():void {
     trace("Hello");
 }
showMessage();

//call the function 

3. 
function showMessage(theMessage):void {
     trace(theMessage);
 }
showMessage("This is Stuff");
showMessage("Hello World");



//pass the information inside the var theMessage into the function

4.  Converting to Farenheit
function celciusToFarenheit(theCelciusNumber):Number {
    return(9/5) * theCelciusNumber + 32;
}

var theTemperature:Number = celciusToFarenheit(28);
trace(theTemperature);



HOMEWORK:
Read Chapter 1 & 2
 

Syllabus

MM2202 Intermediate Syllabus


Book: Learning Actionscript 3.0