【问题标题】:How can I fix object trajectories in my game?如何修复游戏中的对象轨迹?
【发布时间】:2011-07-19 18:53:17
【问题描述】:

我正在用操纵杆制作 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];
}

【问题讨论】:

  • 我希望我的飞船在操纵杆轨迹上开火。它不起作用
  • 您应该使用描述它们所代表的变量名称。例如,您可能应该将wtf 的名称更改为joystickDegrees

标签: iphone objective-c cocoa-touch cocos2d-iphone


【解决方案1】:

我认为您在以下行中错误地设置了子弹的速度。

velocity = CGPointMake(1, spread);

如果spread 表示一个角度,您可能应该将该角度的正弦和余弦作为CGPoint 的 x 和 y 分量传递,如下所示。

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

您可能需要稍作修改,具体取决于您的角度是以弧度还是度数表示。

【讨论】:

  • 好吧,它不起作用,但我认为它的操纵杆。度问题也许我需要先用它做点什么。
【解决方案2】:
// 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    相关资源
    最近更新 更多