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

Author Topic: Sprite not moving  (Read 1600 times)

0 Members and 1 Guest are viewing this topic.

GroundZero

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Sprite not moving
« on: July 10, 2012, 11:04:58 pm »
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

Code: [Select]
clockKeyPress.restart()
it goes all wrong... sometimes it displays 2 times, sometimes only 1.
Numbers are also different each time... not sure whats wrong...
« Last Edit: July 11, 2012, 12:09:14 am by GroundZero »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Sprite not moving
« Reply #1 on: July 11, 2012, 12:05:29 pm »
Is it your intention to call clockKeyPress.restart(); twice? Normally what I do in my game loops is get the delta frame time once at the beginning of the loop and use that for the rest of the loop. I also make sure to restart the clock right after I get it's elapsed value so I don't "miss" any of the time I spend doing stuff between when I get the time and restart the clock. If you restart the clock at the end of the frame and ask for the delta at the beginning of the next, you will get a value that is very close to 0. If you use that value to move your level every time a key is pressed it is likely you won't notice many changes.

You might also want to query the key states every frame instead of polling for key events. With key event polling SFML only tells you when it is pressed and released. That way you yourself need to remember the state of each key every frame and move the level every frame based on that. Now you are moving once per press and release of each key for a very small amount due to the delta being small and thus you don't see much happening.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).