Cocos2d Shell / Bullet Direction

I follow the tutorial on the simple cocos2d game .

however, in this tutorial, bullets that are launched by the user are in only one direction

What can I do to make it work in all directions, not only on one side?

here is the direction code.

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]]; 

All help would be greatly appreciated. Thanks

+4
source share
1 answer

Assuming that you are creating a projectile in the place of your character, you just need to figure out the direction before calculating the end point.

After adding the projectile:

 [self addChild:projectile]; 

Add scalar float:

 float scalarX = 1.0f; 

And make it negative if the touch is left from the symbol:

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

Then just multiply realX by this scalar so that it points the correct path

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

Source: https://habr.com/ru/post/1386639/


All Articles