在学校,我一直在创建一个可以教你如何处理抛物线的游戏。唯一的问题是;我不知道如何在Adobe flash cs6 ActionScript 2中创建抛物线。我需要能够在“游戏中”插入一个完全插入时应该出现的抛物线公式。
问问题
374 次
2 回答
1
我使用curveTo函数管理这个,它绘制二次贝塞尔曲线。所以这里是贝塞尔抛物线:
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;
于 2014-11-21T14:26:12.440 回答
0
您需要一个字段来输入方程式。将这个字段连接到我之前给出的代码,以获得完全回答的问题。
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);
}
as2 中没有 RexEx,我认为这里也不需要 Scale_factor。
于 2014-11-22T17:55:25.533 回答