Hi, i search a solution maybe someone can help. I have three Buttons for a Controller on mobile phone. They turn the Color when i touch them(2 changing Sprites). The Problem is that the Touch not directly React on the Sprite just next to it or just on the half Sprite, how can i change this. Here is my Code. They are 3 Files main.cpp, buttons.h. buttons.cpp:
I think Buttons.cpp is just important fot the problem:
using namespace sf;
using namespace std;
float btnScaleX = 3.0f;
float btnScaleY = 2.6f;
float btnPosX = 100;
float btnLeftPosY = 200;
float btnRightPosY = 500;
float btnFirePosY =1300;
Vector2f btnScaler (btnScaleX, btnScaleY);
Vector2f btnPosLeft(btnPosX, btnLeftPosY);
Vector2f btnPosRight(btnPosX, btnRightPosY);
Vector2f btnPosFire(btnPosX, btnFirePosY);
FloatRect btnRect1;
FloatRect btnRect2;
FloatRect btnRect3;
buttons::buttons()
{
}
buttons::buttons(string btnPng, string btnPng2)
{
btnText.loadFromFile(btnPng);
btnText2.loadFromFile(btnPng2);
}
void buttons::btnInit()
{
btnSprite1.setScale(btnScaler);
btnSprite2.setScale(btnScaler);
btnSprite3.setScale(btnScaler);
btnSprite1.setPosition(btnPosLeft);
btnSprite2.setPosition(btnPosRight);
btnSprite3.setPosition(btnPosFire);
btnSprite3.getTextureRect(btnPosFire);
}
void buttons::btnTouch(){
btnRect1 = btnSprite1.getGlobalBounds();
if(btnRect1.contains(::sf::Touch::getPosition(0).x, ::sf::Touch::getPosition(0).y)){
btnSprite1.setTexture(btnText2);
}
else{
btnSprite1.setTexture(btnText);
}
btnRect2 = btnSprite2.getGlobalBounds();
if(btnRect2.contains(::sf::Touch::getPosition(0).x, ::sf::Touch::getPosition(0).y)){
btnSprite2.setTexture(btnText2);
}
else{
btnSprite2.setTexture(btnText);
}
btnRect3 = btnSprite3.getGlobalBounds();
if(btnRect3.contains(::sf::Touch::getPosition(0).x, ::sf::Touch::getPosition(0).y)){
btnSprite3.setTexture(btnText2);
}
else{
btnSprite3.setTexture(btnText);
}
}
void buttons::btnDraw(RenderWindow &window)
{
window.draw(btnSprite1);
window.draw(btnSprite2);
window.draw(btnSprite3);
}