弹丸/子弹方向Cocos2d

时间:2011-12-16 03:54:11

标签: iphone objective-c ios cocos2d-iphone

我正在关注simple cocos2d game

的教程

然而,在该教程中,用户触发的子弹仅在一个方向上

我能做些什么来让它在所有方向上发射而不仅仅是单方面?

这是方向的代码。

int offX = location.x - projectile.position.x;
int offY = location.y - projectile.position.y;

[self addChild:projectile];

int realX = winSize.width + (projectile.contentSize.width/2);
float ratio = (float) offY / (float) offX;
int realY = (realX *ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);

int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1;
float realMoveDuration = length/velocity;

[projectile runAction:[Sequence actions:[MoveTo actionWithDuration:realMoveDuration position:realDest],
                       [CallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)], nil]];

所有帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:4)

假设您正在角色的位置创建射弹,您只需要在计算终点之前找出方向。

添加射弹后:

[self addChild:projectile];

添加标量浮点数:

float scalarX = 1.0f;

如果该字符留下了触摸,则将其设为负数:

if (offX < 0.0f) scalar = -1.0f;

然后将realX乘以此标量,使其指向正确的方式

int realX = scalar * (winSize.width + (projectile.contentSize.width/2));