Forum Moderators: open

Message Too Old, No Replies

Math Claculations In Flash

I need to know how to add in actionscript.

         

legendarysim

6:15 am on Feb 9, 2005 (gmt 0)

10+ Year Member



Im trying to make a basic claculator that adds only. I got 3 buttons. one for the first number, one for the second, and the final is for the return value. Does any one know if im doing something wrong or im doing it right, the actionscript code.

Please E-mail me if you know anything.
Packrat1990@msn.com

benihana

9:28 am on Feb 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



welcome to WebmasterWorld.

we dont email responses here, we post so they are available for future reference.

what actionscript are you currently using?

legendarysim

10:55 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



I use Macromedia Flash MX2004(7.2) WITH Actionscript version 2.0 and the script i use is (dont amke fun of me if its wrong im a noob at actionscript)

add(mathtest+mathtest2);
return (mathtest3);

whoisgregg

1:44 am on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, legendarysim!

It can be difficult to post info about flash because so much of it is tied up in graphical user interface. It can be a pain posting instructions like "make a new text field, give it an instance of xyz and a var of zyx." However, to learn the actionscript part of things, it's pretty straightforward to post helpful code although it can get a bit long.

The code below will make a entry + entry = result flash movie. The important bit is the first paragraph of code, the rest could be ignored as it just "sets the stage" for the function.

1. Make a NEW flash file.
2. Copy the code below and paste it into the action panel (Window>Development Panels>Actions).
3. Test Movie. (Control>Test Movie)


//this function does the math and returns the result only when it's real
//it needs two input text fields and one dynamic text field.
// In the property window, set the "Var:" of the two inputs to line1 and line2
// and the "Var:" of the dynamic text field to outputResult
function doadd() {
var funcResult;
funcResult = (Number(line1) + Number(line2));
if ((isNaN(funcResult) == false) && isFinite(funcResult)) {
outputResult = funcResult;
} else {
//trace("Value is illegal, "+ line1 +" + "+ line2 +" is either infinite or not a number.");
}};

//Just use all the code and it will make everything it needs.
//These are the fields and the listeners
this.createTextField("mathtest", this.getNextHighestDepth(), 30, 10, 100, 22);
mathtest.border = true;
mathtest.type = "input";
mathtest.variable = "line1";
mathtest.setFocus();
mathtest.onKillFocus = function(textfield_txt:TextField) {
//trace(mathtest._name+" changed");
doadd();
};
mathtest.addListener(txtListener);

this.createTextField("mathtest2", this.getNextHighestDepth(), 30, 32, 100, 22);
mathtest2.border = true;
mathtest2.type = "input";
mathtest2.variable = "line2";
mathtest2.onKillFocus = function(textfield_txt:TextField) {
//trace(textfield_txt._name+" changed");
doadd();
};
mathtest2.addListener(txtListener);

this.createTextField("mathresult", this.getNextHighestDepth(), 30, 64, 100, 22);
mathresult.type = "dynamic";
mathresult.variable = "outputResult";
mathresult.addListener(txtListener);

// Let's make a pretty plus sign and a line:
this.createTextField("plussign", this.getNextHighestDepth(), 10, 36, 100, 22);
plussign.type = "static";
plussign.text = "+";

this.createEmptyMovieClip("dividerline", this.getNextHighestDepth());
dividerline.lineStyle(1, 0x000000, 100);
dividerline.moveTo(10, 59);
dividerline.lineTo(130, 59);
dividerline.endFill();

legendarysim

1:46 am on Feb 10, 2005 (gmt 0)

10+ Year Member



I was WAY! wrong

legendarysim

1:51 am on Feb 10, 2005 (gmt 0)

10+ Year Member



Okay thanks for helping, but i have a question, the field look a lil' tacky for the flash file i want it to go into, is the a way to change the existing fields to textinput fields and still get the math

whoisgregg

10:59 pm on Feb 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for the delay... here's your answer:

In the file you already have made, you should have three text fields. Two of them should be "input" fields with an instance name of "mathtest" and "mathtest2" and the third should be a "dynamic" field with an instance name of "mathresult."

You've probably never used this next value, but with the first input field selected ("mathtest") look in the property panel for a field labeled "Var:". In there, type "line1" without quotes. For "mathtest2" type in "line2" without quotes. Finally, your third field ("mathresult") should be given a "Var:" of "outputResult".

You can visually format these fields however you like.

Then paste this simplified, more streamlined code into the first frame's "actions" panel:

function doadd() {
var funcResult;
funcResult = (Number(line1) + Number(line2));
if ((isNaN(funcResult) == false) && isFinite(funcResult)) {
outputResult = funcResult;
}};
mathtest.onKillFocus = function() { doadd(); };
mathtest2.onKillFocus = function() { doadd(); };

[edited to add that Macromedia tutorial code is horribly bloated -- No one should use the first block of code I posted. Uck. /]

legendarysim

5:06 am on Feb 14, 2005 (gmt 0)

10+ Year Member



Thanks Ill try it as soon as possible.

legendarysim

5:31 am on Feb 14, 2005 (gmt 0)

10+ Year Member



I cant find the "var:" thing in properties panel.

legendarysim

6:09 am on Feb 14, 2005 (gmt 0)

10+ Year Member



Its getting mindnumbing, so i hope you respond soon.

whoisgregg

6:43 am on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Select the textbox first, then in the properties panel, the "Var:" is toward the bottom right corner, just left of the "Character..." button. (At least, in my version of flash which I was certain is the same as yours.)

Also, check your stickymail, I am sending a link to a screenshot in case my description is insufficient. :)

legendarysim

7:05 am on Feb 14, 2005 (gmt 0)

10+ Year Member



read the sticky i gave you, this will help u understand what im trying to say (sorry im confusing you with my noob stuff)

i mean i want to use the textboxes in the components area on the right hand side

whoisgregg

10:40 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've actually never used the component inputs, sorry. I've never liked the look of them so I always just make my own componenets. Perhaps someone else can be of more assistance?

legendarysim

3:50 am on Feb 16, 2005 (gmt 0)

10+ Year Member



LOL. Okay. It just for the program im making, it almost requires a box like that to make it look decent lol. well, when the information you gave me, i try to see if i can form up something.