The screen is blank, no sprite shows up and I dunno what to do.
Main.cpp:
Animation.h:
Animation.cpp:
Player.h:
Player.cpp:
The screen is blank, no sprite shows up and I dunno what to do.
Main.cpp:
Animation.h:
Animation.cpp:
Player.h:
Player.cpp:
read over your rendering logic:
player.draw(); //draw the player
window.clear(); //clear what we've drawn
window.display(); //output what we drew, but since we've cleared it, their's nothing to display.
i'm not familiar with sfml's internal workings, but if it's like any other sane rendering api, then your problem is that you clear w/e you draw, before you display it.
I think something's wrong with the updating part, since the image of Coaked dude.png shows up (and obviously its the full pic not the cropped ones) when I commented out the player.Update(Window).
Problems
one:
Window.clear();
Clears whole buffer with black color
move the Window.clear() before all drawing or do all drawings after the function
Rest seams fine, your image is loaded and sfml has its own system to display to console if the image location is invalid
Also from glimpse of your code at Player::Update() when you don't move you set
playerAnimation.setActive(false); //if player not pressing any button
but when you move you don't set it to true
Two
This in "Animation.cpp" At line 99
You set the crop here as it seems.
spriteImage.setTextureRect(sf::IntRect(X, Y, Width, Height));
If there is problem with crop, check values your passing here.
Getting closer: it seems that the
from Animation.cpp, the spriteTexture.getSize().x returns value 0. So does the Y part. Anyone have any clue why it gives 0, instead of the actual size of the image, which is 96 by 128?
I even tried cast it to float but still
it seems that I have trouble with currentFrameX. All the varaibles in
spriteImage.setTextureRect(sf::IntRect(currentFrameX, currentFrameY * getFrameHeight(), currentFrameX + getFrameWidth(), currentFrameY * getFrameHeight() + getFrameHeight()));
Have the correct values, except for currentFrameX, with values of 1900000yadayada.
I highly suggest finding the mistake you self, you will learn to fix code / find errors and dozen other essential skills needed for programing.
Well, the code for changing frames is slightly convoluted.
How about using a single variable to mark the current frame:
unsigned int currentFrame = 0;
To change a frame you increment it, then check if we've reached the end.
currentFrame++;
if(currentFrame >= (numFramesWide * numFramesHigh))
{
currentFrame = 0;
}
To calculate the x,y image in the imagesheet:
imageX = (currentFrame % numFramesWide);
imageY = (currentFrame / numFramesWide);
To calculate the pixel location of the image:
pixelX = (imageX * ImageWidth);
pixelY = (imageY * imageHeight);
It's easier to just keep track of one variable (currentFrame), then to juggle multiple variables, especially when you can calculate the multiple variables from currentFrame and the constants.
Also the whole 'axis' thing with your functions, where the functions return a different variable with different meaning depending on what magic numbers you pass in is not a good programming idea unless there is a very good reason to do so.
I'm talking about this:
float setPosition(int axis, int value);
And this:
playerAnimation.setPosition(1,x);
playerAnimation.setPosition(2,y);
And why does setPosition() say it returns a float, but never does? It'll return gibberish, which you might accidentally be assigning to your currentFrameX somewhere.
setPosition() should take the x AND y positions, or else should have a separate function for each one.
setPositionX(int x);
setPositionY(int y);
getPositionX();
getPositionY();
Personally, I prefer using some kind of "Point" struct myself. SFML provides one for you if you don't want to use your own: sf::Vector2i (for ints) and sf::Vector2f (for floats).
Thanks for all the replies. Now I redo the codes and make a little change to the previous codes. The problem now is that there are more than one frame being drawn on the screen. I checked the x1 y1 and x2 y2 of the cropped image using sf::setTextureRect(..), and it shows the correct frames with corresponding input. But it also shows the frame next to the right of 'correct frames'.
Please help I've spent hours checking but failed to see the cause of this problem.
main.cpp
Animation.cpp
Player.cpp