So i did and update, now i have
Player.h
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cstdlib>
#include <ctime>
#pragma once;
class Player{
public:
int health, intelect, strenght, agility, mana,id;
sf::Texture ptexture;
sf::Sprite pimage;
Player(int a,int b,int c,int d,int e,int g) : health(a),intelect(b),strenght(c),agility(d),mana(e),id(g){
//loading sprite
if(!ptexture.loadFromFile("Player.png"))
std::cout << "Could not open the img from file!" << std::endl;
pimage.setTexture(ptexture);
};
float movement(Player* p, float frameCounter, float switchFrame, float frameSpeed, sf::Clock clock);
//float frameUpdate(float frameCounter);
};
Player.cpp
#include "Player.h"
float Player::movement(Player* p, float frameCounter, float switchFrame, float frameSpeed, sf::Clock clock){
enum Direction{Left,Right};
sf::Vector2i source(1,Right);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
source.y = Right;
p->pimage.move(2,0); //(x,y)
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
source.y = Left;
p->pimage.move(-2,0); //(x,y)
}
frameCounter += frameSpeed * clock.restart().asSeconds();
std::cout << "FRAME " << frameCounter << std::endl;
if(frameCounter >= switchFrame)
{
std::cout << "FRAME COUNTER INSIDE IF" << frameCounter << std::endl;
frameCounter = 0;
std::cout << "FRAME COUNTER INSIDE IF AFTER 0" << frameCounter << std::endl;
source.x++;
if(source.x * 32 >= p->ptexture.getSize().x)
source.x = 0;
}
//should crop the right image
p->pimage.setTextureRect(sf::IntRect(source.x * 32, source.y*32,32,32));
return frameCounter;
}
main.cpp
#include "Player.h"
int main(){
sf::Vector2f screenDimensions(800,600);
sf::RenderWindow Window(sf::VideoMode(screenDimensions.x,screenDimensions.y),"Magic Cataclysm");
Window.setFramerateLimit(30);
Player* player1 = new Player(1,2,3,4,5,6);
player1 -> pimage.setPosition(0,536);
/*this i changed*/
float frameCounter = 0, switchFrame = 100, frameSpeed = 500;
sf::Clock clock;
//program works as long as window is open
while(Window.isOpen()){
sf::Event Event;
while(Window.pollEvent(Event)){
switch(Event.type){
//when the player click close, program terminates
case sf::Event::Closed:
Window.close();
break;
}
}
/*this is what i changed*/
frameCounter = player1->movement(player1, frameCounter, switchFrame, frameSpeed, clock);
std::cout << "FRAME COUNTER OUTSIDE " << frameCounter << std::endl;
//render the player
Window.draw(player1->pimage);
Window.display();
Window.clear();
}
return 0;
}
i commented the stuff i changed, now my method returns frameCounter
float frameCounter = 0, switchFrame = 100, frameSpeed = 500;
I initialized it outside the loop and outside the movement method, so the frameCounter is not 0 all the time. It updates now, but, animation still does not work properly. I did some console check
It appears that my method does not return frameCounter properly but the cout outside the method shows frameCounter = 0, but it still goes infinity inside this function.
When i run CME tutorial code it gives me this
FRAME is frameCounter before if statement
FRAME2 is frameCounter after "if" statement
This is the tutorial code
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
//#include <cstdlib>
//#include <ctime>
int main(){
enum Direction {Down,Left,Right,Up};
sf::Vector2i source(1,Down);
sf::Vector2i screenDimensions(800,600);
sf::Vector2i blockDimensions(10,10);
float frameCounter = 0, switchFrame = 100, frameSpeed = 500;
sf::RenderWindow Window(sf::VideoMode(screenDimensions.x,screenDimensions.y),"My First Game");
Window.setFramerateLimit(30);
sf::Texture pTexture;
sf::Sprite playerImage;
sf::Clock clock;
bool updateFrame = true;
if(!pTexture.loadFromFile("Player.png"))
std::cout <<"Error could not load player image" <<std::endl;
playerImage.setTexture(pTexture);
while(Window.isOpen()){
sf::Event Event;
while(Window.pollEvent(Event)){
switch(Event.type){
case sf::Event::Closed:
Window.close();
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
source.y = Up;
playerImage.move(0,-2); //(x,y)
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
source.y = Down;
playerImage.move(0,2); //(x,y)
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
source.y = Right;
playerImage.move(2,0); //(x,y)
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
source.y = Left;
playerImage.move(-2,0); //(x,y)
//kalwiatura
}
frameCounter += frameSpeed * clock.restart().asSeconds();
std::cout << "FRAME " << frameCounter << std::endl;
if(frameCounter >= switchFrame)
{
frameCounter = 0;
source.x++;
if(source.x * 32 >= pTexture.getSize().x)
source.x = 0;
}
std::cout << "FRAME2 " << frameCounter << std::endl;
playerImage.setTextureRect(sf::IntRect(source.x * 32, source.y*32,32,32));
Window.draw(playerImage);
Window.display();
Window.clear();
}
return 0;
}
and Player.png attachment from CME tutorial
I think my code does the same, but inside method, but results aren't the same
[attachment deleted by admin]