2017-06-02 26 views
0

ステージ上に異なるリングがあり、ボールがこれらの軌道の1つで回転しているコードを書いています。クリックすると、ボールはリングを離れてリングに垂直に移動します。途中で別のリングに当たった場合、リングに当たるポイントからそのリング内で回転を開始します。コードの最初の部分を正常にコーディングすることができました。ボールが別のリングに当たったときに問題が発生しました。ヒットした場所からボールがスタートしません。代わりに、リング内で別の位置から回転を開始します。私は疑いを持って私が明確であることを願っています。ここはhttps://play.google.com/store/apps/details?id=com.drgames.orbit.jumper回転角度計算AS3

このゲームに似ていたフレームを入力中に私のコードです:

if(!isTravelling){ 
     rad = Math.abs(angle * (Math.PI/180)); // Converting Degrees To Radians 
     _ball.x = currOrbit.x + (currOrbit.width/2) * Math.cos(rad); // Position The Orbiter Along x-axis 
     _ball.y = currOrbit.y + (currOrbit.width/2) * Math.sin(rad); // Position The Orbiter Along y-axis 
     trace(_ball.x+" , "+_ball.y); 
     angle -= speed; // Object will orbit clockwise 
     _ball.rotation = (Math.atan2(_ball.y - currOrbit.y, _ball.x - currOrbit.x) * 180/Math.PI) + 90; 
    } 

    for(var i = 0; i < orbits.length; i++){ 
     if(orbits[i] != currOrbit){ 
      if(ObjectsHit(orbits[i], _ball)){ 
       currOrbit = orbits[i]; 
       _ball.rotation = (Math.atan2(_ball.y - currOrbit.y, _ball.x - currOrbit.x) * 180/Math.PI) + 90; 
       isTravelling = false; 
       stage.addEventListener(MouseEvent.CLICK, onClick); 
      } 
     } 
    } 

    if(isTravelling){ 
     _ball.x += Math.cos(rad) * speed * 2; 
     _ball.y += Math.sin(rad) * speed * 2; 
    } 

答えて

1

あなたは新しい軌道の開始角度を計算するのを忘れているように見える:

if(!isTravelling){ 
     rad = Math.abs(angle * (Math.PI/180)); // Converting Degrees To Radians 
     _ball.x = currOrbit.x + (currOrbit.width/2) * Math.cos(rad); // Position The Orbiter Along x-axis 
     _ball.y = currOrbit.y + (currOrbit.width/2) * Math.sin(rad); // Position The Orbiter Along y-axis 
     trace(_ball.x+" , "+_ball.y); 
     _ball.rotation = angle // no need for calculating the angle again 
     angle -= speed; // Object will orbit clockwise 
    } 

    for(var i = 0; i < orbits.length; i++){ 
     if(orbits[i] != currOrbit){ 
      if(ObjectsHit(orbits[i], _ball)){ 
       currOrbit = orbits[i]; 
       angle = (Math.atan2(_ball.y - currOrbit.y, _ball.x - currOrbit.x) * 180/Math.PI) + 90; // Calculate the starting angle on the new orbit 
       _ball.rotation = angle // Set the balls rotation to the angle 
       isTravelling = false; 
       stage.addEventListener(MouseEvent.CLICK, onClick); 
      } 
     } 
    } 

    if(isTravelling){ 
     _ball.x += Math.cos(rad) * speed * 2; 
     _ball.y += Math.sin(rad) * speed * 2; 
    } 

このそれを修正する必要があります。私は100%確信していないので、あなたのボールの回転(_ball.rotation = angle + 90)に90度を加算または減算する必要があるかもしれないことに注意してください。

関連する問題