In my game I want a canon to fire bullets once every second, im half way there but it's getting too complicated for me so I need some help. I made a small example of the bullet code. The code that has been commented causes the program to crash.
Here is the code
Declarations.h
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
unsigned char level_1
(sf::RenderWindow &App,
sf::Image &Bullet_Bill,
class bullet *bulletlist[],
class bullet &bulletMember,
int &counter,
unsigned char &level);
class bullet
{
public:
bullet(){shoot = true;}
sf::Clock clock;
sf::Sprite sprite;
void setShoot(bool setShoot){shoot = setShoot;}
bool getShoot(){return shoot;}
private:
bool shoot;
};
#endif
Main.cpp
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"
using namespace std;
int main ()
{
unsigned char level = 1;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Bullet");
App.SetFramerateLimit(90);
sf::Image Bullet_Bill;
Bullet_Bill.LoadFromFile("Bullet_Bill.png");
int counter = 0;
bullet *bulletlist[100];
bullet bulletMember;
while(App.IsOpened())
{
sf::Event Event;
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
switch (level)
{
case 1:level_1 (App, Bullet_Bill, &bulletlist[counter], bulletMember, counter, level);
}
}
}
level_1.cpp
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include "Declarations.h"
using namespace std;
unsigned char level_1(sf::RenderWindow &App,
sf::Image &Bullet_Bill,
class bullet *bulletlist[],
class bullet &bulletMember,
int &counter,
unsigned char &level)
{
if (bulletMember.clock.GetElapsedTime() > 1.0 && bulletMember.getShoot() == true)
{
cout << "Bang!" << counter << endl;
bulletMember.setShoot(false);
bulletlist[counter] = new bullet;
bulletlist[counter]->sprite.SetImage(Bullet_Bill);
bulletlist[counter]->sprite.SetPosition(App.GetWidth()/2, App.GetHeight()/2);
counter ++;
}
if (bulletMember.getShoot() == false)
{
bulletMember.clock.Reset();
bulletMember.setShoot(true);
}
/*if (bulletlist[counter]->sprite.GetPosition().x < 0 - bulletlist[counter]->sprite.GetSize().x)
{
delete bulletlist[counter];
counter --;
}*/
//bulletlist[counter]->sprite.Move(-150 * App.GetFrameTime(), 0);
App.Clear(sf::Color(255,255,255,255));
//App.Draw(bulletlist[counter]->sprite);
App.Display();
return level;
}
I appreciate all the help I get becuase right now this is frying my brain.