Evening all,
I am having trouble getting my sprites to move. I know my code is not optimal, but it is about getting my sprites to move at the moment. I am still learning this stuff ^^
I coded it all, but it just wont move, no matter what I do. Hopefully someone can tell me why my sprites wont move.
My code:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <string>
#include <SFML/Graphics.hpp>
#include <level.h>
// global
std::string root = "C:\\Users\\Angelo\\Desktop\\Projecten\\Tiny Little Game\\untitled-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug\\debug\\";
bool pause = false;
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "Crystallibrium", sf::Style::Close);
window.setVerticalSyncEnabled(true);
// set a clock
sf::Clock clockKeyPress;
// Create the level
level level(1);
level.setLevel();
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
if(event.type == sf::Event::LostFocus)
window.setTitle("Crystallibrium - AFK");
if(event.type == sf::Event::GainedFocus)
window.setTitle("Crystallibrium");
sf::Time Time = clockKeyPress.getElapsedTime();
float elapsed = Time.asMilliseconds();
if(elapsed > 0)
{
// check if a key is pressed
if(event.type == sf::Event::KeyPressed)
{
// left arrow key
if(event.key.code == sf::Keyboard::Left)
{
level.moveLevel('L', elapsed);
}
// right arrow key
if(event.key.code == sf::Keyboard::Right)
{
level.moveLevel('R', elapsed);
}
// right up key
if(event.key.code == sf::Keyboard::Up)
{
level.moveLevel('U', elapsed);
}
// right down key
if(event.key.code == sf::Keyboard::Down)
{
level.moveLevel('D', elapsed);
}
}
clockKeyPress.restart();
}
}
// Clear screen
window.clear();
// Display the level
level.drawLevel(window);
// Update the window
window.display();
// Restart clock
clockKeyPress.restart();
}
return EXIT_SUCCESS;
}
level::level(int levelNr) : levelNr(levelNr){}
void level::setLevel(){
// create the level
std::vector<std::string> list;
list.push_back(root + "background.png");
xPos.push_back(0);
yPos.push_back(0);
list.push_back(root + "objects_1.png");
xPos.push_back(40);
yPos.push_back(470);
list.push_back(root + "objects_2.png");
xPos.push_back(405);
yPos.push_back(390);
list.push_back(root + "moon_1.png");
xPos.push_back(100);
yPos.push_back(80);
list.push_back(root + "moon_2.png");
xPos.push_back(47);
yPos.push_back(173);
list.push_back(root + "moon_3.png");
xPos.push_back(78);
yPos.push_back(260);
list.push_back(root + "floor.png");
xPos.push_back(0);
yPos.push_back(520);
list.push_back(root + "platform.png");
xPos.push_back(400);
yPos.push_back(460);
// enter each entry in list into the texture array
for(std::vector<std::string>::iterator pos = list.begin(); pos != list.end(); pos++)
{
sf::Texture texture;
texture.loadFromFile(*pos);
textures.push_back(texture);
}
// create a sprite from each texture but first start a counter
int c = 0;
for(std::vector<sf::Texture>::iterator pos = textures.begin(); pos != textures.end(); pos++)
{
sf::Sprite sprite(*pos);
sprite.setPosition(xPos[c], yPos[c]);
sprites.push_back(sprite);
c++;
}
}
void level::initMove(sf::Sprite &sprite, char direction, float elapsed){
if(direction == 'L')
{
sf::Vector2f pos = sprite.getPosition();
sprite.move(0.5 * elapsed, 0);
}
if(direction == 'R')
{
sf::Vector2f pos = sprite.getPosition();
sprite.move(0.5 * elapsed, 0);
}
if(direction == 'U')
std::cout << "MOVE UP *not implemented yet*\n";
if(direction == 'D')
std::cout << "MOVE DOWN *not implemented yet*\n";
}
void level::moveLevel(char direction, float elapsed){
switch(direction)
{
case 'L':
for(std::vector<sf::Sprite>::iterator pos = sprites.begin(); pos != sprites.end(); pos++)
{
initMove(*pos, 'L', elapsed);
}
break;
case 'R':
for(std::vector<sf::Sprite>::iterator pos = sprites.begin(); pos != sprites.end(); pos++)
{
initMove(*pos, 'R', elapsed);
}
break;
case 'U':
for(std::vector<sf::Sprite>::iterator pos = sprites.begin(); pos != sprites.end(); pos++)
{
initMove(*pos, 'U', elapsed);
}
break;
case 'D':
for(std::vector<sf::Sprite>::iterator pos = sprites.begin(); pos != sprites.end(); pos++)
{
initMove(*pos, 'D', elapsed);
}
break;
}
}
void level::drawLevel(sf::RenderWindow &win){
// display sprites
for(std::vector<sf::Sprite>::iterator pos = sprites.begin(); pos != sprites.end(); pos++)
{
win.draw(*pos);
}
}
My header file:
#ifndef LEVEL_H
#define LEVEL_H
class level {
private:
int levelNr;
std::vector<sf::Texture> textures;
std::vector<sf::Sprite> sprites;
std::vector<std::string> textureList;
std::vector<int> xPos;
std::vector<int> yPos;
public:
// def. constructor
level(int levelNr);
// functions
void setLevel();
void initMove(sf::Sprite &sprite, char direction, float elapsed);
void moveLevel(char direction, float elapsed);
void drawLevel(sf::RenderWindow &win);
};
#endif // LEVEL_H
Update:It runs fine, but as soon as I add a
clockKeyPress.restart()
it goes all wrong... sometimes it displays 2 times, sometimes only 1.
Numbers are also different each time... not sure whats wrong...