Firing bullets , Help me!

I have a Point A (origin) and B (touch). I want that the bullet move from A to B, but if the bullet doesn’t crash with a target in that point, I want it to keep on moving in the same direction till disappear off the screen.
Is there any method to do that? Is a math question , isnt it? Im thinking on vector director but I dont know. Right know what I get is that where I touch , is where the sprite stop and I need it to keep moving till out screen.

CCPoint a;
CCPoint b;
CCPoint direction;

direction = ccpSub(b, a); // take vector direction
direction = ccpNormalize(direction); // normalize (look at your math book :wink: )

…there is a sprite:

CCPoint p = sprite~~>getPosition;
p = ccpAdd);
sprite~~>setPosition§;

bye

Antonio Rodríguez wrote:

I have a Point A (origin) and B (touch). I want that the bullet move from A to B, but if the bullet doesn’t crash with a target in that point, I want it to keep on moving in the same direction till disappear off the screen.
Is there any method to do that? Is a math question , isnt it? Im thinking on vector director but I dont know. Right know what I get is that where I touch , is where the sprite stop and I need it to keep moving till out screen.

Stefano Campodall’Orto wrote:

CCPoint a;
CCPoint b;
CCPoint direction;
>
direction = ccpSub(b, a); // take vector direction
direction = ccpNormalize(direction); // normalize (look at your math book :wink: )
>
…there is a sprite:
>
CCPoint p = sprite~~>getPosition;
p = ccpAdd);
speed = bulletspeed * deltatime;
sprite~~>setPosition§;
>
>
bye
>
>
>
Antonio Rodríguez wrote:
> I have a Point A (origin) and B (touch). I want that the bullet move from A to B, but if the bullet doesn’t crash with a target in that point, I want it to keep on moving in the same direction till disappear off the screen.
> Is there any method to do that? Is a math question , isnt it? Im thinking on vector director but I dont know. Right know what I get is that where I touch , is where the sprite stop and I need it to keep moving till out screen.

In your solution you set a position in the end, but I want to set the position with an action, but what In doind in this code is to ste the sprite where I touch, and I want it to keep movin off screen!

CCTouch* touch =(CCTouch*) touches~~>anyObject;
CCPoint location = touch~~>locationInView();
location = CCDirector::sharedDirector()>convertToGL;
CCSize winSize = CCDirector::sharedDirector
>getWinSize;
CCSprite *projectile = CCSprite::create);
projectile~~>setPosition);
int offX = location.x-projectile~~>getPosition().x;
int offY = location.y-projectile~~>getPosition.y;
if return;
this~~>addChild(projectile);

int realX = location.x + (projectile~~>getContentSize.width/2);
float ratio = offX/ offY;
int realY = location.y;
CCPoint realdest = ccp;
int offRealX = realdest.x~~ projectile~~>getPosition.x;
int offRealY = realdest.y~~ projectile~~>getPosition.y;
float length = sqrtf+);
float velocity = 460/1;
float realMoveDuration = length/velocity;
CCActionInterval* actionMove = CCMoveTo :: create;
projectile~~>runAction(actionMove);

sorry… but I do not use any “action” in my games. I use a single update function in my ‘Scene’, and update every objects inside it.

I would write:

  • class for a bullet
  • inside class, create a update function

class bullet
{

bool update(float dt)
{
… move with the code from the last post and

if( (sprite~~>getPosition.x > screenWidth || sprite~~>getPosition().x < 0) ||
(sprite~~>getPosition.y > screenHeight || sprite~~>getPosition().y < 0))
{
return true; // out of screen
}

}
}

  • create a function to generate a bullet and insert it in a list (or array)
  • inside of the function update on scene, update every bullets

for(int i = bullets.size()~~1; i >= 0; i—)
{
bullet* o = bullets[i];
if)
{
delete o;
bullets.erase*i); // bye bye bullet
}
}
This is a my solution for create a bullet and update it out the screen… but I think everybody have their solutions :slight_smile:
Cheers!

Stefano
Antonio Rodríguez wrote:

In your solution you set a position in the end, but I want to set the position with an action, but what In doind in this code is to ste the sprite where I touch, and I want it to keep movin off screen!
>
>
CCTouch* touch = touches~~>anyObject;
CCPoint location = touch~~>locationInView;
location = CCDirector::sharedDirector~~>convertToGL;
>
CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
CCSprite *projectile = CCSprite::create);
projectile~~>setPosition);
>
int offX = location.x-projectile~~>getPosition.x;
int offY = location.y-projectile~~>getPosition.y;
>
if return;
>
this~~>addChild;
>
int realX = location.x* .width/2);
float ratio = offX/ offY;
int realY = location.y;
>
CCPoint realdest = ccp;
>
int offRealX = realdest.x~~ projectile~~>getPosition.x;
int offRealY = realdest.y~~ projectile~~>getPosition.y;
>
float length = sqrtf+);
float velocity = 460/1;
>
float realMoveDuration = length/velocity;
>
CCActionInterval* actionMove = CCMoveTo :: create;
>
projectile~~>runAction(actionMove);

ha…just the right code i want to make for my bullet.Thx Stefano Campodall’Orto:)