For the purpose of a moving character I think that using the delta time between the frames can be used for that.
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)==true)
movement.y -= 1 * delta_time );
Now you'll have to change it depending on how your implementation works but this is the most common thing to do. Though the other ways proposed have it's benefits as well. But this one works with least amount of code and least amount of trouble.
If I am unclear I can give you more hints on how to do this ;)
For the purpose of a moving character I think that using the delta time between the frames can be used for that.
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)==true)
movement.y -= 1 * delta_time );
Now you'll have to change it depending on how your implementation works but this is the most common thing to do. Though the other ways proposed have it's benefits as well. But this one works with least amount of code and least amount of trouble.
If I am unclear I can give you more hints on how to do this ;)
I'm having a bit of trouble thinking of how that actually works. I was thinking that I would start a clock, then I would have an if statement like so:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && clock.getElapsedTime > sf::Seconds(.25))
{
sprite.move(0, -14);
}
But when I do that, and run the program, the sprite doesn't even move. What's wrong with that?
Well if you want framerate depended movement then you'll have to do what Groogy proposed.
Something like:
sf::Sprite sprite;
sf::Vector2i direction(0, -1);
sf::Vector2f speed(0, 14);
sf::Clock clock;
while(window.isOpen())
{
sf::Time dt = clock.restart();
sf::Event event;
while(window.pollEvent(event))
{
if(even.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sprite.move(0, direction.y * speed.y * dt.asSeconds());
}
}
The sf::Clock will get restarted every frame thus, it will capture the time between two frames aka delta time or dt.
If you now take the velocity (speed) and multiply it by dt then you get the speed for that short time period and you don't apply the fully velocity for every frame. The direction variable is just a help to easily swap directions without having to change the actual velocity value.
It's adviced to use window.setFramerateLimit and use a fixed timestep (dt) to match the framerate (i.e. dt = 1/FPS).
Well if you want framerate depended movement then you'll have to do what Groogy proposed.
Something like:
sf::Sprite sprite;
sf::Vector2i direction(0, -1);
sf::Vector2f speed(0, 14);
sf::Clock clock;
while(window.isOpen())
{
sf::Time dt = clock.restart();
sf::Event event;
while(window.pollEvent(event))
{
if(even.type == sf::Event::Closed)
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sprite.move(0, direction.y * speed.y * dt.asSeconds());
}
}
The sf::Clock will get restarted every frame thus, it will capture the time between two frames aka delta time or dt.
If you now take the velocity (speed) and multiply it by dt then you get the speed for that short time period and you don't apply the fully velocity for every frame. The direction variable is just a help to easily swap directions without having to change the actual velocity value.
It's adviced to use window.setFramerateLimit and use a fixed timestep (dt) to match the framerate (i.e. dt = 1/FPS).
I'm not quite understanding what exactly this code is doing. For example, why are you multiplying the direction by the speed, and the time? What does that accomplish? Also, why do you have a vector "speed" , and multiplying that by -1. Why not just get rid of the vector and just have the sprite move -14 pixles. It would kill two birds with one stone.
Again, sorry if these are stupid questions, I simply want a better understanding of this.
sf::Rect< T >::Rect ( T rectLeft,
T rectTop,
T rectWidth,
T rectHeight
)
That's from the documentation. How do I pass in 4 cords with only 4 integers? Should I be passing in 4 vectors instead, because they store an x and a y cord? Also, I'm not understanding the first two params.
I guess because I would have done it with the cords of each corner, and I thought that was the approach they were trying to do as well.
Okay, I'm literally pulling my hair out right now, trying to figure out why this isn't working. If you guys could tell me what's wrong I would be so happy.
The image I'm using (zoomed):
http://i.imgur.com/jAc3D.png
Not zoomed:
http://i.imgur.com/JNRGT.png
The source:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
//Create window, and limit frame rate
sf::RenderWindow window (sf::VideoMode(800, 600, 32), "Game", sf::Style::Default);
window.setFramerateLimit(30);
//Declare image
sf::Texture texture;
//Load image
if(!texture.loadFromFile("Sprites/main.png"))
{
return 1;
}
//Creates and places the main sprite
sf::Sprite sprite;
sprite.setPosition(400, 300);
//Defines the rects that will be used to switch character view
sf::IntRect front(2, 2, 17, -22);
sf::IntRect back (22, 2, 16, 21);
sf::IntRect left (2, 27, 16, 23);
sf::IntRect right (23, 27, 16, 23);
//Starts clock
sf::Clock clock;
//Main window loop
while(window.isOpen())
{
sf::Event event;
sf::Time time = clock.restart();
while(window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
window.close();
}
if(event.key.code == sf::Keyboard::Insert)
{
sf::Image screenshot = window.capture();
screenshot.saveToFile("Screenshot.png");
}
}
//Movement
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
sprite.setTextureRect(front);
sprite.move(0, -14 * time.asSeconds());
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
sprite.setTextureRect(back);
sprite.move(0, 14 * time.asSeconds());
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
sprite.setTextureRect(right);
sprite.move(16 * time.asSeconds(), 0);
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
sprite.setTextureRect(left);
sprite.move(-16 * time.asSeconds(), 0);
}
//Draw sequence
window.clear(sf::Color(255, 0, 0)); //(Red, Green, Blue, (optional) Alpha) Alpha is transperency
//Draw....
window.draw(sprite);
window.display();
}
return 0;
}