I've created a wall that my player ( a draggable object) must navigate around to reach his goal. Right now, my player can pass right through this wall as if it was invisible. But instead, of course, I want this wall to stop my player from passing through it. I want to use physics to do this, as I plan to have many walls in my game and don't with to hand-code the boundaries for each. I'm guessing I just need to declare the physics statements correctly for my draggable object and wall (barrier) to get the effect I want, but I'm not sure how to do this. Here's what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | --declare physics local physics = require("physics") physics.start() physics.setGravity(0, 0) --declare my draggable object (player): local player = display.newCircle(20,20,40) player.x = 320 player.y = 900 physics.addBody(player,"kinematic") --my barrier: local barrier = display.newRect( 0, 480, 320, 10 ) barrier:addPhysics(physics,'static',{}) physics.addBody(barrier,"static") --Player movement function local function onTouch( event ) local player = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = player.parent parent:insert( player ) display.getCurrentStage():setFocus( player ) player.isFocus = true -- Store initial position player.x0 = event.x - player.x player.y0 = event.y - player.y elseif player.isFocus then if "moved" == phase then player.x = event.x - player.x0 player.y = event.y - player.y0 end end return true end |