Exercise 7 Calculator
syllabus | schedule | assignments | Web Resources | ARTC courses
Name your file: username_ex7.FLA. You will turn in the FLA file, not the SWF.

1) Create a button about 34 x 34 and name it one:

  • Make whatever type of over and down states that you want, inside of the button's timeline.
  • While still inside of this timeline, make a new layer, and use text tool to type the number 1, center it over the button. Make this a Static text box.

2) Open the Library, select the button you just made and go to: Library > Options > Duplicate:

  • A pop-up box will appear where you should rename this to two.
  • Double-click the button named two, entering symbol edit mode, and on the top layer, change the number 1 to a number 2.

3) Repeat this process for numbers 3-9 and 0. Also repeat this process for a decimal point, multiplication, division, plus, minus, clear and equals symbols. You should now have a library of buttons for:

1 2 3 4 5 6 7 8 9 0  .  -  +  =  x  /  C

4) Go back to the main timeline and delete any objects or graphics that may be on the main stage.

5) Create and name 3 layers: body, window, buttons:

  • On the body layer, make a rectangular shape, about 250 x 300 px in proportion.
    • (Later you may go back and adjust the look and shape of this.)
  • On the window layer, make a rectangle box about 150 x 25 px with a color that is contrasting to the body.
    • Place it towards the top.
  • Make a dynamic text box on top of that and set:
    • Instance Name = thewindow
    • All other boxes should be unselected.
  • Make the font for this dynamic text box a color that contrasts with the small rectangle box on this layer, and make the font size around 15 and Helvetica for the font type.
  • On the buttons layer, drag in secession, each of the buttons from the library.
  • Arrange them in a sensible fashion on top of the body and around the window.

6) Select Zero button (by clicking on it once - don't double click), and add the following code in the actions panel:


on (release) {

AddDigit("0");

}

• Do the same for buttons 1-9, except put the appropriate number for the AddDigit("number") parameter.

7) Click on the Multiplication button and type:


on (release) {

DoOperator("x");

}

• Type the same code for the other operators, plus, minus and division, except type ("+"), ("-"), and ("/"), respectively.

8) Click on the Decimal button and type:

on (release) {

if (!decimal) {
AddDigit(".");
decimal = true;
}

}

9) Click on the Equals button and type:

on (release) {

DoOperator();

}

10) Click on the Clear button and type:

on (release) {

thewindow.text = "0";
decimal = false;

}

11) Add an actions layer to your main time line and place this code on frame 1:

//function for adding characters into the window
function AddDigit(digit) {

// Add a digit to the window
if (clear) {

clear = false;
decimal = false;
thewindow.text = "0";

}

if (thewindow.text == "0" and digit != ".") {

thewindow.text = digit;

} else {

thewindow.text = thewindow.text + digit;

}

}

//doing an operation with the number characters
function DoOperator(newOper) {

if (operator == "+") {

thewindow.text = Number(operand1) + Number(thewindow.text);

}

if (operator == "-") {

thewindow.text = Number(operand1) - Number(thewindow.text);

}

if (operator == "x") {

thewindow.text = Number(operand1) * Number(thewindow.text);

}

if (operator == "/") {

thewindow.text = Number(operand1) / Number(thewindow.text);

}

operator = "=";
clear = true;
decimal = false;

if (newOper != null) {

operator = newOper;
operand1 = thewindow.text;

}

}

12) TEST movie.

Example:


Student Sample

NOTE: Consider doing one or more of the following:

  • Design a more intricate interface for the user to enter info.
  • Add a memory function or a square root operator.
  • Use the buttons to trigger animations or other visual elements.