Maze Collision

I need to figure out how to make the program recognize that the pig is colliding with the grass… The pig is a drag object.

Thanks


require “CiderDebugger”;

local physics = require “physics” 

physics.start()

cx, cy = display.contentCenterX, display.contentCenterY 

cw, ch = display.contentWidth, display.contentHeight

title = display.newRoundedRect(cx, 55, cw, 45, 3)

g1 = {type = “gradient”, color1 = {.1,.3,1}, color2 = {.2,1,1}, direction = “up”}

title:setFillColor(g1)

grass1 = display.newImage(“grass.png”, cx-100, cy+230)

grass1:scale(.5,.5)

physics.addBody(grass1, “static”)

grass2 = display.newImage(“grass.png”, cx-10, cy+230)

grass2:scale(.5,.5)

physics.addBody(grass2, “static”)

grass3 = display.newImage(“grass.png”, cx+100, cy+230)

grass3:scale(.5,.5)

physics.addBody(grass3,“static”)

grass4 = display.newImage(“grassright.png”, cx-150, cy+170)

grass4:scale(.5,.5)

physics.addBody(grass4,“static”)

grass5 = display.newImage(“grassright.png”, cx-150, cy+80)

grass5:scale(.5,.5)

physics.addBody(grass5,“static”)

grass6 = display.newImage(“grassright.png”, cx-150, cy-10)

grass6:scale(.5,.5)

physics.addBody(grass6,“static”)

grass7 = display.newImage(“grassleft.png”, cx-50, cy+90)

grass7:scale(.5,.5)

physics.addBody(grass7,“static”)

grass8 = display.newImage(“grassleft.png”, cx-50, cy-10)

grass8:scale(.5,.5)

physics.addBody(grass8,“static”)

grass9 = display.newImage(“grassleft.png”, cx-50, cy-99)

grass9:scale(.5,.5)

physics.addBody(grass9,“static”)

grass10 = display.newImage(“grassright.png”, cx+40, cy+160)

grass10:scale(.5,.5)

physics.addBody(grass10,“static”)

grass11 = display.newImage(“grassright.png”, cx+40, cy+70)

grass11:scale(.5,.5)

physics.addBody(grass11,“static”)

grass12 = display.newImage(“grassright.png”, cx+40, cy-10)

grass12:scale(.5,.5)

physics.addBody(grass12,“static”)

grass13 = display.newImage(“grassDOWN.png”, cx+20, cy-147.5)

grass13:scale(.5,.5)

physics.addBody(grass13,“static”)

grass14 = display.newImage(“grassdown.png”, cx+100, cy-147.5)

grass14:scale(.5,.5)

physics.addBody(grass14,“static”)

grass15 = display.newImage(“grassleft.png”, cx+150, cy+80)

grass15:scale(.5,.5)

physics.addBody(grass15,“static”)

grass16 = display.newImage(“grassleft.png”, cx+150, cy-10)

grass16:scale(.5,.5)

physics.addBody(grass16,“static”)

grass17 = display.newImage(“grassleft.png”, cx+150, cy-70)

grass17:scale(.5,.5)

physics.addBody(grass17,“static”)

function dragging ( event ) 

 local t = event.target 

 local phase = event.phase 

 if (phase == “began”) then 

 display.getCurrentStage():setFocus(t); 

 t.isFocus = true; 

 – starting at: 

 t.x0 = event.x - t.x 

 t.y0 = event.y - t.y 

 elseif t.isFocus then 

 if (phase == “moved”) then 

 t.x = event.x - t.x0 

 t.y = event.y - t.y0 

 elseif (phase == “ended” or phase == “cancelled”) then 

 display.getCurrentStage():setFocus(nil) 

 t.isFocus = false 

 end 

 end 

 return true 

end

local function onPigCollision(e) 

 if e.phase == “began” then 

 print(“It works!”)

 end 

end

pig = display.newImage(“pig.png”, cx-125, cy-150) 

pig:scale(.06, .06) – 30% 

pig:addEventListener(“touch”, dragging)

physics.addBody(pig,“static”)

pig:addEventListener(“collision”, onPigCollision)

