1
Graphics / Sorry, it was my fault.
« on: August 11, 2011, 02:47:45 pm »
You didn't change too much, my function was bad. I have made a revised function that I have tesed in my actual game, so I know for a fact it works. It actually handles moving in the function. Here is the fuction:
You shouldn't have to change any code because it uses sf::Drawable which applies to all moveable objects. I would change your code to:
All you have to do is change 800 to 800 minus your sprites/shapes width and 600 to 600 minus your sprites/shapes height. Enjoy!
Code: [Select]
//smartMove(sf::Sprite&,int,int,int,int);
//usage: smartMove(sprite you are controlling, move x, move y, window width, window height);
//example: smartMove(Player,1,0,400,250)
//tip: subtract the sprite height and sprite width from the window height and window width
bool smartMove(sf::Drawable &obj,int moveX,int moveY,int windowWidth,int windowHeight) {
sf::Vector2f pos = obj.GetPosition();
sf::Vector2f scl = obj.GetScale();
//position from sprites base point
int posX = pos.x;
int posY = pos.y;
if (moveX > 0 || moveY > 0) {
if (posX + moveX >= windowWidth|| posY + moveY >= windowHeight) {
//Good luck moving there, YOU REBEL!!
return false;
}
}
if (moveX < 0 || moveY < 0) {
if (posX - moveX <= 0 || posY - moveY <= 0) {
//You want to move your sprite there? NO!!!
return false;
}
}
if (moveX == 0 && moveY == 0)
return false;
//if you want to handle moving in the function
obj.Move(moveX,moveY);
//go ahead, Mr.Sprite
return true;
}
You shouldn't have to change any code because it uses sf::Drawable which applies to all moveable objects. I would change your code to:
Code: [Select]
if (LeftKeyDown)
smartMove(blob,-KeyMove,800,600);
if (RightKeyDown)
smartMove(KeyMove, 0,800,600);
if (UpKeyDown)
smartMove(0, -KeyMove,800,600);
if (DownKeyDown)
smartMove((0, KeyMove,800,600);
if (GKeyDown)
Blob.Rotate(KeyMove);
if (HKeyDown)
Blob.Rotate(-KeyMove);
}
All you have to do is change 800 to 800 minus your sprites/shapes width and 600 to 600 minus your sprites/shapes height. Enjoy!