SFML community forums

Help => Graphics => Topic started by: OutlawLee on October 19, 2012, 10:43:17 pm

Title: Best way to draw projectiles ? [SOLVED]
Post by: OutlawLee on October 19, 2012, 10:43:17 pm
Im making a game like space invaders, you fly a spaceship and destroy enemies. I got the player spaceship loading and displaying and moving. Now i want to make projectiles, but i cant get it to work, how should i do it ?
projectiles from your spaceship obviously.

I tried something but kinda failed:

Inside of a class:

static sf::Texture _projectileTexture;
static sf::Sprite _projectileSprite[30];

inside of a .cpp

_projectileTexture.loadFromFile("projectile1.png");
for(int n = 0; n < 30; n++)
      _projectileSprite[n].setTexture(_projectileTexture);

for(int n = 0; n < curProjectile;  n++)
       gameWindow.draw(_projectileSprite[n]);

how i try to make new projectiles:

if(inProjectile == true)
{
        curProjectile ++;
        pProjectile[curProjectile];
        _projectileSprite[curProjectile].setPosition(pProjectile[curProjectile].posX, pProjectile[curProjectile].posY);
        return;
}

General declarations:

int curProjectile = -1;

struct Projectile
{
        float posX;
        float posY;
        bool alive;
};
Projectile *pProjectile = new Projectile[30];

running in main so i can initialize

for(int n(0); n < 30; n++)
{
        pProjectile[n].posX = 20;
        pProjectile[n].posY = 20;
}
 

suggestions please :D


Let me explain here what i did:

1. Load the projectile .png into a texture.
2. Load the texture into 30 sprite projectiles. (hoping that it will be enough)
3. Create with the Struct 30 projectiles.
4. Create/Set position of each one as they get shot.
Title: Re: Best way to draw projectiles ?
Post by: Foaly on October 20, 2012, 12:21:59 am
Please discribe more precise what you want to achieve. Also what exactly failed in your code?
Title: Re: Best way to draw projectiles ?
Post by: OutlawLee on October 20, 2012, 10:44:15 am
I want to shoot with my ship.
They are not displaying, and i dont know if this is a efficient method to make new projectiles.
Title: Re: Best way to draw projectiles ?
Post by: OutlawLee on October 20, 2012, 12:49:28 pm
FIXED.