I’ve been trying to find a solution to finding collisions between the two different types of randomly generated tiles I am laying on my grid, and I’m getting a bit confused. First, here is my grid code:
local obstacleAmount = 8
local cellWidth = 10
local cellHeight = 15
local grid = {}
local barrier = display.newGroup
function buildGrid()
for x = 0, cellHeight do
grid[x] = {}
for y = 0, cellWidth do
if math.random(0,10) \<= obstacleAmount then
grid[x][y] = 1
cella = display.newImage("images/brick.png", x\*32, y\*32)
else
grid[x][y] = 0
cellb = display.newImage("images/wall.png", x\*32, y\*32)
barrier:insert(cellb)
end
end
end
end
buildGrid()
Seems straightforward enough to me. I decided to put the wall tile in a display group and use the collision testing found here to determine if my player has come into contact with any “cellb” tiles.
function hitTestObjects(obj1, obj2)
return obj1.contentBounds.xMin \< obj2.contentBounds.xMax
and obj1.contentBounds.xMax \> obj2.contentBounds.xMin
and obj1.contentBounds.yMin \< obj2.contentBounds.yMax
and obj1.contentBounds.yMax \> obj2.contentBounds.yMin
end
The actual behavior is that when my player comes in contact with ANY tiles (cella OR cellb) it is registering a collision. I have narrowed it down to the manner in which I am iterating out my tiles (the x*32 portion of my above code) but I am at a loss to find a way to accomplish the iteration while still achieving the desired affect of random generation with non-physics based collision detection. I have tried moving the image files out of the function and just calling from a table, but of course I still need to define the cell width and height as relating to the size of the grid.
My preferred method would be identifying the grid[x][y] = 0 portion with the below listener:
local function drag(event)
if event.phase == "moved" then
player.x = event.x
player.y = event.y
print (hitTestObjects(player, barrier))
But I’m not sure exactly how to implement that. I’ve tried several different methods of replacing the event.phase portion, the barrier portion, and using the cellb identifier itself. Should I instead be specifying the points of cellb? I’m open to different ways to handle this.
Does anyone have any tips for me? Any assistance would be greatly appreciated. If anyone requires additional information from me, please let me know. Thanks! [import]uid: 135394 topic_id: 26256 reply_id: 326256[/import]