This is a part of my pong game (the function that controls the movement of the paddle).
Initially I had it operating using the vkup & vk_down arrows. Everything worked correctly. Now I would like to add an additional feature that allows the user to also use the mouse POS to control the paddle.
Here is my function:
PHP Code:
void movePaddle()
{
POINT myMouse;
//NOTE 0x8000 - Determines that the key is pressed instantly (realtime)
//So there is no delay
/////////////////////////////////// Use Keyboard to control paddle
if(GetAsyncKeyState(VK_UP) & 0x8000)
{
if(player_t.position.top > 0)
{
player_t.position.top += ((player_t.myPos.y - 1) - paddleSpeed);
player_t.position.bottom += ((player_t.myPos.y - 1) - paddleSpeed);
}
}
//Move paddle down
else if(GetAsyncKeyState(VK_DOWN)& 0x8000)
{
if(player_t.position.bottom < screenHeight)
{
player_t.position.top -= ((player_t.myPos.y - 1) - paddleSpeed);
player_t.position.bottom -= ((player_t.myPos.y - 1) - paddleSpeed);
}
}
/////////////////////////////////// Use mouse to control paddle
////Move paddle up
if(GetAsyncKeyState(VK_LBUTTON))
{
if(player_t.position.top > 0 && GetCursorPos(&myMouse) < player_t.position.top)
{
player_t.position.top += ((player_t.myPos.y - 1));
player_t.position.bottom += ((player_t.myPos.y - 1));
}
}
//Move paddle down
else if(GetAsyncKeyState(VK_LBUTTON))
{
if(player_t.position.bottom < screenHeight && GetCursorPos(&myMouse) < player_t.position.bottom)
{
player_t.position.top -= ((-player_t.myPos.y - 1));
player_t.position.bottom -= ((-player_t.myPos.y - 1));
}
}
}
Currently the keyboard still works fine, however the mouse ONLY controls the paddle upwards. if I comment out the first if() statement for the mouse, then the paddle moves downward fine so I know my code is correct, however if I uncomment and I have both statements available (if() & else if()) it breaks it and it only moves upwards. I tried combining the 2 into 1 and it still doesn't work.
PHP Code:
if(GetAsyncKeyState(VK_LBUTTON))
{
if(player_t.position.top > 0 && GetCursorPos(&myMouse) < player_t.position.top)
{
player_t.position.top += ((player_t.myPos.y - 1));
player_t.position.bottom += ((player_t.myPos.y - 1));
}
else if(player_t.position.bottom < screenHeight && GetCursorPos(&myMouse) < player_t.position.bottom)
{
player_t.position.top -= ((-player_t.myPos.y - 1));
player_t.position.bottom -= ((-player_t.myPos.y - 1));
}
}
Any ideas?? I'm pretty sure my code is correct.