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

Author Topic: sprite.move makes sprite disappear  (Read 4621 times)

0 Members and 1 Guest are viewing this topic.

Deftwun

  • Newbie
  • *
  • Posts: 27
    • View Profile
sprite.move makes sprite disappear
« on: April 08, 2011, 02:55:19 am »
So i seem to be missing something with the sprite.move function. whenever i use the move function and then try to draw the sprite... nothing happens. If i use set position and then draw it appears no problem.

There are 2 classes; engine and object. Engine has the renderwindow, Object array element GameObject[0] has the sprite.

assume the sprite has already initially been drawn at its start location.

EVENT HANDLER
Code: [Select]

void Engine::EventHandler(){
    GameWindow.GetEvent(Event);
    if (Event.Type == sf::Event::KeyPressed){
        switch (Event.Key.Code) {

            case sf::Key::Right:
                GameObject[0].Move(6);
                break;
            case sf::Key::Return:
                GameObject[0].Sprite.SetPosition(200,200);
                break;
            case sf::Key::Space:
                GameWindow.Draw(GameObject[0].Sprite);
                GameWindow.Display();
                break;


OBJECT MOVE FUNCTION
Code: [Select]
bool Object::Move(char Direction){
    //direction 6 == right
    if (Direction == 6)    Sprite.Move(10,0);


}

[/b]

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
sprite.move makes sprite disappear
« Reply #1 on: April 08, 2011, 04:35:22 am »
Are you limiting your FPS? If not, the sprite may be moving faster than the eye can see.

Deftwun

  • Newbie
  • *
  • Posts: 27
    • View Profile
sprite.move makes sprite disappear
« Reply #2 on: April 09, 2011, 02:22:47 am »
Oh Yeah. Your right; I'm not at all, thats exactly whats happening. I'll have to look into that more and mess around with my  implementation.
(I'm new to c++ and sfml so I've just been playing with some basic stuff)

Thanks alot Wizzard!

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
sprite.move makes sprite disappear
« Reply #3 on: April 09, 2011, 09:48:42 am »
Use time-based movement rather than frame based movement.

I.e. get the amount of time passed since the last frame (there's a function in sf::Window for this) and multiply your values with that. Assuming your values are "move this amount per second".

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
sprite.move makes sprite disappear
« Reply #4 on: April 09, 2011, 12:18:15 pm »
But be careful with that, too. If FPS are dropping fairly low, the time difference is getting big. And for fast movements, the movement step can be really huge. At least huge enough to skip a wall, for example.

I'd go for time-based logics with a fixed maximum time (similar to the rendering loop, which mostly also has a fixed FPS).

Deftwun

  • Newbie
  • *
  • Posts: 27
    • View Profile
sprite.move makes sprite disappear
« Reply #5 on: April 11, 2011, 01:13:12 am »
Thanks alot for the input guys. I've now set a framerate limit on the window at 60 and am multiplying my movement rate by the frame time and all is working great.

 

anything