So I decided to make my own button/collision function which pretty much just tests if a position (x, y)are in a certain area (x,y,w,h):
function button(actX,actY,x,y,w,h)
--test if actX and actY are in the area defined by x,y,w,h
if (x + w/2) > actX
and actX > (x - w/2)
and (y + h/2) > actY
and actY > (y - h/2) then
--Special instruction for if actX or actY are the CurrentTouch
if actX == CurrentTouch.x
or actY == CurrentTouch.y then
if CurrentTouch.state == ENDED then
return false
end
end
-- if in the area, return true, else return false
return true
else
return false
end
end
Then I made a table consisting of the spawn position, and all boxes that make up a level in the game, and a variable that states the level that loads:
levels = {
l0 = {
-- Spawn location for charactor
playerspawn = {x = WIDTH/2,y = HEIGHT/2},
-- collision is a table consisting of tables that define a collision area
collision = {
-- Collision box 1 (the one the charactor isn't colliding with)
{p = false, r = 0, x = WIDTH/2, y = 6, w = WIDTH, h = 12},
-- Collision box 2 (this box works)
{p = true, r = 0, x = 250, y = 200, w = 200, h = 12}
}
},
l1 = {
-- this is for another level but is not used yet
playerspawn = {x = 10,y = HEIGHT/10}
}
}
-- this is the level that is being loaded
currentlevel = levels.l0
Then I made my charactor able to collide with all rectangles stated in currentlevel.collision (levels.l0.collision) using the button function and a for loop and drew them on screen with rect():
for k,v in pairs(currentlevel.collision) do
-- for all the collision boxes, make a button to detect somthing
if button(self.x,self.y,v.x,v.y,v.w,v.h) == true then
-- self.collided tells the charactor whether somthing has collided with one of the boxes or not.
self.collided = true
else
self.collided = false
end
-- this colors the wall black or white based on a bool.
if v.p == true then
fill(255, 255, 255, 255)
else
fill(0, 0, 0, 255)
end
-- this draws a rectangle for each of the areas so i can see them on screen
pushMatrix()
rect(v.x,v.y,v.w,v.h)
popMatrix()
end
However, the charactor only collides with the second rectangle in the table, and not the first rectangle. self.collided doesn’t seem to get set to true when the charactor touches the first collision area. Can someone help me find where, and what the problem(s) are that are causing this?