Introduction to Flash and Actionscript

Variables and Arrays

Variables in Actionscript 3.0

General Information


The syntax for declaring a variable is as follows:

 

//var variableName:dataType;

Declaring Variables

Numeric Variables

var intQuantity:int; // Whole number

var numPrice:Number; // Decimal number.

var blnActive:Boolean; //boolean (true or false)

var strName:String; // string

Assigning Values to Variables ( variableName = value;)

intQuantity = 12;

numPrice = 1.25;

blnActive = true;

strName = "Ms. Gravelle"

 

Printing Values of Variables using trace

 

- add the following lines and test your program

 

trace("Hello : " + strName);

trace("The quantity is: " + intQuantity);

trace("The price is: " + numPrice);

trace("The total is: " + numPrice * intQuantity);

 

- change the values and add additional trace lines to produce the following output


Hello Ms. Gravelle

The quantity is: 12
The price is: 1.25
The total is: 15


The values have been changed.

 

Goodbye Mr. Smith
The new quantity is now: 5
The new price is now: 2.75
The new total is now: 13.75



Using Text Boxes

 

myTextBox. text = "A String of Information"

 

myTextBox.text = strName // StrName is a string variable

 

myTextBox.txt = String (intAge) // intAge is an integer variable so we change it to a String

 

 

 

Tutorial #1 - Using Text Boxes to Display Output

Create a TextBox on the Stage.

Make it Classic Text, Dynamic and embed it

Give it the instance name myTitleBox_txt

 

Using Code add a Title into the textbox

 

 

 

Now create 3 variables to store the following data: Name, age, date of birth (string, int, string)

Assign values to the variables

Create 3 text boxes for headings.

myAgeTitle_txt, myNameTitle_txt, myDOBTitle_txt and write messages into them and format.

myAgeTitle_txt.text = "Age" // Assign heading to Title Box

Create 3 text boxes to store values and 3 buttons. (Under the Headings)

myAge_txt, myName_txt, myDOB_txt,

myAge_btn, myName_btn, myDOB_btn

 

When the button is clicked display the information in the appropriate text box using the variables

 

 

myName_txt.text = strName // StrName is a string variable

 

myTextBox_txt.text = String (intAge) // intAge is an integer variable so we change it to a String before putting in text box

 

Make the boxes interesting and nicely formatted

Every line of text must be commented.

 

Assignment #1 - Restaurant Bill

Every line must be commented.
You must use variables for all your output values.

A fast food restaurant charges $4.89 for burgers, $2.59 for fries and $1.50 for sodas.

Create an Bill that calculates the total cost of burgers, fries, and sodas and then displays the total, the tax (13%) and the final cost and a thankyou message from the waitress.

The order is for 5 burgers, 6 fries, and 7 sodas.

Create the following variables: numBurgerPrice, numFryPrice, numSodaPrice, intBurgers, intFries, intSodas, strWaitress

Assign values to each of the variables for price, quantity, and waitress name.

Create a variable for subTotal (numsubTotal) and calculate the value.

Create a variable for tax (numTax) and for total (numTotal) and calculate each.

Note: to round to 2 decimal places use:

//Rounds to 2 decimal places

myanswer_txt.text = numTotal.toFixed(2)

Make the output interesting, include graphics!

 

Tutorial #2 - Getting Information from TextBoxes

Instead of assigning values in our code, we can get variable values from the user using input Text Boxes.

Create 2 Textboxes one for age and one for Name. Set them as input in the Properties box.

 

Assignment #2 - Bill of Sale

Create a purchase program that has input boxes for the following.

Product Purchased
Price
Quantity

You must allow the user to purchase 3 items.

Then in a Text Box display a Bill that includes product name, price, quantity, subtotal, tax, and final total.

Then allow the user to enter the amount of cash they are paying with and display the change they will received.

 


 

Post your .fla and .swf. There is no Rubric for this section,
it was for learning purposes and will only be evaluated for completeness (20 marks) and comments(20 marks).
Remember every line needs a comment. Yes!!!!! Every line.

Above and Beyond Assignment #4 - Sleep ZZZZZZZZ

Calculate the number of hours of your life that you have spent sleeping. Assume that you sleep 8 hours each night. To simplify the problem, assume that there are 30 days in each month and 365 in each year. 

Input:

Enter your birth date:
Year: 1990
Month: 9
Day: 8
Enter today’s date:
Year: 2006
Month: 2
Day: 12

Output:

You have been alive for 5,634 days.
You have slept 45, 072 hours.


 

Above and Beyond Assignment #5 - Pizza

Design a pizza ordering program that includes at least the following:

size, quantity, toppings, drinks.

This is your Above and Beyond assignment so make sure to add additional items and features to obtain all possible marks.