Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Shooting projectiles Straight **Advice needed***  (Read 2422 times)

0 Members and 1 Guest are viewing this topic.

Pacman

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Shooting projectiles Straight **Advice needed***
« on: November 23, 2014, 01:21:26 am »
Hey SFML coders !
How do i get a sprite to be fired as projectile and move straight ahead .

Please explain Precise and simple ... your knowledge will be appreciated
Here is the code i have.

 
/////////////////////////////
   ///Create the bullet
////////////////////////////
void Bullet::createBullet(sf::Texture &bulletTexture, sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
  if(!bulletTexture.loadFromFile("Images/missile.png"))///creates the bullet
        cout << "ERROR:bullet could not load" << endl;

    bulletImage.setTexture(bulletTexture);///
    sf::Vector2f shipLocation = shipImage.getPosition(); ///Gets the location of the ship

    float locationX = shipLocation.x - 83;///sets the X location of the ship places the bullet of  it
    float locationY= shipLocation.y +4 ;///sets the Y loction of the ship

    bulletImage.setPosition(locationX, locationY);
}
 

////////////////////////
///Move the bullet
////////////////////////
void Bullet::moveBullet(sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
    float moveX = shipImage.getPosition().x;
    bulletImage.move(-3,0);
}

 


BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Shooting projectiles Straight **Advice needed***
« Reply #1 on: November 23, 2014, 03:35:30 am »
Hey SFML coders !
How do i get a sprite to be fired as projectile and move straight ahead .
Now honestly.
That is very poorly described what you want, how about you put more information of what exactly you want to have, and then i help you.
Stuff like:
In my game i want to shoot a gun, and projectiles to travel in direction of mouse. How do i do this?

To also answer your question:
angle_in_degrees is just angle in degrees...
distance_to_move is value you move the spite in given angle.
Code: [Select]
posX += cos(angle_in_degrees)*distance_to_move;
posY += sin(angle_in_degrees)*distance_to_move;

BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

 

anything