Question

I have a buggy IF statement. I am trying to check if the touch is within a certain area. However, it seems to be creating an invisible touch box for the sprite, a never ending touch box to it's right and off the screen. The left and top area work fine but the X axis to the right is buggy. Here is my IF statement.

if (tl.State == TouchLocationState.Pressed
    && tl.Position.X >= harePlayer.Position.X
    && tl.Position.Y >= harePlayer.Position.Y
    && harePlayer.Position.X <= (harePlayer.Position.X + 52)
    && tl.Position.Y <= (harePlayer.Position.Y + 50))
Was it helpful?

Solution

I took the liberty of reformatting your code, to avoid having a scrollbar in the question, and immediately spotted the problem:

&& harePlayer.Position.X <= (harePlayer.Position.X + 52)

This part will always be true. (always = unless you get a new (random?) value very time you read either of these properties)

Change it to this:

&& tl.Position.X <= (harePlayer.Position.X + 52)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top