2011-07-19 8 views
-4

私はジョイスティックでiPhoneゲームを作っています。ジョイスティックの軌道に弾丸を発射する船がありますが、なんらかの理由で弾丸を正しい方向に動かすことができません。誰かが私が間違っているかもしれないことを理解するのを助けることができますか?私はあなたが次の行に間違ってあなたの弾丸の速度を設定していると思いゲームでオブジェクトの軌跡を修正するにはどうすればよいですか?

-(void) shootBulletFromShip:(Ship*)ship 
{ 
    double degrees = [[[NSUserDefaults standardUserDefaults] objectForKey:@"lol"] doubleValue];  
    NSLog(@"%f",degrees); 

    float fDegrees = degrees; 

    velocity = CGPointMake(1, fDegrees); 


    outsideScreen = [[CCDirector sharedDirector] winSize].width; 

    self.position = CGPointMake(ship.position.x,ship.position.y); 
    self.visible = YES; 

    [self scheduleUpdate]; 
} 
+0

質問は? – sidyll

+0

私の船がジョイスティックの軌道で射撃したい。それは仕事ではありません –

+2

あなたはそれらが表すものを記述する変数名を使うべきです。例えば、 'wtf'の名前を' joystickDegrees'のように変更するべきでしょう。 –

答えて

1

は、ここに私のコードです。

velocity = CGPointMake(1, spread); 

spreadの場合は、次のようにあなたはおそらく、あなたのCGPointのx軸とy成分として、その角度の正弦と余弦を渡す必要が角度を示しています。

velocity = CGPointMake(cos(spread), sin(spread)); 

角度をラジアンまたは度で表示するかどうかによって、これを少し変更する必要がある場合があります。

+0

うーん、それは動作しませんが、私はそのjoystick.degrees問題は多分私は前にそれを行う必要があると思う。 –

0
// Calculate the angle of the touch from the center of the joypad 
float dx = (float)joypadBG.position.x - (float)convertedPoint.x; 
float dy = (float)joypadBG.position.y - (float)convertedPoint.y; 

float distance = sqrtf((joypadBG.position.x - convertedPoint.x) * (joypadBG.position.x - convertedPoint.x) + (joypadBG.position.y - convertedPoint.y) * (joypadBG.position.y - convertedPoint.y)); 

// Calculate the angle of the players touch from the center of the joypad 
float touchAngle = atan2(dy, dx); 

// If the players finger is outside of the joypad, make sure the joypad cap is drawn at the // joypad edge. 
if (distance > joypadMaxRadius) 
{ 
    [joystick setPosition:ccp(joypadBG.position.x - cosf(touchAngle) * joypadMaxRadius, joypadBG.position.y - sinf(touchAngle) * joypadMaxRadius)]; 
} 
else 
{ 
    [joystick setPosition:convertedPoint]; 
} 

diff = ccpSub([joystick position], joypadBG.position); 
float angleRadians = atanf((float)diff.y/(float)diff.x); 

float angleOffset = CC_DEGREES_TO_RADIANS(90); 

if(diff.x < 0) 
{ 
    angleRadians += angleOffset; 
} 
else 
{ 
    angleRadians -= angleOffset; 
} 
関連する問題