Fairly new to SFML and I was trying to make a simple 2d shooter without using classes. So instead I started using arrays, yet I can't get the laser beams/bullets to work right. Here's all the relevant code
sf::Sprite sLasor[ARRAY_LENGTH];
int lasorCounter = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && frameCounter > 30){
lasorCounter++;
frameCounter = 0;
sLasor[lasorCounter].setPosition(fArwing.x + 65, fArwing.y);
}
if (lasorCounter >= 19){
lasorCounter = 0;
}
if(sLasor[lasorCounter].getPosition().y > 0){
sLasor[lasorCounter].move(0, -5);
}
window.draw(sLasor[lasorCounter]);
The problem is that every time I press space, the lasers reset and the one I shot earlier just disappears. If I don't shoot another one the bullet will continue going until it exits the screen.
I agree with Turbine's solution to use classes. Preferably contained in an STL container such as an std::vector (http://www.cplusplus.com/reference/vector/vector/), std::deque (http://www.cplusplus.com/reference/deque/deque/), std::list (http://www.cplusplus.com/reference/list/list/), or std::array (http://www.cplusplus.com/reference/array/).
However, with your code, the problem that you're experiencing is likely due to the fact that you only ever manipulated one laser: sLasor[lasorCounter]. That is regardless of what lasorCounter actual is, you only use the "current" lasorCounter value.
If you have 5 lasers, I would expect lasorCounter to be 5. Therefore, instead of just updating and drawing the fifth laser, you should update and draw all lasers up to the fifth:
for (unsigned int laserNumber{ 0u }; laserNumber < lasorCounter; ++laserNumber)
{
sf::Sprite& currentLaser{ sLasor[laserNumber] };
// update and draw "currentLaser"
}