Good Afternoon,
I have experience in C++, however, I am very new with SFML. I managed to get a sprite on the screen, with the ability to not only move, but to go through the proper animations well in movement. My issue though, is the sprite itself isn't move too fast, but the animation of his movement is moving way to quickly. If that makes sense. Provided below is my code. I apologize if I put this up incorrectly, first time posting on here. Normally I am able to figure things out on my own, but I cannot seem to figure this one out.
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
int main()
{
int rLinkX = 0;
int rLinkY = 3;
int rIdle = 0;
int lLinkX = 0;
int lLinkY = 2;
int lIdle = 0;
int upLinkX = 0;
int upLinkY = 1;
int upIdle = 0;
int dwnLinkX = 0;
int dwnLinkY = 0;
int dwnIdle = 0;
sf::RenderWindow window(sf::VideoMode(1024, 512), "Welcome to SDVA 203!");
sf::CircleShape shape(75.f);
shape.setPosition(200, 200);
window.setMouseCursorVisible(false);
sf::Texture texture;
if (!texture.loadFromFile("Content/Crosshair.png"))
{
cout << "Could not find image." << endl; // error...
}
shape.setTexture(&texture);
sf::Sprite sprite;
sf::Texture linkTexture;
if (!linkTexture.loadFromFile("Content/zelda.png", sf::IntRect(0 * 40, 0 * 40, 40, 40)))
{
cout << "Could not find image." << endl; // error...
}
sprite.setTexture(linkTexture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
window.close();
}
if (event.key.code == sf::Keyboard::Left)
{
lIdle = 0;
rIdle = 0;
upIdle = 0;
dwnIdle = 0;
cout << "The Left Arrow Key Was Pressed" << endl;
sprite.setTextureRect(sf::IntRect(lLinkX * 40, lLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
sprite.move(-5, 0);
}
if (event.key.code == sf::Keyboard::Right)
{
lIdle = 0;
rIdle = 0;
upIdle = 0;
dwnIdle = 0;
cout << "The Right Arrow Key Was Pressed" << endl;
sprite.setTextureRect(sf::IntRect(rLinkX * 40, rLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
sprite.move(5, 0);
}
if (event.key.code == sf::Keyboard::Up)
{
lIdle = 0;
rIdle = 0;
upIdle = 0;
dwnIdle = 0;
cout << "The Up Arrow Key Was Pressed" << endl;
sprite.setTextureRect(sf::IntRect(upLinkX * 40, upLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
sprite.move(0, -5);
}
if (event.key.code == sf::Keyboard::Down)
{
lIdle = 0;
rIdle = 0;
upIdle = 0;
dwnIdle = 0;
cout << "The Down Arrow Key Was Pressed" << endl;
sprite.setTextureRect(sf::IntRect(dwnLinkX * 40, dwnLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
sprite.move(0, 5);
}
}
if (event.type == sf::Event::KeyReleased)
{
if (event.key.code == sf::Keyboard::Right)
{
rIdle = 1;
}
if (event.key.code == sf::Keyboard::Left)
{
lIdle = 1;
}
if (event.key.code == sf::Keyboard::Up)
{
upIdle = 1;
}
if (event.key.code == sf::Keyboard::Down)
{
dwnIdle = 1;
}
}
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Right)
{
std::cout << "the right button was pressed" << std::endl;
}
}
if (event.type == sf::Event::MouseWheelScrolled)
{
if (event.mouseWheelScroll.wheel == sf::Mouse::VerticalWheel)
std::cout << "wheel type: vertical" << std::endl;
std::cout << "wheel movement: " << event.mouseWheelScroll.delta << std::endl;
std::cout << "mouse x: " << event.mouseWheelScroll.x << std::endl;
std::cout << "mouse y: " << event.mouseWheelScroll.y << std::endl;
if (event.mouseWheelScroll.delta == 1)
{
shape.scale(1.8, 1.8);
}
else if (event.mouseWheelScroll.delta == -1)
{
shape.scale(.8, .8);
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
cout << "Left Arrow" << endl;
lLinkX++;
if (lLinkX >= 7)
{
lLinkX = 0;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
cout << "Right Arrow" << endl;
rLinkX++;
if (rLinkX >= 7)
{
rLinkX = 0;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
cout << "Left Arrow" << endl;
dwnLinkX++;
if (dwnLinkX >= 7)
{
dwnLinkX = 0;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
cout << "Right Arrow" << endl;
upLinkX++;
if (upLinkX >= 7)
{
upLinkX = 0;
}
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
shape.setPosition(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y);
}
if (rIdle == 1)
{
sprite.setTextureRect(sf::IntRect(rLinkX * 40, rLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
rLinkX++;
if (rLinkX >= 7)
{
rLinkX = 0;
}
}
if (lIdle == 1)
{
sprite.setTextureRect(sf::IntRect(lLinkX * 40, lLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
lLinkX++;
if (lLinkX >= 7)
{
lLinkX = 0;
}
}
if (upIdle == 1)
{
sprite.setTextureRect(sf::IntRect(upLinkX * 40, upLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
upLinkX++;
if (upLinkX >= 7)
{
upLinkX = 0;
}
}
if (dwnIdle == 1)
{
sprite.setTextureRect(sf::IntRect(dwnLinkX * 40, dwnLinkY * 40, 40, 40));
if (!linkTexture.loadFromFile("Content/zelda.png"))
{
// error...
}
sprite.setTexture(linkTexture);
dwnLinkX++;
if (dwnLinkX >= 7)
{
dwnLinkX = 0;
}
}
shape.setOrigin(75, 75);
window.clear();
window.draw(shape);
window.draw(sprite);
window.display();
}
return 0;
}