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;
}