Hi there, I'm trying to draw a certain amount of zombies to the screen during a wave. My program runs, but whenever I switch states to the playing state, the program crashes (probably due to a pointer problem). Heres my code:
PlayState.hpp:#include "Player.hpp"
#include "Background.hpp"
#include "Button.hpp"
#include "ZombieWave.hpp"
class GameEngine;
class PlayState : public GameState
{
public:
PlayState(GameEngine& game, bool replace = true);
void pause();
void resume();
void update();
void draw();
private:
sf::Event event;
Player* player;
ZombieWave zm;
Background map;
Background widthBorder1, widthBorder2;
Background heightBorder1, heightBorder2;
bool waveDone;
};
PlayState.cpp:#include <memory>
#include <iostream>
#include "Engine.hpp"
#include "PlayState.hpp"
#include "Player.hpp"
#include "IntroState.hpp"
PlayState::PlayState(GameEngine& game, bool replace) : GameState(game, replace) {
zm.loadZombies(5);
std::cout << "PlayState Init" << std::endl;
player = player->createPlayer();
std::string playerFilepath = "Resources/headup.png";
player->init(playerFilepath, 475, 300);
std::string mapFilepath = "Resources/land.png";
map.init(mapFilepath, 0, 0);
std::string widthBorderpath = "Resources/widthborder.png";
widthBorder1.init(widthBorderpath, 0, 0);
std::string heightBorderpath = "Resources/heightborder.png";
heightBorder1.init(heightBorderpath, 0, 0);
widthBorder2.init(widthBorderpath, 0, 747);
heightBorder2.init(heightBorderpath, 1004, -25);
waveDone = false;
}
void PlayState::pause() {
std::cout << "PlayState Pause" << std::endl;
}
void PlayState::resume() {
std::cout << "PlayState Resume" << std::endl;
}
void PlayState::update() {
if (!waveDone)
zm.spawnZombies();
while(m_game.screen.pollEvent(event)) {
switch(event.type) {
case sf::Event::Closed:
m_game.quit();
break;
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Escape:
m_next = m_game.build<IntroState>(true);
break;
case sf::Keyboard::W:
player->kc.setKeyboardEvent(W);
if (!player->playerCollisionTest(widthBorder1.getSprite()))
player->movePlayer(event);
break;
case sf::Keyboard::A:
player->kc.setKeyboardEvent(A);
if (!player->playerCollisionTest(heightBorder1.getSprite()))
player->movePlayer(event);
break;
case sf::Keyboard::S:
player->kc.setKeyboardEvent(S);
if (!player->playerCollisionTest(widthBorder2.getSprite()))
player->movePlayer(event);
break;
case sf::Keyboard::D:
player->kc.setKeyboardEvent(D);
if (!player->playerCollisionTest(heightBorder2.getSprite()))
player->movePlayer(event);
break;
}
break;
while (!waveDone)
zm.moveZombies(player);
}
}
}
void PlayState::draw() {
// Clear the previous drawing
m_game.screen.clear();
map.draw(m_game.screen, event);
widthBorder1.draw(m_game.screen, event);
widthBorder2.draw(m_game.screen, event);
heightBorder1.draw(m_game.screen, event);
heightBorder2.draw(m_game.screen, event);
player->draw(m_game.screen, event);
m_game.screen.display();
}
Zombie.hpp:#include "Drawable.hpp"
#include "Player.hpp"
class Zombie : public Drawable
{
public:
void init(std::string& filename, float x, float y) {};
void init(std::string& filename);
void draw(sf::RenderWindow&, sf::Event);
sf::Sprite& getSprite();
void move(Player*);
void die() { dead = true; }
void attack(Player*);
void setHealth(int);
void decreaseHealth(int);
int getHealth() { return m_health; }
sf::Vector2f getPosition() { return sprite.getPosition(); }
bool isDead() { return dead; }
protected:
bool dead;
unsigned int m_health;
unsigned int m_damage;
};
Zombie.cpp:
#include <iostream>
#include "Zombie.hpp"
void Zombie::init(std::string& filename)
{
srand(time(NULL));
float randX = rand() % -20 - 1;
float randY = rand() % -20 - 1;
texture.loadFromFile(filename);
sprite.setTexture(texture);
sprite.setPosition(randX, randY);
dead = false;
}
void Zombie::draw(sf::RenderWindow& screen, sf::Event event)
{
}
void Zombie::move(Player* player)
{
if (dead)
std::cout << "Zombie is dead. Cannot move" << std::endl;
else
sprite.move(player->getPlayerCoords().x, player->getPlayerCoords().y);
}
void Zombie::attack(Player* player)
{
}
void Zombie::setHealth(int health)
{
m_health = health;
}
void Zombie::decreaseHealth(int health)
{
m_health -= health;
}
sf::Sprite& Zombie::getSprite()
{
return sprite;
}
ZombieWave.hpp:#include <vector>
#include "Zombie.hpp"
class ZombieWave
{
public:
ZombieWave() {};
~ZombieWave()
{
zombies.clear();
delete tempZombie;
}
void loadZombies(int numZombies);
void spawnZombies();
void moveZombies(Player* player);
int getWaveNum() { return waveNum; }
int getNumZombies() { return numZombies; }
private:
int waveNum;
int numZombies;
std::vector<Zombie*> zombies;
Zombie* tempZombie;
};
ZombieWave.cpp:#include "ZombieWave.hpp"
void ZombieWave::loadZombies(int numZombies)
{
for (int f = 0; f < numZombies; f++)
zombies.push_back(new Zombie);
}
void ZombieWave::spawnZombies()
{
for (unsigned int f = 0; f < zombies.size(); f++)
{
auto iter = zombies.begin();
while (iter != zombies.end())
{
tempZombie = *iter;
std::string tempZombieString = "Resources/zomheadleft.png";
tempZombie->init(tempZombieString);
numZombies++;
}
}
tempZombie = NULL;
}
void ZombieWave::moveZombies(Player* player)
{
for (unsigned int f = 0; f < zombies.size(); f++)
{
auto iter = zombies.begin();
while (iter != zombies.end())
{
tempZombie = *iter;
tempZombie->move(player);
}
}
}
Thanks for hearing me out