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

Author Topic: subRect not moving with Sprite  (Read 1563 times)

0 Members and 1 Guest are viewing this topic.

Portalboat

  • Newbie
  • *
  • Posts: 9
    • View Profile
subRect not moving with Sprite
« on: February 01, 2011, 07:43:22 am »
I'm trying to make a pong game, but the paddle can always go offscreen. So, to fix this, I was planning on doing this:
 
Code: [Select]
sf::Sprite PaddlePlayer::Update(sf::RenderWindow &App)
{
ElapsedTime = App.GetFrameTime();
if(App.GetInput().IsKeyDown(sf::Key::Down))
{
if(!rectPaddle.Contains(0, 240))
{
Paddle.Move(0, 150*ElapsedTime);
}
else
{
Paddle.SetY(240);
std::cout << "Y set to 240\n";
}
}
if(App.GetInput().IsKeyDown(sf::Key::Up))
{
if(!rectPaddle.Contains(0, 0))
{
Paddle.Move(0, -150*ElapsedTime);
}
else
{
Paddle.SetY(0);
std::cout << "Y set to 0";
}
}
return Paddle;
}

(rectPaddle is the subRect of sprite Paddle, so rectPaddle = Paddle.GetSubRect)
However, based on my results (the paddle always jumps back up to the top, no matter where it is.) I can assume that rectPaddle doesn't move with the sprite. I tried using the Offset function to get it to move, but since that is an offset, it doesn't set exactly to the sprite's location, at least for multiple times.

So my question is, how would I get the subRect to always be moving with the sprite, or at least find some way to detect if the sprite is off the screen or not?