View Full Version : Math With Actionscript???
biodegradable
11-01-2005, 10:05 PM
alright I would normally do this in Visual Basic but I reformated and lost my copy and it will take 5 hours to download annother copy from Microsoft.com...and i thought you could do this in Flash as well becouse flash is math right? ok here is what I am trying to do. By request of my Teacher I am to make a program to simplify "Converse of Pythogram therom" in flash ....not complicated at all this is all i need.
3 input boxes and 1 result box (4 input)
first 3 boxes are called a, b, and c (instance names) and third box is result. I also have a button to calcultae everything.
This is my math in my head.
if a.^2 + b.^2 = c.^2
then result = "Right Triangle"
This is me trying to put it into Actionscript
if (a.text^2 + b.text^2 = c.text^2) {
then result.text = "Right Triangle"
}
but then I was like wait I wand all this math to be computed by a button
on (release) {
if (a.text^2 + b.text^2 = c.text^2) {
then result.text = "Right Triangle"
}
}
But I know my button is messed up....soooo...what am I doing wrong?
**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 2: Left side of assignment operator must be variable or property.
if (a.text^2 + b.text^2 = c.text^2) {
**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 3: Syntax error.
then result.text = "Right Triangle"
Total ActionScript Errors: 2 Reported Errors: 2
wu-tang
11-01-2005, 10:17 PM
You will need to define them as numeric variables first:
function isRight():Void {
var a:Number = Math.pow(Number(a.text),2);
var b:Number = Math.pow(Number(b.text),2);
var c:Number = Math.pow(Number(b.text),2);
if((a+b) == c) trace("it is right");
else trace("not right");
}
button.onRelease = isRight;
silverfish
11-01-2005, 10:19 PM
and there's no "then" in AS "if" statements, just if (condition) {}
biodegradable
11-01-2005, 10:29 PM
what version of Flash are you using?
I keep getting this error
There is no property with the name 'text'.
and why does right after the if have 2 "("
and what is "isRight"
wow your code is a whole lot more complex than mine 0_0 why does it need to be that complex? it is not a complex code. is it becouse there is no "then" in AS? thats lame
Lastly I don't need it to be a+b=c i need it to be squared or a^2 + b^2 = c^2
amen0
11-01-2005, 10:33 PM
it's not complex but it's the right way to Script :)
isRight is the function that will be executed when the btn is released.
wu-tang
11-01-2005, 10:33 PM
Sorry i named the variables the as same the textfield:
you can do it like this:
on(release){
if((Math.pow(Number(a.text),2)+Math.pow(Number(b.t ext),2)) == Math.pow(Number(c.text),2)) trace("it is right");
else trace("not right");
}
amen0
11-01-2005, 10:45 PM
based on wu-tang, but he named 2 vars the same name
biodegradable
11-01-2005, 10:51 PM
but I need "not right" to be sent to results.text becouse I am going to have to have multiple results.
Like after I have
if a^2 + b^2 = c^2
then results are the triangle is a right triangle
else
if a^2 + b^2 > c^2
then results are the triangle is an acute triangle
else
if a^2 + b^2 < c^2
then results are the triangle is an abtuse triangle
understand? thats why i wanted my results to be in a results.text.
amen0
11-01-2005, 10:55 PM
modify the code to
function isRight():Void {
var a:Number = Math.pow(Number(_root.a_txt.text), 2);
var b:Number = Math.pow(Number(_root.b_txt.text), 2);
var c:Number = Math.pow(Number(_root.c_txt.text), 2);
if ((a+b) == c) {
Result = "it is right";
} else if ((a+b)<c) {
Result = "abtuse triangle";
} else {
Result = "acute triangle";
}
}
button.onRelease = isRight;
biodegradable
11-01-2005, 11:02 PM
**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 1: Statement must appear within on handler
function isRight():Void {
**Error** Scene=Scene 1, layer=Layer 2, frame=1:Line 13: Statement must appear within on handler
button.onRelease = isRight;
Total ActionScript Errors: 2 Reported Errors: 2
I tried my code all i kept getting was acute triangles LOL
MichaelxxOA
11-01-2005, 11:05 PM
@biodegradable:
You are getting this error because you put you're code ON the movieclip, if you select your movieclip and highlight all of the actions then cut them and paste it onto the frame of the main timeline, that will at least get rid of the errors, not guarunteeing that it will work. Take Care.
-Michael
amen0
11-01-2005, 11:06 PM
can u upload ur .fla [edit][no need Solved by MichaelxxOA ]
biodegradable
11-01-2005, 11:11 PM
Here it is 0_0
i had my AS on the button not on the movie itself
talking on the phone I have delayed thinking hold on
ok why won't this work
on(release){
if((Math.pow(Number(a.text),2)+Math.pow(Number(b.t ext),2)) == Math.pow(Number(c.text),2)) trace("Right Triangle");
else if((Math.pow(Number(a.text),2)+Math.pow(Number(b.t ext),2)) >= Math.pow(Number(c.text),2)) trace("Acute Triangle");
else if((Math.pow(Number(a.text),2)+Math.pow(Number(b.t ext),2)) <= Math.pow(Number(c.text),2)) trace("Obtuse Triangle");
else trace("Something went Wrong");
}
all the trace results are Acute Triangles
amen0
11-01-2005, 11:16 PM
like you want
biodegradable
11-01-2005, 11:26 PM
Why dosn't this code work?
amen0
11-01-2005, 11:32 PM
like this
a is in ur code is a var and not instance;
on(release){
if((Math.pow(Number(a),2)+Math.pow(Number(b),2)) == Math.pow(Number(c),2)) trace("Right Triangle");
else if((Math.pow(Number(a),2)+Math.pow(Number(b),2)) >= Math.pow(Number(c),2)) trace("Acute Triangle");
else if((Math.pow(Number(a),2)+Math.pow(Number(b),2)) <= Math.pow(Number(c),2)) trace("Obtuse Triangle");
else trace("Something went Wrong");
}
biodegradable
11-02-2005, 12:49 AM
works perfectly...
but instead of a trace can i have it go to
results.text
using this code
on(release){
if((Math.pow(Number(a),2)+Math.pow(Number(b),2)) == Math.pow(Number(c),2)) trace("Right Triangle");
else if((Math.pow(Number(a),2)+Math.pow(Number(b),2)) >= Math.pow(Number(c),2)) trace("Acute Triangle");
else if((Math.pow(Number(a),2)+Math.pow(Number(b),2)) <= Math.pow(Number(c),2)) trace("Obtuse Triangle");
else trace("Something went Wrong");
}
I am writing Macromedia right now seeing if "then" can be put into next Flash....
becouse i could just write it like
if a.text + b.text = c.text
then results.text = "Right"
amen0
11-02-2005, 11:34 AM
place instead trace(".....");
results.text="YourText";//results the instance name of the text box
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.