um this is kinda unrelated to the question but its just a tip on how to improve your code. At the moment you seem to be spawning grass in a messy way that is not great on performance, instead you could do this

display.setStatusBar( display.HiddenStatusBar ) display.setDefault( "background", 80/255 ) local cx = display.contentCenterX local cy = display.contentCenterY local function newGrassObj( params )     local grass = display.newImage( params.image, params.x, params.y)     grass:scale( .5, .5 )     return grass end -- create a params table to hold information about grass local params = {     {image = "grass.png", x = cx - 100, y = cy + 230 },     {image = "grass.png", x = cx - 10, y = cy + 230 },     {image = "grass.png", x = cx + 100, y = cy + 230 },     {image = "grassright.png", x = cx - 150, y = cy + 170 },     {image = "grassright.png", x = cx - 150, y = cy + 170 },     {image = "grassright.png", x = cx - 150, y = cy + 80 },     {image = "grassleft.png", x = cx - 50, y = cy + 90 },     {image = "grassleft.png", x = cx - 50, y = cy - 10 },     {image = "grassleft.png", x = cx - 50, y = cy - 99 },     {image = "grassright.png", x = cx - 40, y = cy + 160 },     {image = "grassright.png", x = cx + 40, y = cy + 70 },     {image = "grassright.png", x = cx + 40, y = cy - 10 },     {image = "grassDOWN.png", x = cx + 20, y = cy - 148 },     {image = "grassdown.png", x = cx + 100, y = cy - 148 },     {image = "grassleft.png", x = cx + 150, y = cy + 80 },     {image = "grassleft.png", x = cx + 150, y = cy - 10 },     {image = "grassleft.png", x = cx + 150, y = cy - 70 }, } local collection = {} -- Iterate through params array and add new grass into an array for \_,item in ipairs( params ) do     local grass = newGrassObj( item )     collection[#collection + 1] = grassOBJ end function addPhysics ( event )     for \_,ball in ipairs( collection ) do         physics.addBody( grassOBJ, "static" )     end end addPhysics ()  

um this is kinda unrelated to the question but its just a tip on how to improve your code. At the moment you seem to be spawning grass in a messy way that is not great on performance, instead you could do this

display.setStatusBar( display.HiddenStatusBar ) display.setDefault( "background", 80/255 ) local cx = display.contentCenterX local cy = display.contentCenterY local function newGrassObj( params )     local grass = display.newImage( params.image, params.x, params.y)     grass:scale( .5, .5 )     return grass end -- create a params table to hold information about grass local params = {     {image = "grass.png", x = cx - 100, y = cy + 230 },     {image = "grass.png", x = cx - 10, y = cy + 230 },     {image = "grass.png", x = cx + 100, y = cy + 230 },     {image = "grassright.png", x = cx - 150, y = cy + 170 },     {image = "grassright.png", x = cx - 150, y = cy + 170 },     {image = "grassright.png", x = cx - 150, y = cy + 80 },     {image = "grassleft.png", x = cx - 50, y = cy + 90 },     {image = "grassleft.png", x = cx - 50, y = cy - 10 },     {image = "grassleft.png", x = cx - 50, y = cy - 99 },     {image = "grassright.png", x = cx - 40, y = cy + 160 },     {image = "grassright.png", x = cx + 40, y = cy + 70 },     {image = "grassright.png", x = cx + 40, y = cy - 10 },     {image = "grassDOWN.png", x = cx + 20, y = cy - 148 },     {image = "grassdown.png", x = cx + 100, y = cy - 148 },     {image = "grassleft.png", x = cx + 150, y = cy + 80 },     {image = "grassleft.png", x = cx + 150, y = cy - 10 },     {image = "grassleft.png", x = cx + 150, y = cy - 70 }, } local collection = {} -- Iterate through params array and add new grass into an array for \_,item in ipairs( params ) do     local grass = newGrassObj( item )     collection[#collection + 1] = grassOBJ end function addPhysics ( event )     for \_,ball in ipairs( collection ) do         physics.addBody( grassOBJ, "static" )     end end addPhysics ()