我正在使用 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 添加到负角度),但这导致了相同的端点。
任何人都可以帮助或给我一些尝试吗?
谢谢!