cocos2d-iphonegame-physicsccsprite

Cocos2d bullet movement


As of now I have the bullet (a sprite) using a CCAction moveTo the players position. I have it set up so that the bullet always travels at a constant rate using t = d/v. But I need help so that the bullet passes through the given point and keeps going for a certain distance.

CCSprite * bullet = [CCSprite spriteWithFile:@"Projectile.png"];
    int gunRange = 300;
    int velocity = 300;
    int  t = distanceFromPlayer/velocity;
    CCAction *shoot = [CCMoveTo actionWithDuration:t
                                          position:player.position];
    bullet.position = enemy.position;
    if (distanceFromPlayer <= gunRange) {
        [self addChild:bullet];
        [bullet runAction:shoot];
    }

Need to know how to shoot if in range (I think I have that part), shoot towards the player position and keep going in that direction once there (No idea on how to do this), and for the bullet sprite to be removed after it has traveled a distance equal to the gun range (No idea for this either). Please Help.


Solution

  • This line doesn't make sense to me:

    bullet.position = enemy.position;
    

    Use MoveTo to move the bullet to the enemy position not the above line.

    Also to move the bullet past the enemy to a certain position just use the old high school trigonometry we'll all learned -- SOH CAH TOA for right triangles. You have the angle of the bullet and the distance to the enemy so using the info and right triangle trig you can have the bullet move past the target a certain distance

    Hope this helps!