Hi, I can make the sprite character move on the with out any problem with out using any classes or headers, however when ever I try make use of .h files my sprite resets back to the original position. it just wont save the curent location, always resets it to the original coordinates.
i know its really badly written and there some exces staff on it. Please any one help, I pretty sure it is something stupid that i am missing
player.h
#include <SFML/Graphics.hpp>
#include <iostream>
#include "animation.h"
class Player
{
public:
void loadPlayer();
void playerMovement(sf::RenderWindow &window);
void drawPlayer(sf:: RenderWindow &window);
int inc(sf::RenderWindow &window){
c=100;
if(window.GetInput().IsKeyDown(sf::Key::C)) {c++;
std::cout<<"c:"<<c<<std::endl;}
}
~Player(){
c--;
std::cout<<"c:"<<c<<std::endl;
}
protected:
Animation playerAnimation;
//playerAnimation.isActive();
private:
sf::Image playerImage;
sf::Sprite playerSprite;
float VelocityX,velocityY;
float coordinateX, coordinateY, moveSpeed;
int sourceX, sourceY;
static int c;
};
player.cpp
#include "player.h"
void Player::loadPlayer(){
if(playerImage.LoadFromFile("player.png")){
playerSprite.SetImage(playerImage);}
}
int Player::c=0;
void Player::playerMovement(sf::RenderWindow &window){
int sDown=0;
int sLeft=48;
int sUp=144;
int sRight=96;
coordinateX=0,coordinateY=0;
coordinateX=10, coordinateY=10;
sourceX=0, sourceY=0;
moveSpeed=7;
if (window.GetInput().IsKeyDown(sf::Key::Right)){
sourceY=sRight;
coordinateX+=moveSpeed*window.GetFrameTime();//needs work
}
else if (window.GetInput().IsKeyDown(sf::Key::Left)){
sourceY=sLeft;
coordinateX=-moveSpeed;
}
else coordinateX=0;
if (window.GetInput().IsKeyDown(sf::Key::Down)){
sourceY=sDown;
coordinateY=moveSpeed;
}
else if (window.GetInput().IsKeyDown(sf::Key::Up)){
sourceY=sUp;
coordinateY=-moveSpeed;
}
else coordinateY=0;
playerAnimation.setPosition(1, coordinateX);
playerAnimation.setPosition(2, coordinateY);
coordinateX+=coordinateX;
coordinateY+=coordinateY;
if (coordinateX!=0||coordinateY!=0)
sourceX+=playerImage.GetWidth()/3;
else sourceX=0;
if (sourceX==playerImage.GetWidth()) sourceX=0;
playerSprite.SetSubRect(sf::IntRect(sourceX,sourceY,sourceX+playerImage.GetWidth()/3,sourceY+playerImage.GetHeight()/4));
playerSprite.SetPosition(coordinateX,coordinateY);
window.Clear(sf::Color(c,255,255));
window.Draw(playerSprite);
std::cout<<"velocityX:"<<coordinateX<<std::endl;
}
void Player::drawPlayer(sf:: RenderWindow &window){
window.Draw(playerSprite);
}
animation.h
#include <SFML/Graphics.hpp>
class Animation
{
public:
void setPosition(int axis, float value);
bool isActive(bool active);
private:
float coordinateX,coordinateY;
};
animation.cpp
#include "animation.h"
bool Animation::isActive(bool active){
active=true;
}
void Animation::setPosition(int axis, float value){
if(axis==1)
value=coordinateX;
else
value=coordinateY;
this->coordinateX=coordinateY;
this->coordinateY=coordinateY;
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Graphics");
Player mainCharecter;
mainCharecter.loadPlayer();
int i=1;
bool active=true;
/*
float velocityX=0,velocityY=0;
float x=10, y=10;
int sourceX=0, sourceY=0;
*/
while (active==true)
{
sf::Event event;
while (window.GetEvent(event))
{
if (event.Type == sf::Event::Closed||event.Key.Code==sf::Key::Escape)
active=false;
}
mainCharecter.playerMovement(window);
window.SetFramerateLimit(10);
window.Display();
}
return EXIT_SUCCESS;
}