Iphone 如何在游戏中修正对象轨迹?
我正在制作一个带有操纵杆的iPhone游戏。它有一艘飞船,应该在操纵杆轨道上发射子弹,但由于某种原因,我无法让子弹朝正确的方向移动。有人能帮我找出我做错了什么吗 这是我的密码:Iphone 如何在游戏中修正对象轨迹?,iphone,objective-c,cocoa-touch,cocos2d-iphone,Iphone,Objective C,Cocoa Touch,Cocos2d Iphone,我正在制作一个带有操纵杆的iPhone游戏。它有一艘飞船,应该在操纵杆轨道上发射子弹,但由于某种原因,我无法让子弹朝正确的方向移动。有人能帮我找出我做错了什么吗 这是我的密码: -(void) shootBulletFromShip:(Ship*)ship { double degrees = [[[NSUserDefaults standardUserDefaults] objectForKey:@"lol"] doubleValue]; NSLog(@"%f",deg
-(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];
}
我认为你在下面的一行中错误地设置了子弹的速度
velocity = CGPointMake(1, spread);
如果spread
指示一个角度,则可能应将该角度的正弦和余弦作为CGPoint
的x和y分量传递,如下所示
velocity = CGPointMake(cos(spread), sin(spread));
根据角度是以弧度表示还是以度表示,您可能需要对此稍作修改。//计算触摸屏与操纵手柄中心的角度
// 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;
}
float dx=(float)操纵手柄bg.position.x-(float)convertedPoint.x;
float dy=(float)操纵手柄bg.position.y-(float)convertedPoint.y;
浮动距离=sqrtf((joypadBG.position.x-convertedPoint.x)*(joypadBG.position.x-convertedPoint.x)+(joypadBG.position.y-convertedPoint.y)*(joypadBG.position.y-convertedPoint.y));
//计算玩家从操纵台中心触摸的角度
浮动接触角=atan2(dy,dx);
//如果玩家的手指在操纵手柄外,请确保在//操纵手柄边缘拉上操纵手柄盖。
如果(距离>最大半径)
{
[操纵手柄设置位置:ccp(操纵手柄位置x-cosf(接触角)*操纵手柄最大半径,操纵手柄位置y-sinf(接触角)*操纵手柄最大半径)];
}
其他的
{
[操纵手柄设置位置:转换点];
}
差异=ccpSub([操纵手柄位置],操纵手柄位置);
浮动角度弧度=atanf((浮动)差异y/(浮动)差异x);
浮动角度偏移=CC_度到_弧度(90);
如果(差值x<0)
{
角度弧度+=角度偏移;
}
其他的
{
角度弧度-=角度偏移;
}
我希望我的飞船在操纵杆轨道上开火。它不起作用。你应该使用变量名来描述它们所代表的内容。例如,您可能应该将wtf
的名称更改为类似joystickDegrees
的名称。好吧,它不起作用,但我认为是操纵杆。问题可能是我以前需要对它做些什么。