0

私はフラッシュが初めてで、コードにバグが見つかりません。宇宙船がベクトルで加速できるようにしたい、最大速度を超えて加速できないようにしたい、加速が止まったときにその速度ベクトルを維持したいが、その後摩擦に苦しむ(スペースダスト、))。(それは第 2 段階で発生します。)

私の数学は正しいと思いますが、velVector でバグが発生し、 NaN が返されることがあります。これが私のコードです。

var friction:Number = .96;
var force:Number = .1;
var maxVel:Number = 3;
var accVector:Object = new Object();
var velVector:Object = new Object();
var velocity:Number;
var acceleration:Number;

船が正しい方向を向いている場合は「加速」機能を実行し、そうでない場合は「ドリフト」機能を実行します。常に「moveship」を実行します

function accelerate():void {
    curRotation.vx = Math.cos(rotation/180*Math.PI);
    curRotation.vy = Math.sin(rotation/180*Math.PI);

    var angle:Number = Math.atan2(curRotation.vy, curRotation.vx);

    velocity = Math.sqrt(velVector.vx^2 + velVector.vy^2); //get velocity in both directions
    acceleration = (maxVel - velocity)/maxVel*force; //this is to make it harder to accelerate when it approaches maxvelocity

    accVector.vx = Math.cos(angle) * acceleration; //accelerate in each dimension
    accVector.vy = Math.sin(angle) * acceleration;

    velVector.vx += accVector.vx; //add acceleration to velocity
    velVector.vy += accVector.vy;
}

function drift():void {
    velVector.vx *= friction; //decrease velocity when not accelerating
    velVector.vy *= friction;       
}

function moveShip():void {
    trace("velocity", velocity)
    x += velVector.vx; //move
    y += velVector.vy;
}

どうもありがとう!

4

1 に答える 1

0

あなたの問題は、値の検証なしで安全でない数学演算を使用することにあると思います。したがって、あなたの velVector は、加速度に依存する accVector に依存します。「加速度」valをカウントする除算演算があります。それでおしまい:

acceleration = (maxVel - velocity)/maxVel*force;

AS3 ではゼロで割ることができ、そのような状況では無限大になります。ただし、0 で除算している場合は NaN を取得する可能性もあります。

var acceleration:Number = 0 / 0;
trace(acceleration); //NaN

NaN で結果を Number または型なし変数 (int ではありません!) に書き込もうとすると、常に NaN が返されます。

var a:Number = 5 * NaN;
trace(a); //NaN

だから、私はここに NaN を与える例を示します:

acceleration = 0 / 0; //NaN

accVector.vx = Math.cos(angle) * acceleration; //NaN regardless on "angel" value

velVector.vx += accVector.vx; //NaN regardless on "velVector" value.

これが役立つことを願っています。

于 2013-04-03T06:05:40.060 に答える