私はフラッシュが初めてで、コードにバグが見つかりません。宇宙船がベクトルで加速できるようにしたい、最大速度を超えて加速できないようにしたい、加速が止まったときにその速度ベクトルを維持したいが、その後摩擦に苦しむ(スペースダスト、))。(それは第 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;
}
どうもありがとう!