Check collision with player on a range of X values


(Nate ) #1

Hey guys! I have done this before with for loops but I do not think that is the cleanest way to accomplish what I want to do. Basically I have some collision code and I want the x value to be a range of numbers instead of just one number. Here is an example:

            if (collide("player", x + 100, y))
            {
                power = 2;
            }
            else
            {
                power = .8;
            }

Where x + 100 is, how would i be able to check 1-100 instead? I have done this with for loops in the past but was wondering if there was a more resource friendly way of doing so. Thank you guys!


(Jonathan Stoler) #2

What about collideRect()?

World.collideRect("player", x, y, 100, 0)


(Nate ) #3

hm, will this check x + 1, x + 2 , x + 3… x + 100? I have never used collideRect() before.


(Jonathan Stoler) #4

Yes, this checks for Entities that are placed within the rectangle (in this case, width = 100), so all 100 x values will be checked. It should be faster than a loop, also, although unless you’re doing lots (millions, maybe?) of collisions every frame, the difference shouldn’t be noticeable.


(Nate ) #5

Hm… still not sure if I am using this right… it doesn’t work unless I do,

World.collideRect("player", x , y, 100, 100);

How would I do it for the opposite direction? -100, -100?

EDIT: Nevermind, I figured out to do the opposite side of the player I just had to do:

World.collideRect("player", x - 100, y, 100, 100);