I am trying to make my Desktop game playable on IOS. I have a Button.cs
class, which has this code in it:
public bool IsPressed(TouchCollection touches)
{
for (int i = 0; i < touches.Count; i++)
{
if (TouchTriggerRect.Contains(touches[i].Position.X, touches[i].Position.Y))
{
return true;
}
}
return false;
}
Where TouchTriggerRect
is a rectangle describing the button coordinates on the screen
I have noticed that the IsPressed
method only works on some buttons (specifically, on all the buttons on the left side of the screen in Landscape orientation)
After some debugging, I understood that the IsPressed
method doesn't work on the buttons placed on the right side of the screen because touches made there are getting offset by some random X and Y
Any suggestions on how to fix this bug? Or what can be causing this problem?
P.S I am using a Monogame.Extended.Camera2D
in my game
P.S.S I have tried to build a simpler app, which draws red circles at all the touch locations it receives, and it workes perfectly. So, the problem is not in my devices
P.S.S.S This is how I get my touch Input: touches = TouchPanel.GetState();
UPD:
This is the code I use for my transformations:
var bounds = camera.BoundingRectangle;
Vector2 pos;
//The button is basically a rectangle, so I give it an X, a Y, a Width and a Height
JumpButton = new Button(bounds.Right - 80, bounds.Bottom - 74, 64, 64);
pos = camera.WorldToScreen(JumpButton.X, JumpButton.Y);
JumpButton.ScreenX = pos.X;
JumpButton.ScreenY = pos.Y;
//Same for all the other buttons
And this is the code for TouchTriggerRect
public Rectangle TouchTriggerRect => new Rectangle((int)ScreenX, (int)ScreenY, Width, Height);