Guys, I'm making a 2d game. when I hover with the mouse over a piece and I click and hold the left button and then move the mouse, it starts dragging the piece, cool.
But when I start moving the mouse faster, my cursor drops the piece, wtf is that?
[attachment=32577:mousedrop.gif]
(the recording software I used is bugged, basically my cursor is in the center of the piece when I drag it, it's kind of slowed down here, don't pay attention to it.)
Here are my functions.
void App::handleEvents()
{
while( SDL_PollEvent( &event ) )
{
//If mouse event happened
if( event.type == SDL_MOUSEMOTION )
{
//Get mouse position
SDL_GetMouseState( &mouseX, &mouseY );
//If the mouse coordinates are somewhere over the piece, set to true.
if( mouseX > piece->getRect().x && mouseX < piece->getRect().x + piece->getRect().w &&
mouseY > piece->getRect().y && mouseY < piece->getRect().y + piece->getRect().h )
{
mouseOnPiece = true;
}
else
{
mouseOnPiece = false;
}
}
//If left button is clicked, set to true
if( event.type == SDL_MOUSEBUTTONDOWN )
{
if( event.button.button == SDL_BUTTON_LEFT )
{
leftMouseButton = true;
}
}
else if( event.type == SDL_MOUSEBUTTONUP )
{
if( event.button.button == SDL_BUTTON_LEFT )
{
leftMouseButton = false;
}
}
if( event.key.keysym.sym == SDLK_ESCAPE || event.type == SDL_QUIT )
{
running = false;
}
}
}
void App::handleLogic()
{
//If the mouse is over the piece and you hold the left button, then set the center of the piece to the cursor
if( mouseOnPiece == true && leftMouseButton == true )
{
piece->setRect( mouseX - TILE_EDGE / 2, mouseY - TILE_EDGE / 2 );
}
}