Hello!
So I'm getting a stange error when I'm creating a array from something I call "Obstacle", the problem I'm having is when I'm trying to load the array(sprites) they are all turning white. Now to the strange everything works perfectly when I'm not having the array (only loading one object).
I think that I'm loading the array too many times so that everything get overwriten.
I'm not going to post the Object and the Obstacle code tho I known thats right beacuse it works when I'm creating only one obejct in gameHandler.
gameHandler.h(creating the objects here and calling to load the sprites)
#pragma once
#include "Player.h"
#include "Obstacle.h"
#include "Background.h"
class gameHandler :public sf::Drawable
{
private:
static const int CAP = 3;
Player player = ("img/playerSheet.png");
Obstacle obstacles[CAP];
//Obstacle obstacles = ("img/obstacle.png");
Background bg = ("img/cloud.png");
void draw(sf::RenderTarget &target, sf::RenderStates states)const;
gameHandler.cpp
calling for the constructor to load the imagegameHandler::gameHandler()
{
for (int i = 0; i < this->CAP; i++)
{
this->obstacles[i] = Obstacle("img/obstacle.png");
}
}
drawing the spritesvoid gameHandler::draw(sf::RenderTarget &target, sf::RenderStates states)const
{
target.draw(this->bg, states);
target.draw(this->player, states);
target.draw(this->obstacles[0], states);
target.draw(this->obstacles[1], states);
}
update functionvoid gameHandler::update()
{
this->player.jump();
for (int i = 0; i < this->CAP; i++)
{
this->collision(this->obstacles[i]);
}
int yPos = rand() % 690+200;
int xPos = rand() % 200 + 1;;
this->obstacles[0].moveObstacle(1000, -yPos);
this->obstacles[1].moveObstacle(1000, 1050 - yPos);
}
game.cpp (main)
#include <sfml/Graphics.hpp>
#include "gameHandler.h"
#include <iostream>
using namespace std;
int main()
{
gameHandler handler;
sf::RenderWindow window(sf::VideoMode(1000, 900), "Flappy bird <<Fist14>> Edition");
srand(int(time(0)));
sf::Clock clock;
window.setFramerateLimit(900);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
sf::Time time = clock.getElapsedTime();
//cout << 1.0f/time.asSeconds() << endl;
clock.restart().asSeconds();
window.clear();
handler.update();
window.draw(handler);
window.display();
}
return 0;
}
Thanks
Stolle