Turning off and On drag function

I Need help to set drag to become turn off when I hit start, and when I hit pause the drag becomes draggable

Likes here the code for it



function new()
local localGroup = display.newGroup()

local physics = require(“physics”)
require "ui"
physics.start()

–Drag function
local function startDrag( event )
local t = event.target

local phase = event.phase
if “moveable” == true then
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”

elseif t.isFocus then
if “moved” == phase then
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

– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “static”
end

end
end
end
– Stop further propagation of touch event!
return true
end

local gameUI = require(“gameUI”)
local dragBody = gameUI.dragBody

–This represents platform
local wall2 = display.newRect(50, 100, 20, 100)
physics.addBody(wall2, “static”, {friction = 0.7})
wall2.isPlatform = true
wall2:setFillColor(0, 0, 0)
wall2.rotation = 90
wall2:addEventListener( “touch”, startDrag )

local function rotate (event)
wall2.rotation = wall2.rotation +10
end

wall2:addEventListener(“tap”, rotate)

–Platform2
local wall3 = display.newRect(100, 200, 20, 100)
physics.addBody(wall3, “static”, {friction = 0.7})
wall3.isPlatform = true
wall3:setFillColor(0, 0, 0)
wall3.rotation = 90
wall3:addEventListener( “touch”, startDrag )

local function rotate (event)
wall3.rotation = wall3.rotation +10
end
wall3:addEventListener(“tap”, rotate)

–Start button
local bt = display.newImage (“pause.png”)
bt.x = 290
bt.y = 30

–Pause Button
local bt2 = display.newImage (“pauseover.png”)
bt2.x = bt.x
bt2.y = bt.y

-Function for start button
local function done ()
if “ended” == phase then
if “moveable” == false then
physics.start()
physics.addBody(ball, { bounce = 0.3, radius = (ball.contentHeight /1.7) - 2})
bt.isVisible = false
bt2.isVisible = true
end
end
end
bt:addEventListener(“tap”, done)

–Function for pause button
local function done ()
physics.pause()
bt.isVisible = true>
bt2.isVisible = false>
ball.x = 160
ball.y = 30
bt2.isVisible = false
end
bt2:addEventListener(“tap”, done)

What I’m trying to do for the start button function is have the platform become undraggable when start function is started, and when pause button function is tap the platform becomes draggable again.

[import]uid: 17058 topic_id: 18944 reply_id: 318944[/import]

simple variable that you check for true or false in the drag function

like:
[lua]local function drag(event)
if canBeDragged == true then

end
end[/lua]

change this var when you need it to change [import]uid: 16142 topic_id: 18944 reply_id: 72971[/import]

Do I need to put a quotation marks on can be dragged so it looks like this

if “canBeDragged” == true then

end
end

Like that whats I mean are the quotation necessary. [import]uid: 17058 topic_id: 18944 reply_id: 72982[/import]

quotations are only used for strings, for variables you just leave them as they are, no quotations [import]uid: 16142 topic_id: 18944 reply_id: 72984[/import]

Ok thanks I just tried out and putted the var into the drag function and also put it at the start function and it didn’t work what am I doing wrong

Code:

–Drag function
local function startDrag( event )
local t = event.target

local phase = event.phase
if canBeDragged == true then if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”

elseif t.isFocus then
if “moved” == phase then
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

– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “static”
end

end
end
end
– Stop further propagation of touch event!
return true
end

-Function for start button
local function done ()
if canBeDragged == false then physics.start()
physics.addBody(ball, { bounce = 0.3, radius = (ball.contentHeight /1.7) - 2})
bt.isVisible = false
bt2.isVisible = true
end
end
end
bt:addEventListener(“tap”, done)
I’ve done it like that and didn’t work what am I doing wrong [import]uid: 17058 topic_id: 18944 reply_id: 72991[/import]

you need to declare this variable first outside the function
local canBeDragged = true

personally i think you lack some knowledge, you’d better read some lua documentation [import]uid: 16142 topic_id: 18944 reply_id: 72992[/import]

Sorry if I lack knowledge In lua . Also what are some good lua documentation I can read. [import]uid: 17058 topic_id: 18944 reply_id: 73002[/import]

Well Just want to say thanks now I got working it. I do lack a little of lua is just it takes time for me to understand. But thanks for the help really appreciated :). [import]uid: 17058 topic_id: 18944 reply_id: 73004[/import]