Ok, so I'm having major problems with creating and updating a vector of sprites that are meant to represent projectiles. Everything compiles fine, but my sprite, or should I say sprite, has other plans it seems...
The fireball is meant to be moving across the screen, but it doesn't.
All relevant code, will compile to the above exe.:
Entity.h
#pragma once
#include "SFML/Graphics.hpp"
#include <Windows.h>
class Entity : public sf::Drawable, public sf::Transformable
{
public:
void createTexture(const sf::Image& imageRef, int xTexturePosition, int yTexturePosition, int xTextureSize, int yTextureSize);
void createSprite(int width, int height, int xPosition, int yPosition, float scale);
protected:
sf::Texture texture;
sf::Sprite sprite;
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
target.draw(sprite, states);
};
};
Entity.cpp
#include "Entity.h"
void Entity::createTexture(const sf::Image& imageRef, int xTexturePosition, int yTexturePosition, int xTextureSize, int yTextureSize)
{
texture.loadFromImage(imageRef, sf::IntRect((xTexturePosition * xTextureSize), (yTexturePosition * yTextureSize), xTextureSize, yTextureSize));
}
void Entity::createSprite(int width, int height, int xPosition, int yPosition, float scale)
{
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, width, height));
sprite.setOrigin((width / 2), (height / 2));
sprite.setScale(scale, scale);
sprite.setPosition(xPosition, yPosition);
}
Fireball.h
#pragma once
#include "Entity.h"
#include <vector>
class Fireball : public Entity
{
public:
void createFireball(sf::Keyboard& keyboard);
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
for(unsigned int i = 0; i < fireball.size(); i++)
{
target.draw(fireball[i], states);
}
};
std::vector <sf::Sprite> fireball; // A vector of sprites called fireball.
};
Fireball.cpp
#include "Fireball.h"
void Fireball::createFireball(sf::Keyboard& keyboard)
{
if( keyboard.isKeyPressed(keyboard.Space))
{
fireball.resize(fireball.size() + 1);
}
for (unsigned int i = 0; i < fireball.size(); i++)
{
fireball[i] = sprite; // Gives the fireball at [i] the properties of the protected sprite in Entity.
fireball[i].move(1,1); // This seems to do nothing.
if(fireball[i].getPosition().x > 700)
{
fireball.erase(fireball.begin() + i);
}
fireball.shrink_to_fit();
}
}
Game.h
#pragma once
#include "SFML/Graphics.hpp"
class Game
{
public:
Game();
void runGame();
private:
int screenHeight;
int screenWidth;
sf::Clock clock;
long float updateRate;
};
Game.cpp
#include "Game.h"
#include "Fireball.h"
Game::Game()
{
screenWidth = 1600;
screenHeight = screenWidth / 16 * 9;
}
void Game::runGame()
{
sf::Keyboard keyboard;
sf::Keyboard& keyboardRef = keyboard;
sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "Y U NO WORK");
sf::RenderWindow& windowRef = window;
sf::Image spriteSheet;
spriteSheet.loadFromFile("Main sprite sheet.png");
sf::Image& imageRef = spriteSheet;
Fireball fb;
fb.createTexture(imageRef, 0, 8, 16, 16);
fb.createSprite(16, 16, 150, 150, 4);
long float updateRate = 1000.0f / 60.0f;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || keyboard.isKeyPressed(keyboard.Escape))
window.close();
}
window.clear(sf::Color(0,0,0));
if(clock.getElapsedTime().asMilliseconds() >= updateRate)
{
clock.restart();
// * * * * * * * * * * * * * * * * Game logic goes here
fb.createFireball(keyboardRef);
window.draw(fb);
window.display();
}
}
}
int main()
{
Game game;
game.runGame();
return 0;
}
I've been stuck on this problem for what seems like an age. I think the problem is something to do with the draw function that is being used. Is window.draw(fb) using the draw function from Entity or the one in Fireball?
Also, shouldn't the fireball
.move() in Fireball be doing something? The sprite just sits there.
I'm no C++ pro so plz help a noob out.