SFML community forums
Help => General => Topic started by: Treena on October 05, 2010, 06:33:15 am
-
I'm trying to create an asteroids game clone, and I'm stuck on creating the bullets. I'm pretty sure I've isolated the problem to being when I push space and increment the variable, numberofbullets, that's when the program crashes. When I removed incrementing the variable in that if statement, the program ran fine, the only problem was I only had one bullet being drawn over and over again. Here's the code:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
#include "Bullet.h"
#include "Ship.h"
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Asteroid Clone");
//Loading ship image
Ship ship1;
ship1.loadImage("ship.png");
Bullet bullet[10]; //Bullet array, will make it larger when I get bullets working
int numberofbullets = 0; // Index to count array of bullets
int i;
while(App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
if(App.GetInput().IsKeyDown(sf::Key::Up))
ship1.moveForward(App);
if(App.GetInput().IsKeyDown(sf::Key::Right))
ship1.rotateRight(App);
if(App.GetInput().IsKeyDown(sf::Key::Left))
ship1.rotateLeft(App);
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
bullet[numberofbullets].SetPosition(ship1); //Set position of bullet based on ship
numberofbullets++; // Incrementing so next time player presses space a new bullet is made
}
App.Clear(sf::Color(0, 0, 0));
//Drawing of sprites
ship1.drawShip(App);
for(i=0; i <= numberofbullets; i++) // Draws number of bullets that have been made
{
bullet[numberofbullets].draw(App);
}
if (numberofbullets == 10) // Resets array, will make larger when I get it working
{
numberofbullets = 0;
}
App.Display();
}
return EXIT_SUCCESS;
}
-
The problem might be the for loop where you draw the bullets. Try changing the condition to i < numberofbullets.
-
What's going on when you've got 11 bullet. :wink:
-
Bullet bullet[10]; //0 to 9, make 10 bullets
int numberofbullets = 0; // Index to count array of bullets
//Here, you are positionning a bullet
//And incrementing the counter : why not
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
bullet[numberofbullets].SetPosition(ship1);
numberofbullets++;
}
//Here you are drawing from 0 to "numberofbullets" which is one over and very problematic when reaching 10 or more !!!
for(i=0; i <= numberofbullets; i++)
{
bullet[numberofbullets].draw(App);
}
//And here, you are correcting the numberofbullets too late
if (numberofbullets == 10)
{
numberofbullets = 0;
}
Try to draw on paper what you are doing. Verify and block your increment before trying to draw it. Be carful with arrays.
Quick and dirty corrections :
//Initialization
std::vector<Bullet> bullets;
//Adding a bullet
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
bullets.push_back(new Bullet(ship1.GetPosition());
}
/Drawing bullets
for (i=0;i<bullets.size();i++)
{
App.Draw(bullets[i]);
}
Isn't it more clear like that for example ?
-
So my problem wasn't incrementing the variables, but when the bullet number got past 9, I had my checking statement too late.
And thanks for the vector suggestion Mindiell. I had only known about arrays and dynamic arrays, so after reading up a little on vectors, it seems like using them would solve my problems. I'm not at home so I can't change my program, but I'm sure that "quick fix" would fix my problem.