Help needed(SFML): Creating "pointer array objects" (*object

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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.
counter is always 1 greater than the last bullet you new'd. You should decrement it before the delete. In the if control expression on line 31, you should be checking bulletlist[counter-1]

Line 36 and 39, likewise.

When you have a value that represents the number of items held in an array, it is not a valid index into that array.
Last edited on
Topic archived. No new replies allowed.