---FIXED---
Hey guys, i've just coded myself a very simple button system where you can change colours of a block, now it works but not 100%, if I hold my mouse over my block which is 40 by 40, and hold left click, it will add maybe 5 or 10 to red, but then stop, even if the mouse is still in collision with the block and im holding left click, what I would like it to do is just keeping adding red until I let go or the mouse isnt in collision with that block anymore.
here is my code for the colour adding
if(iEvent.type == sf::Event::MouseButtonPressed == true && iEvent.mouseButton.button == sf::Mouse::Left){leftMouseButton = true;}else{leftMouseButton = false;}
if(leftMouseButton == true)
{
for(int i=0;i<menuBtns.size();i++)
{
if(math.pointInRect(sf::Mouse::getPosition(screen).x,sf::Mouse::getPosition(screen).y,int(menuBtns.at(i).pos.x),int(menuBtns.at(i).pos.y),menuBtns.at(i).pos.w,menuBtns.at(i).pos.h) == true)
{
switch(menuBtns.at(i).rtnID())
{
case 1: if(clickTimer == 0){clickTimer = 1; player.setHairId(player.rtnHairId()+1);}
break;
case 2: if(clickTimer == 0){clickTimer = 1; player.setHairId(player.rtnHairId()-1);}
break;
case 3: player.setColor('r',1); cout << "Red +1" << endl;
break;
case 4: player.setColor('r',-1); cout << "Red -1" << endl;
break;
case 5: player.setColor('b',1); cout << "Blue +1" << endl;
break;
case 6: player.setColor('b',-1); cout << "Blue -1" << endl;
break;
case 7: player.setColor('g',1); cout << "Green +1" << endl;
break;
case 8: player.setColor('g',-1); cout << "Green -1" << endl;
break;
}
}
}
}
the math.pointInRect is this piece of code
bool pointInRect(int pnt_x, int pnt_y, int rect_x, int rect_y, int rect_w, int rect_h)
{
if ( (pnt_x >= rect_x) && (pnt_x <= rect_x + rect_w - 1) )
{
if ( (pnt_y >= rect_y) && (pnt_y <= rect_y + rect_h - 1) )
{return true;}
}
return false;
}
any reasons why it doesnt keep just adding?
ok for my code I have just changed this
if(math.rectTorectCollision(sf::Mouse::getPosition(screen).x,sf::Mouse::getPosition(screen).y,1,1,int(menuBtns.at(i).pos.x),int(menuBtns.at(i).pos.y),menuBtns.at(i).pos.w,menuBtns.at(i).pos.h) == true)
now it uses my rectTorectCollision which is this
bool rectTorectCollision(int rect1_x, int rect1_y, int rect1_w, int rect1_h,
int rect2_x, int rect2_y, int rect2_w, int rect2_h)
{
if ( pointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
if ( pointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
if ( pointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
if ( pointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
if ( pointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
if ( pointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
if ( pointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
if ( pointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
return false;
}
now if i hold left click it will keep adding red but if i move it will stop, how can i get it to keep going?
lol once again i have created a useless topic because i have fixed it once again...
in my rectorectcollision i put this at the top of my code
if(iEvent.type == sf::Event::MouseButtonPressed == true && iEvent.mouseButton.button == sf::Mouse::Left){leftMouseButton = true;}
if(iEvent.type == sf::Event::MouseButtonReleased == true && iEvent.mouseButton.button == sf::Mouse::Left){leftMouseButton = false;}
it now works fine, it seems that when you press left click, after about a second it will just go back to 0, it seems the framerate would reset it, so now i just put another if statement and it works.