Hi everyone,
I'm fairly new to 2D gaming in C# but here goes...
I have a pong game created where the ball always bounces off the bats at correct angles. I want to incorporate some control of how the ball bounces. For example, if the ball's Y velocity is negative (the ball is moving UPWARDS) AND the bat on either side is moving DOWNWARDS, I want the ball to bounce back where it came from (reverse both x and y velocities).
My code is as follows:
if ((Velocity.X < 0 && collision(_player1)) || (Velocity.X > 0 && collision(_player2)))
{
Velocity.X = -Velocity.X;
//The above code works fine, the below code to reverse the ball is not working.
//I believe the code itself is correct, but the 'bools' are not being updated
//If I add '_isUpKeyPressed = true;' then it works, but I want it updated from parent class
if (_isUpKeyPressed && Velocity.Y > 0 || _isDownKeyPressed && Velocity.Y < 0)
{
Velocity.Y = -Velocity.Y;
}
}
The above code is in the Ball class (Ball.cs file) which is inherited from the parent class in ControlSprite.cs. The below code is in ControlSprite.cs.
public bool _isUpKeyPressed;
public bool _isDownKeyPressed;
public bool _isRightKeyPressed;
public bool _isLeftKeyPressed;
public void KeyDown(Keys keys)
{
if (keys == _upKey)
{
_isUpKeyPressed = true;
}
//etc etc
}
The bool values become true when you hold down the correct key to move the bat. Any ideas of how to update the bool values in the inherited class?
Thanks for any help!
Loading
Sam HobbsPosted Nov 14, 2010, 11:58 PM
BobPosted Nov 14, 2010, 11:51 PM
Thanks again.
Sam HobbsPosted Nov 14, 2010, 11:46 PM
BobPosted Nov 14, 2010, 11:25 PM
Inherited Class = Ball
I have declared the bool values in the parent class and made them public. I want to use them in the inherited class but they do not seem to update properly. When the KeyDown event is triggered in the parent class, the particular bool value is true. When the KeyUp event is triggered in the parent class, that bool value becomes false. When the key is down and the bool is true, I want the Update method in the inherited class to recognize this and apply my conditional code.
Thanks for the help.
Sam HobbsPosted Nov 14, 2010, 11:21 PM