Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - howderek

Pages: [1]
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:
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!

2
Graphics / I would do it differently
« on: August 05, 2011, 10:22:23 pm »
I would do that by checking each movement to the edge. I would create a function called locationAllowed like below:
Code: [Select]

bool locationAllowed(sf::Shape &shape) {
sf::Vector2f temp = shape.GetPosition();
int x = temp.x + 1;
int y = temp.y + 1;
if (x > 800 || x < 0) {
   return false;
}
if (y > 600 || y < 0) {
   return false;
}
return true;
}

That code is tested and functional. Then I would rewrite your if statements:
Code: [Select]

         if (LeftKeyDown  && locationAllowed(Blob))
            Blob.Move(-KeyMove, 0);
         if (RightKeyDown && locationAllowed(Blob))
            Blob.Move(KeyMove, 0);
         if (UpKeyDown  && locationAllowed(Blob))
            Blob.Move(0, -KeyMove);
         if (DownKeyDown  && locationAllowed(Blob))
            Blob.Move(0, KeyMove);
         if (GKeyDown  && locationAllowed(Blob))
            Blob.Rotate(KeyMove);
         if (HKeyDown  && locationAllowed(Blob))
            Blob.Rotate(-KeyMove);


I hope this helps you!

Pages: [1]