Thursday, February 24, 2011

Passing and Receiving Information

//creating a function that create random functionality that we can call over and over again

import flash.events.MouseEvent;

update_btn.addEventListener(MouseEvent.CLICK, updateEverything);
random_btn.addEventListener(MouseEvent.CLICK, updateRandomly);




function updateMessage(theMessage):void {
    //myTextBox.text = "Renee";  //for when the function used to say updateMessage():void
    myTextBox.text = theMessage;
}

function updateEverything(event:MouseEvent):void {
    updateMessage(input_txt.text);
}

//now creating the random function
function updateRandomly(event:MouseEvent):void {
    var randomNumber:Number = pickRandom(100);
    updateMessage(randomNumber);
   
}

function pickRandom(theRandomRange):Number {
    return Math.ceil(Math.random() * theRandomRange);
}

Homeworkios

Add multiple names to the login.
Log out button.
Delete contacts.

:void vs. Returning and Receiving Information

var globalVariable:String ="Good Morning";    
var theReturnValue:String;  


function myFunction():void { 
    globalVariable  = " Good Night "; 


myFunction();   
trace(globalVariable);   


//the change of the value changes inside that  function
//and the change is global, there is no need to return that value so the function is :void

function myFunctionThatReturnsSomething():String { 
    return (globalVariable + " Cat head");    
} 
trace(globalVariable); 

trace(myFunctionThatReturnsSomething());  



//the receiving function
//the value that you are passing is called an argument
function myOtherReturnThing(theVar):String { 
    return (globalVariable + "Round2" + theVar); 
}  

theReturnValue = myOtherReturnThing("You are Welcome ");  

trace(theReturnValue); 

///////

OUTPUT:     
 Good Night
 Good Night
 Good Night  Cat head
 Good Night Round2You are Welcome

Address Book - Code

THE ADDRESS BOOK
fyi - you can make the password field have *** instead of showing the typed letters by:
1. select that textbox
2. under properties, paragraph, behaviors - select password


The code:

import flash.events.MouseEvent;




var loginName:Array = ["Renee", "Donald", "Ross"];
var myPassword:Array = ["booyah", "master", "Higgins"];


var theFirstNames:Array = ["Ross", "Joe", "Renee", "Donald", "Brian", "Annelie"];
var theLastNames:Array = ["Higgins", "La Ponza", "Rose-Perry", "Quitangon", "Lee", "Zambrana"];
var theEmailAddress: Array = ["ai@aiiemail", "asdfas", "reneeroseperry@yahoo.com", "awesome@blah", "Zemail@email.com", "me@me.com"];
var theBirthdate: Array = [ "Mystery", "April 21", "November 16", "May 1", "February 20", "January 12" ];



/* var theSearchResult:Number = theFirstNames.indexOf("Joe");
trace(theLastNames[theSearchResult]);
//index of - it searches the array and returnes the index number of the searched position */

//first_txt.text = "";
search_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void {

   var theSearchText:String = first_txt.text;  //storing the typed text into a variable when the button is clicked
 


//use that first name var to find the index number

 var theSearchResult:Number = theFirstNames.indexOf(theSearchText) //state the array you want to search (theFirstNames)
//then use that index number to match it to the last name index number
// at this point if you trace(theSearchResult) it gives you the index number of the theSearchText


 var theLastName:String =   theLastNames[theSearchResult]
 var theEmail:String = theEmailAddress[theSearchResult]
 var theDob:String = theBirthdate[theSearchResult]
              

//display that index number (lastname)
  last_txt.text =  theLastName
  email_txt.text = theEmail
  dob_txt.text = theDob
  
}

add_btn.addEventListener (MouseEvent.CLICK, addName);
   function addName (event:MouseEvent):void {
       //trace("poop");
       var addName:String = addName_txt.text;
       //trace(addName);
       theFirstNames.push(addName);
       //trace(theFirstNames);
      
       var addLast:String = addLast_txt.text;
       theLastNames.push(addLast);
       trace(theFirstNames);
       trace(theLastNames);
     
      var addEmail:String = addEmail_txt.text;
      theEmailAddress.push(addEmail);
      trace(theEmailAddress);
     
      var addDob:String = addDob_txt.text;
      theBirthdate.push(addDob);
   }
  
login_btn.addEventListener (MouseEvent.CLICK, submitIT);
   function submitIT(event:MouseEvent):void {
      
      // search the array for the user name, get that position
      var enteredName:String = login_txt.text;
      var searchedLoginName:Number = loginName.indexOf(enteredName)
      var correctPass:String = myPassword[searchedLoginName]
     
   
     
      // if not found
     
      //else is found
      //store the index number in variable
      //use that index number to look up the password in the pwarray
      //stored to var correctPass
     
      
       if ( passwordField_txt.text ==correctPass) { 
          //trace("yay");
          greyBox.visible = false;
          var welcomeName:String = login_txt.text;
          login_txt.text = "";
          passwordField_txt.text = "";
          response_txt.text = "Welcome " + welcomeName + ", I deserve an A!";
       
          
       }
      else {
            //trace("hell no")
           greyBox.visible =true;
           login_txt.text = "";
           passwordField_txt.text = "";
           response_txt.text = "You Suck, try again.";
      }
   }




 

Thursday, February 17, 2011

Class Notes - Creating an Address Book

ARRAY:

Special kind of variable that can hold more than one value at a time, a list of information.

2 ways to create and array.
var mySecondArray:Array = ["123 Main St.", "Pooptown", "USA", 94566]

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


/* The top one is in "order", so "Pooptown" is second. In the second one that is listed out with numbers you could state [5] ="Pooptown" but  have it in any order in the code and it will be fifth.

Arrays can hold 
Strings
Boolean
Number
Objects/Variables -a reference to another object on the stage such a movie clip */


example:
var myArary:Array = new Array();

myArray[0] = "Renee"; //string
myArray[1] = 35;    //number
myArray[2] = true;   //boolean
trace(myArray);

var mySecondArray:Array = ["Ross", 35, true]; 
trace(mySecondArray);




Push and Pop
Getting information in and out. 

We are going to push information into the array. 
It always pushes the information onto the end because the other positions are fixed.


var theFirstNames:Array = ["Ross", "Joe", "Renee", "Donald", "Brian"];
trace(theFirstNames[2]); //retrieve the value from a specific index position  



The Pop always removes the value from the end.

var theFirstNames:Array = ["Ross", "Joe", "Renee", "Donald", "Brian"];   
trace(theFirstNames[2]); //retrieve the value from a specific index position 

theFirstNames.push("Isaiah");   
trace(theFirstNames)  
 

theFirstNames.pop();   
trace(theFirstNames) 


OUTPUT:  
Renee
Ross,Joe,Renee,Donald,Brian,Isaiah
Ross,Joe,Renee,Donald,Brian


List of Methods of an Array.

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

trace(theFirstNames) 

theFirstNames.pop();  
trace(theFirstNames)  

theFirstNames.splice(3, 1);  //index to start at, the number of items to delete
trace(theFirstNames);  

//(3, 1) the position you want to remove, the number of things you are taking away

theFirstNames.splice(0,0, "Donald");   //start index, # to delete (none), the value to add to the array
trace(theFirstNames);  


trace(theFirstNames.indexOf("Joe"));  
//index of - it searches the array and returnes the index number of the searched position


OUTPUT:  
Renee
Ross,Joe,Renee,Donald,Brian,Isaiah
Ross,Joe,Renee,Donald,Brian
Ross,Joe,Renee,Brian
Donald,Ross,Joe,Renee,Brian
2




//If you want to find the last name of someone in the array:
//You find the index of the first name and make a variable to contain that number, then you trace that number "theSearchResult" of the LastNames.

var theFirstNames:Array = ["Ross", "Joe", "Renee", "Donald", "Brian", "Annelie"];  
var theLastNames:Array = ["Higgins", "La Ponza", "Rose-Perry", "Quitangon", "Lee", "Zambrana"]; 

var theSearchResult:Number = theFirstNames.indexOf("Joe"); 
trace(theLastNames[theSearchResult]); 




---------
The Address Book:
You have to make Several text boxes.
The one for first names has to be Input text.
The others are dynamic texts.
They are all classic text boxes.
Use static text to create the labels for the boxes.

Add a button from the components window. All you have to do is drag the button onto the stage and then name the instance of that button.

Also, you will have to  embed the fonts. You only have to do it once and it does it for all the textboxes.



var theFirstNames:Array = ["Ross", "Joe", "Renee", "Donald", "Brian", "Annelie"]; 
var theLastNames:Array = ["Higgins", "La Ponza", "Rose-Perry", "Quitangon", "Lee", "Zambrana"]; 
var theEmailAddress: Array = ["ai@aiiemail", "asdfa@ys", "reneeroseperry@yahoo.com", "awesome@blah", "Zemail@email.com"]; 
var theBirthdate: Array = [ "Mystery", "April 21", "November 16", "May 1", "February 20", "January 12" ]; 

/* var theSearchResult:Number = theFirstNames.indexOf("Joe");
trace(theLastNames[theSearchResult]);
//index of - it searches the array and returnes the index number of the searched position */

//first_txt.text = ""; (if you have something in the parenthesis then a name will already be in that field, you could have one of the names already in there to make testing easier, but it is not necessary for the final product
search_btn.addEventListener(MouseEvent.CLICK, onClick); 

function onClick(event:MouseEvent):void { 

var theSearchText:String = first_txt.text;    //storing the typed text into a variable when the button is clicked
 
//that variable that you just created is currently a String, it has to be a number to correlate to the array position

//use that first name var to find the index number

 var theSearchResult:Number = theFirstNames.indexOf(theSearchText)  
//state the array you want to search (theFirstNames)
//then use that index number to match it to the last name index number
// at this point if you trace(theSearchResult) it gives you the index number of the theSearchText

 var theLastName:String =   theLastNames[theSearchResult]     
 var theEmail:String = theEmailAddress[theSearchResult]   
 var theDob:String = theBirthdate[theSearchResult]    
              

//display that index number (lastname)
  last_txt.text =  theLastName 
  email_txt.text = theEmail 
  dob_txt.text = theDob 
  
 
   






---------
Homework:
Be able to add new entries.
Password protect the address book:
create a mc box that has your user name and password, it searches to see if it has that password if yes then you tween the search fields to become visible.
the search fields are there but they are only visible if the person enters in a user name and password that comes up true in the array.

Have the ability to add new contacts. The new contacts will be wiped out when you quit. Because we are not using PHP.

Thursday, February 3, 2011

Class Notes

We can do some fairly simple tweens with AS, this is used for creating animations that are independent of the timeline.



//the tween object takes 6 arguments

make a symbol and name it circle_mc
In this demo the result should be that the circle moves to the right and stops.

import fl.transitions.Tween; //the tween isn't normally imported, so CS% does that for us. If you are not using CS5 you will have to type that in.

//to get all the imports you can highlight the word tween in the import and select view help, then copy and paste the code
import fl.transitions.easing.*;
//the * imports all of the arguments within the easing class



var tween1:Tween  = new Tween (circle_mc,
        //the first one is the object, what we want the tween to act upon


var tween1:Tween  = new Tween (circle_mc, "x"
        //all the properties that we pass, need to be a string

var tween1:Tween  = new Tween (circle_mc, "x", strong.easeOut
        //the next argument is the function

var tween1:Tween  = new Tween (circle_mc, "x", strong.easeOut, 50
        //the starting value (as in on the stage where the x and y will be, for this demo we put 50)
       //it doesn't matter where it is on the stage, it matters what you input here

var tween1:Tween  = new Tween (circle_mc, "x", strong.easeOut, 50, 450
        //the ending position, it is going to move to 450, so it is moving over 400 since we are starting at 50



var tween1:Tween  = new Tween (circle_mc, "x", strong.easeOut, 50, 450, 1, true);
        //that last 2 arguments are tied together.
        //next is a number - the duration of the tween
        //the next is boolean - whether or not to use seconds, if you put false you are using frames
        //1, true = 1 sec
        //2, false = 2 frames

-----------------------------
//Final code with no notes:
import fl.transitions.Tween;
import fl.transitions.easing.*;
var tween1:Tween  = new Tween (circle_mc, "x", Strong.easeOut, 50, 450, 1, true);
---------------------------------------------------------------------------------------

Other types of movements

var tween1:Tween  = new Tween (circle_mc, "x", Back.easeIn, 50, 450, 1, true);
var tween1:Tween  = new Tween (circle_mc, "y", Bounce.easeInOut, 50, 250, 1, true);
var tween1:Tween  = new Tween (circle_mc, "x", Elastic.easeOut, 50, 250, 3, true);

________________________________________________________________________
//RANDOM NUMBER
//often used for chance in games


var randomNumber:Number;
randomNumber = Math.random();  //picks a number 0 and 1
trace(randomNumber); 





var randomNumber:Number;
randomNumber = Math.random() * 35;     //by multiplying by 35 picks a number 0 and 35
trace(randomNumber); 
//it will never pick 0 or 35, it will be a fraction.


var randomNumber:Number;
randomNumber = Math.round(Math.random() * 35);
trace(randomNumber);
//.5 and up, rounds up. .49 and down rounds down. Like in elementary school.

var randomNumber:Number;
randomNumber = Math.floor(Math.random() * 35);
trace(randomNumber);
//rounds down always, never gives 35

var randomNumber:Number;
randomNumber = Math.ceil(Math.random() * 35);
trace(randomNumber);
//rounds up always, never give 0
--------------

//DICE
var randomNumber:Number;

function getRandom():Number {
    Math.ceil(Math.random() * 6);
}   





//now we have to call the function
var randomNumber:Number;

randomNumber = getRandom();
trace(randomNumber);

function getRandom():Number {
   return  Math.ceil(Math.random() * 6);
} 


//we need the function to return the value back, you have to add return. this is different from you have functions that just run, like a shape that spins. 



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

//DICE
make a dice 
make it a symbol. select the symbol from the library
in the timeline hit F6, six times. On each of those frames create each side of the dice.
   (hold the ctrl button to drag and place the dot)
(make sure that the symbol is still selected) and create a new layer
 in that layer AS put stop();





 back on the stage, you will have an instance of that symbol.
name it die01_mc


add the highlighted code to the AS on frame 1 of the stage
//it moves to the frame randomly


var randomNumber:Number;
randomNumber = getRandom();
trace(randomNumber); 

die01_mc.gotoAndStop(randomNumber);
function getRandom():Number {
    return Math.ceil(Math.random() * 6);
}  


-------------------
To have 4 dice each pulling a random number
you have to put it on the stage 4 times each with it's own instance name.

AS:


var randomNumber:Number;
var randomNumber2:Number;
var randomNumber3:Number;
var randomNumber4:Number;

randomNumber = getRandom();
randomNumber2 = getRandom();
randomNumber3 = getRandom();
randomNumber4 = getRandom();

trace(randomNumber, randomNumber2, randomNumber3, randomNumber4);


die01_mc.gotoAndStop(randomNumber);
die02_mc.gotoAndStop(randomNumber2);
die03_mc.gotoAndStop(randomNumber3);
die04_mc.gotoAndStop(randomNumber4);

function getRandom():Number {
    return Math.ceil(Math.random() * 6);
}


--------------------
//ADDING A BUTTON
make a symbol and add it to the stage, instance name it poop_btn




AS:
var randomNumber:Number;
var randomNumber2:Number;
var randomNumber3:Number;
var randomNumber4:Number; 

randomNumber = getRandom();
randomNumber2 = getRandom();
randomNumber3 = getRandom();
randomNumber4 = getRandom(); 

trace(randomNumber, randomNumber2, randomNumber3, randomNumber4);

poop_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void {  //the void is because we are not returning a value
    //trace("click");
   
randomNumber = getRandom();
randomNumber2 = getRandom();
randomNumber3 = getRandom();
randomNumber4 = getRandom();
//we are putting the call of the function in the event handler
//if you put the function in there it is creating a new instance of that everytime

die01_mc.gotoAndStop(randomNumber);
die02_mc.gotoAndStop(randomNumber2);
die03_mc.gotoAndStop(randomNumber3);
die04_mc.gotoAndStop(randomNumber4);
}  

function getRandom():Number {
    return Math.ceil(Math.random() * 6);
}
            

Class Notes Stuffs

//TIME EVENT

//the first is the delay in  miliseconds. The second is the repeat count
//you have to add an event listener for the timer event.timer

var timer:Timer = new Timer (10,0); //1000 for real clock, 10 to  speed it up so we can see what is happening

timer.addEventListener (TimerEvent.TIMER, onTimer);
timer.start();

function onTimer(event:TimerEvent):void {
// trace(“timer”);
rectangle01.rotation +=6;  //360/60
rectangle02.rotation +=.1;  //360/60/60
rectangle03.rotation +=.00833333;  //
}

// ENTER FRAME EVENTS
//triggered when the timeline goes into a certain frame (in the playhead)
//unless you put in a stop frame it keep going at the specified frame rate

//rotating on the enter frame event



// variables
var speed:Number = 10;

// event listenters
stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
stop_mc.addEventListener(MouseEvent.CLICK, onStop);
go_mc.addEventListener(MouseEvent.CLICK, onGo);


// event handlers
function onFrameLoop(george:Event):void {
    //trace("frame");
    rectangle01_mc.rotation += speed;
} //essentially speed is 10 because that is what set the variable equal to

function onStop(event:MouseEvent):void {
    //trace("stop");
    //speed = 0;
    stage.removeEventListener(Event.ENTER_FRAME, onFrameLoop);
//you have to have both the event and the even handler (just copypaste and change add to remove)

function onGo(event:MouseEvent):void {
    //trace("go");
    //speed = 10;
    stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
}

//if you give negative number it rotates counter clockwise




// TEXT FIELDS

dynamic_txt.text = "Ross Higgins";

button_mc.addEventListener(MouseEvent.CLICK, onClick);

function onClick(event:MouseEvent):void {
    //var theInput:String = input_txt.text;
    //trace(theInput);
    dynamic_txt.text = input_txt.text;
}

//there are 2 different types of text engines now in Flash
//the TLF (CS5) more complex and allows you to link multiple boxes so that text can overflow into the next box
//The classic text box: The Static Text, The Dynamic text box (can give it an istance name so we can update it), Input Text (we can also give it an instance name in order to withdraw the information and use it in our code)
 //the fonts should automatically embedd, in the TLF text fields you have to embed them.