Reputation: 11
For school I've been creating a game that can teach you how to deal with Parabolas. The only problem is; I don't know how to create a parabola in Adobe flash cs6 ActionScript 2. I need to be able to insert a parabolic formula "in game" which should appear when fully inserted.
Upvotes: 1
Views: 387
Reputation: 180
You needed a field to enter equation.. concatenate this one, to the code, i gave earlier, for FULLY answered question.
tool._yscale = tool._xscale = 100//scaleFactor;
/////////////////////////////////////
//GUI
_root.createEmptyMovieClip("texte",102);
texte.createTextField("formula",103,5,5,100,20);
texte.formula.text = "-2x^2+4x+8"//"place math here";//
texte.formula.type = "input";
texte.formula.border = true;
texte.formula.restrict = "0-9\\+\\x\\-\\^";
//mediate
var _a:Number = 0; //parsed coeficients
var _b:Number = 0; //will be stored
var _c:Number = 0; //here as numbers
//housekeeping
function forMe(i,from,to){return Number(ar[i].substring(from,to))}
//user gave us input
function parseInput(str){
//prepare values
arr = str.split("-");
ar = new Array();//temp
if(arr.length>1){
for(i=0;i<arr.length;i++){
((arr[i]!="")&&(i!=0))?ar[i]="-"+arr[i]:ar[i] ="+"+arr[i]}}
else{ar=arr}
for(i=0;i<ar.length;i++){
arr[i] = ar[i].split("+")} //trace(arr.join());
ar = arr.join().split(",");
for(i=0;i<ar.length;i++){
(ar[i]=="")?ar[i]="+":null;}
//assign values
for(i=0;i<ar.length;i++){
hasX = ar[i].indexOf("x");//shorthands
hasL = ar[i].indexOf("^");
isNeg = ar[i].indexOf("-");
if(hasX!=-1){
if(hasL!=-1){
if(isNeg!=-1){
a=-forMe(i,0,hasX);}
else{
a=forMe(i,0,hasX);}}
else{
(isNeg!=-1)?b=-forMe(i,0,hasX):b=forMe(i,0,hasX);}}
else{
(isNeg!=-1)?c=-ar[i]:c=ar[i];}}
//these need to be reassigned..
from=[-x,parabolaSlope(x,a,b,c)];
to=[x,parabolaSlope(x,a,b,c)];
controlPoint=[0,-parabolaSlope(x,a,b,c)];
}
//event, might be a propper button, your choise!
onMouseUp=function(){
parseInput(texte.formula.text);
tool.clear();
drawLineOrCurve(tool,from,to,props,controlPoint);
}
There is no RexEx in as2 and i think Scale_factor is also not needed here.
Upvotes: 0
Reputation: 180
I managed this using curveTo function, it draws quadric Bezier curve. So here isBezier parabola:
function drawLineOrCurve(mc_where,from,to,props,controlPoint){
(props[2]==null)?props[2]=100:null;//bugsquash!
mc_where.lineStyle(props[0],props[1],props[2]);
mc_where.moveTo(from[0],from[1]);
(controlPoint==null)?
mc_where.lineTo(to[0],to[1]):
mc_where.curveTo(controlPoint[0],controlPoint[1],to[0],to[1]);
return [from[0],from[1],to[0],to[1],controlPoint[0],controlPoint[1]];
}
this.createEmptyMovieClip("tool",100);
function parabolaSlope(x,a,b,c){
//https://en.wikipedia.org/wiki/Parabola
return a*x*x+x*b+c;
}
props=[2,0xff00ff]; //linestyle
a = 1;
b = 6;
c = 0;
x = 10; //max x value
from=[-x,parabolaSlope(x,a,b,c)];
to=[x,parabolaSlope(x,a,b,c)];
controlPoint=[0,-parabolaSlope(x,a,b,c)]; //[this is why control point is negative here][2]
//
drawLineOrCurve(tool,from,to,props,controlPoint); //draw it
trace("(x,y)from "+tool._x+":"+tool._y+" to "+x+":"+parabolaSlope(x,a,b,c));
tool._x=Stage.width/2; //center movieclip
tool._y=Stage.width/2;
tool._rotation = 180; //flash has inverted Y axis
scaleFactor = Stage.height/parabolaSlope(x,a,b,c)*100;
tool._yscale = tool._xscale = scaleFactor;
this is why control point is negative here
Upvotes: 1