Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help needed: Creating "pointer array objects" (*object[])  (Read 1440 times)

0 Members and 1 Guest are viewing this topic.

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Help needed: Creating "pointer array objects" (*object[])
« on: April 21, 2012, 06:01:24 pm »
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
Code: [Select]
#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

Code: [Select]
#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

Code: [Select]
#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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help needed: Creating "pointer array objects" (*object[])
« Reply #1 on: April 22, 2012, 04:26:24 am »
Forget arrays and use STL containers like std::vector instead. You should also learn how to work with iterators.

Have you got a good book like the C++ primer? Because this things are more difficult to learn on the internet, often important background knowledge is omitted.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Dannehood

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Help needed: Creating "pointer array objects" (*object[])
« Reply #2 on: April 22, 2012, 09:25:04 am »
I have watched a few videos of the stl containers, vector, map, list, set but I nevered used any of them before so I would be very happy if you could give me an example,

 

anything