Please help, Im trying to create an app where at first you can drag some
boxes (called buttons in the app) and then after you press the “newGameButton” button, those boxes become unmovable. Ive been trying to do this by removing the event listener or
removing the physics engine but nothing seems to work. I need help asap.
Thanks heres my code. I’m using a modified version of the DragMe sample
code
local physics = require("physics") physics.start() physics.setGravity(0,0) local gameIU = require("gameUI") local arguments = { { x=50, y=10, w=100, h=100, r=10, red=255, green=0, blue=128 }, { x=10, y=50, w=100, h=100, r=10, red=0, green=128, blue=255 }, { x=90, y=90, w=100, h=100, r=10, red=255, green=255, blue=0 } } local function printTouch( event ) if event.target then local bounds = event.target.contentBounds print( "event(" .. event.phase .. ") ("..event.x..","..event.y..") bounds: "..bounds.xMin..","..bounds.yMin..","..bounds.xMax..","..bounds.yMax ) end end function onTouch( event ) local t = event.target -- Print info about the event. For actual production code, you should -- not call this function because it wastes CPU resources. printTouch(event) phase1 = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end function localDrag(event) gameUI.dragBody(event, {maxForce=10, frequency=2}) --slow dragging end -- Iterate through arguments array and create rounded rects (vector objects) for each item for \_,item in ipairs( arguments ) do local button = display.newRoundedRect( item.x, item.y, item.w, item.h, item.r ) button:setFillColor( item.red, item.green, item.blue ) button.strokeWidth = 2 button:setStrokeColor( 200,200,200,255 ) button:addEventListener ("touch", localDrag) physics.addBody(button, {}) button.linearDamping =10 button.angularDamping =5 -- Make the button instance respond to touch events end function startGame (event) if event.phase == "ended" then phase1:removeSelf(event) --onTouch = nil --physics.stop() --print( "Button Touch(" .. event.phase .. ") ("..event.x..","..event.y..")" ) end end local switch = display.newImage("newGameButton.png",-90, 300) switch.xScale =.5; switch.yScale =.5 switch:addEventListener ("touch", startGame)