Hi
i want to put the player in the middle of the screen but i don't know
can someone tell me how to do it?
also when i move it to a side and then press another side its legs stop moving(fixed thanks for Ixrec)
sorry for my english
#ifdef SFML_STATIC
#pragma comment(lib, "glew.lib")
#pragma comment(lib, "freetype.lib")
#pragma comment(lib, "jpeg.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "gdi32.lib")
#endif // SFML_STATIC
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#pragma region Variables
sf::RenderWindow window;
sf::Texture Map;
sf::Sprite sMap;
sf::Clock MyClock;
sf::Event MyEvent;
sf::Vector2f position(800, 600);
sf::Texture Image;
sf::Sprite player;
sf::View MyView;
sf::Vector2i Place(1, 0);
sf::Vector2f MapPosition(position.x/2, position.y/2);
enum Direction{Down, Left, Right, Up};
float FrameSpeed = 500, SwitchFrame = 60, FrameCount = 0;
bool updateFrame = false;
#pragma endregion sf
int main()
{
//Window
window.create(sf::VideoMode(position.x, position.y), "My First SFML Game", sf::Style::Close);
window.setPosition(sf::Vector2i(300, 50));
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
//Texture Map
if (!Map.loadFromFile("LOF-Map.png"))
std::cout << "Couldn't Load The Map" << std::endl;
//Texture player
if (!Image.loadFromFile("Player.png"))
std::cout << "Couldn't load the Image" << std::endl;
Image.setSmooth(true);
//Sprite Map
sMap.setTexture(Map);
//Sprite Player
player.setTexture(Image);
player.setPosition(sf::Vector2f(100, 100));
//Game Loop
while (window.isOpen())
{
//Event loop
while (window.pollEvent(MyEvent))
{
place.x = 1;
switch (MyEvent.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
updateFrame = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
updateFrame = true;
Place.y = Down;
player.move(0, 1);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
updateFrame = true;
Place.y = Left;
player.move(-1, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
updateFrame = true;
Place.y = Right;
player.move(1, 0);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
updateFrame = true;
Place.y = Up;
player.move(0, -1);
}
FrameCount += FrameSpeed * MyClock.restart().asSeconds();
if (updateFrame == true){
if (FrameCount > SwitchFrame)
{
FrameCount = 0;
Place.x++;
if (Place.x * 32 >= Image.getSize().x)
Place.x = 0;
}
}
if (player.getPosition().x<= 0)
player.setPosition(sf::Vector2f(0,player.getPosition().y));
if (player.getPosition().y < 0)
player.setPosition(sf::Vector2f(player.getPosition().x, 0));
//Display a block of the SpriteSheet
player.setTextureRect(sf::IntRect(Place.x * 32, Place.y * 32, 32, 32));
window.clear();
window.setView(MyView);
window.draw(sMap);
window.draw(player);
window.display();
}
return EXIT_SUCCESS;
}