0

我正在使用 cocos2d 制作游戏,并且我正在尝试从当前敌人位置以一定长度的敌人角度创建射击路径。

这是我用来旋转和射击枪的代码。

gun runAction: [CCSequence actions: [CCDelayTime actionWithDuration:2.0],
                 [CCCallBlockN actionWithBlock:^(CCNode *node)
                  { [self shootEnemyLaserFromPositionAtAngle:gun];}],
                 [CCRotateTo actionWithDuration:0.5 angle:250],
                 [CCCallBlockN actionWithBlock:^(CCNode *node)
                  { [self shootEnemyLaserFromPositionAtAngle:gun];}],
                 [CCRotateTo actionWithDuration:0.5 angle:230],
                 [CCCallBlockN actionWithBlock:^(CCNode *node)

我正在使用这个方程来计算向量(这个代码在 shootEnemyLaserFromPositionAtAngle 中):

CGPoint endPoint = CGPointMake(gun.position.x + (800 * cos(gun.rotation)), gun.position.y + (800 * sin(gun.rotation)));
NSLog(@"end Point x: %f, %f", endPoint.x, endPoint.y);

shipLaser.position = gun.position;

CGPoint shootVector = ccpNormalize(ccpSub(endPoint, shipLaser.position));
CGPoint shootTarget = ccpMult(shootVector, _winSize.width*2);

[shipLaser runAction:[CCSequence actions: [CCMoveBy actionWithDuration:4.0 position:shootTarget],
                      [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)], nil]];

我的问题是它最终会朝奇怪的方向射击。我的端点值是这样打印出来的。我还打印了下面的枪旋转角度:

angle: -90.000000
end Point x: 21.541107, -499.593536
angle: -110.000000
end Point x: -419.216644, 250.997940
angle: -130.000000
end Point x: 86.166939, 959.688538

我注意到角度突然变负了。我尝试更改为正角度(将 360 添加到负角度),但这导致了相同的端点。

任何人都可以帮助或给我一些尝试吗?

谢谢!

4

2 回答 2

2

和函数cos()sin()从弧度输入返回一个值。你想用CC_RADIANS_TO_DEGREES它来修复它。

于 2012-10-01T22:01:02.520 回答
1

gun.rotation如果试图找到它的正弦或余弦,您需要将度数转换为弧度。使用float gunRotation = CC_DEGREES_TO_RADIANS(gun.rotation);(或double)并将其传递给三角函数。当然,这些片段之外可能还有其他问题,所以如果您发现其他相关问题,请更新问题。

于 2012-10-01T21:57:09.587 回答