I am using cocos2D-x and am trying to achieve the effect of enemies going past and shooting projectiles at the player who is moving around the screen. I am using the code below, which is based on this article (Scroll down to the ’Shooting Projectiles’ section): https://www.raywenderlich.com/95835/cocos2d-x-tutorial-beginners
Point projPos = shooter->getPosition(); projectile->setPosition(projPos); projectile->setVisible(true); Vec2 offset = target->getPosition() - projPos; offset.normalize(); auto shootAmt = offset * 1000; auto projEndPt = shootAmt + projPos; // Approach 1. auto moveAct = MoveBy::create(10.0f, shootAmt); // Approach 2. auto moveAct = MoveTo::create(10.0f, projEndPt); projectile->runAction(moveAct);
It works most of the time, however the projectiles speed varies, it seems based on the distance between the player and shooter. At medium distances the speed is fine, at shorter distances the speed is too slow and at longer distances the speed is too fast. The time being fixed (10.0f) over a variable distances seems to be the cause. I have tried both approaches as commented in the code, moving by move amount (Approach 1) and move to point (Approach 2) and both seem to give the same result.