Hi (again)... So, I am making this space based game. So far, I've been able to do player, enemy, backgrounds, click events, game states, movement... everything perfectly (almost). I did a lot of learning on the way. Now, I moved onto creating the lasers. Here's where the problem started. Did a bit of research on the forums and other places... Here's what I did.
if(!greenlasertex.loadFromFile("images/playerlaser.png"))
return EXIT_FAILURE;
std::list<sf::Sprite> greenlaser(MAX, sf::Sprite(greenlasertex));
for(std::list<sf::Sprite>::iterator greenlaserit = greenlaser.begin(); greenlaserit != greenlaser.end(); greenlaserit++)
{
greenlaserit->setOrigin(greenlaserit->getGlobalBounds().width/2, greenlaserit->getGlobalBounds().height/2);
greenlaserit->setScale(0.1f, 0.1f);
greenlaserit->setPosition(player.getPosition().x, player.getPosition().y);
}
float laserspeed = 800.f;
while(window.isOpen())
{
float timer = clock.restart().asSeconds();
window.clear();
for(std::list<sf::Sprite>::iterator greenlaserit = greenlaser.begin(); greenlaserit != greenlaser.end(); greenlaserit++)
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
greenlaserit->setPosition(player.getPosition().x, player.getPosition().y);
std::list<sf::Sprite>::iterator greenlaserit = greenlaser.begin(), reload;
while(greenlaserit != greenlaser.end())
{
reload = greenlaserit;
reload++;
greenlaserit->move(0.f, -laserspeed * timer);
window.draw(*greenlaserit);
greenlaserit = reload;
}
window.display();
}
Theoretically, I know what this piece of code is doing. It's creating a list of sprites and placing the whole list at the player's position. Then, as soon as the game is started, all the elements in the list of sprites are moving together upwards without any input. When Space key is pressed, the position of all the elements in the list is again reset to player's position. And the whole list moves upward again. Obviously, this is not ideal code and absolutely wrong. Two major problems of this is, 1) All the elements move at a time instead of one at a time. 2) The elements move without any input at the start of the game. What changes must I make in order to place a single element at the player position when input is given and not move until that element is placed? Hope I explained myself clearly. Please point me in the right direction. Thank you.