1

学校では放物線の扱い方を教えてくれるゲームを作っています。唯一の問題は次のとおりです。Adobe flash cs6 ActionScript 2で放物線を作成する方法がわかりません。完全に挿入すると表示される「ゲーム内」放物線式を挿入できる必要があります。

4

2 に答える 2

1

私はこれをcurveTo関数を使って管理しました.2次ベジエ曲線を描きます. これがベジエ放物線です。

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 に答える