Stop draggable Objects, After and before of variable change

Hello everyone :slight_smile:
i need to stop all my created objects, try this code and you will understand, it is simple, when active is equal to 1 i need to stop all my objects the default value of variable active is 0 and i can create and drag the objects but when i touche on the ball the active value is equal to 1 and i should not drag the objects, but it dont appear, i can drag the objects that was created before than i touch on the ball and the objects created after touch on the ball dont drag and it is good, only need to stop the drag objects created before touche on the ball. try the code to see the problem :slight_smile:

[code]
display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
physics.start( true )
physics.setDrawMode( “normal” )
physics.setGravity( 0,0 )

–***************************************************

– Game

–***************************************************

local game = function()

local active = 0
print(active)
local ball1 = display.newCircle( 80, 120, 20 )
ball1:setFillColor( 0, 255, 0 )
physics.addBody(ball1,“kinematic”, { density=2, bounce=0.3, radius=25});
ball1.x = 250; ball1.y = 200

local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)

local function touch(event)
if event.phase == “ended” then
active = active + 1
print(active)
physics.setGravity( 0,9.8 )
ball1.bodyType=“dynamic”
end
end
ball1:addEventListener( “touch”, touch )

local block1 = {}

local spawnID = 0
local function spawnMyObject (event)
if event.phase == “began” then

spawnID = spawnID + 1
if #block1 < 3 then
block1[#block1+1] = display.newRect( 50, 50, 50, 50 )
block1[#block1]:setFillColor( 255, 255, 255, 100 )
block1[#block1].id = spawnID
physics.addBody(block1[#block1], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true

function spawner:collision (event)
if event.phase == “began” then
for i = 1, #block1, 1 do
if block1[i].id == event.other.id then
block1[i]:removeSelf()
table.remove(block1,i)
return true
end
end
end
end

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

t.x0 = event.x - t.x
t.y0 = event.y - t.y

event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

event.target.bodyType = “dynamic”

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”

end – last if

end-- 2º if “move” end
end --if begin

end – startdrag end

if active == 0 then
block1[#block1]:addEventListener(“touch”, startDrag)
elseif active == 1 then
block1[#block1].id:removeEventListener(“touch”, startDrag)
end

end – if #block end

end – 1º if “begin” end

end --spawnMyObject end

spawner:addEventListener(“touch”, spawnMyObject)

end–pausegame ()

game()
[/code] [import]uid: 26056 topic_id: 16429 reply_id: 316429[/import]

Not sure if I understood exactly what you trying to accomplish

  • Able to drag all objects

  • Touch the ball and STOP all objects from being dragged?
    All objects or objects that spawn before you touch or after?

If i can clearly understand I might be able help ya out ! [import]uid: 30314 topic_id: 16429 reply_id: 61327[/import]

yes, when i touch on the ball i want to stop all obcjects, before and after than i touched on the ball

because with my code i only can stop objects that were created after i touch on the ball and i need to stop both, after and before than i touched on the ball [import]uid: 26056 topic_id: 16429 reply_id: 61329[/import]

Ill run your code give me a sec and ill try and fix this and explain whats wrong [import]uid: 30314 topic_id: 16429 reply_id: 61337[/import]

ok, thanks LeivaGames :slight_smile: [import]uid: 26056 topic_id: 16429 reply_id: 61338[/import]

I found the problem im just trying to figure out how to solve it in a clean way, I got into a similar problem in one of my projects and was due to spawning objects from a table executing a function with a system timer. Ill post back as soon as I fix this for you, im still working on it! [import]uid: 30314 topic_id: 16429 reply_id: 61342[/import]

ok leiva thanks for the help, i am making my project and i cant continue because of this part :confused:
thanks again
[import]uid: 26056 topic_id: 16429 reply_id: 61343[/import]

Question in this sample is the ball your button? Because this is what I got so far

IF you have an object out and you touch ball the ball becomes dynamic
falls off screen and current objects are unable to drag and future objects too.

Only problem is you cant touch the ball unless there is an object on screen
this can be fix easily but i think i manage to solve your problem?? [import]uid: 30314 topic_id: 16429 reply_id: 61351[/import]

no the ball is a object that i cant touch to win gravity and loss draggable objects and when i touch she is dynamic and i need to stop all draggable objects, like blocks etc

but i dont want to touch on the ball more than 1 time, i only need to touch 1 time to active the ball and stop all draggable objects, i dont know who can help me with this problem :confused: [import]uid: 26056 topic_id: 16429 reply_id: 61354[/import]

Ok you have a few options:

I created a game call Dragging Physics earlier this year and what I did
was create a START button and STOP button.

  • The Start button removes all event listeners
  • The Stop button adds all event listeners

You can download it and see what I mean.

Anyhow try this code out this will prevent further dragging on current
and future objects the only problem is the ball (button) falls off but ill
show you what else you can do.

For one you can leave the button but just remove touch listener.

[lua]module(…, package.seeall)

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

display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
physics.start( true )
physics.setDrawMode( “normal” )
physics.setGravity( 0,0 )

–***************************************************

– Game

–***************************************************

local game = function()

local active = 0
print(active)
local ball1 = display.newCircle( 80, 120, 20 )
ball1:setFillColor( 0, 255, 0 )
physics.addBody(ball1,“kinematic”, { density=2, bounce=0.3, radius=25});
ball1.x = 250; ball1.y = 200

local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)

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

t.x0 = event.x - t.x
t.y0 = event.y - t.y

event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

event.target.bodyType = “dynamic”

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”

end – last if

end-- 2º if “move” end
end --if begin

end – startdrag

local block1 = {}

local spawnID = 0
local function spawnMyObject (event)

function touch(event)
if event.phase == “ended” then
active = active + 1
print(active)
physics.setGravity( 0,9.8 )
ball1.bodyType=“dynamic”
if active == 1 then
block1[#block1]:removeEventListener(“touch”, startDrag)
end

end
end
ball1:addEventListener( “touch”, touch )

if event.phase == “began” then

spawnID = spawnID + 1
if #block1 < 3 then
block1[#block1+1] = display.newRect( 50, 50, 50, 50 )
block1[#block1]:setFillColor( 255, 255, 255, 100 )
block1[#block1].id = spawnID
physics.addBody(block1[#block1], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true

if active == 0 then
block1[#block1]:addEventListener(“touch”, startDrag)
if active == 1 then
block1[#block1]:removeEventListener(“touch”, startDrag)

end
end

function spawner:collision (event)
if event.phase == “began” then
for i = 1, #block1, 1 do
if block1[i].id == event.other.id then
block1[i]:removeSelf()
table.remove(block1,i)
return true
end
end
end
end

end

end – 1º if “begin” end

end --spawnMyObject end

spawner:addEventListener(“touch”, spawnMyObject)

end–pausegame ()

game()

return localGroup
end [import]uid: 30314 topic_id: 16429 reply_id: 61357[/import]

Hope that helped any other problem with that current code can
be fix a lot easier the main issue was the way the code was set up
the “current spawned” object were not reading if active had change
or not they only checked “active” once they spawned.

[import]uid: 30314 topic_id: 16429 reply_id: 61358[/import]

i will try now the code, i downloaded the app :slight_smile: and is good :slight_smile: congratz :slight_smile: and very thank for the help i will now try this [import]uid: 26056 topic_id: 16429 reply_id: 61359[/import]

Thanks hope this helps if not let me know what problems you run
into and well fix it, my app is “ok” lol i was only studying programming
with 0% experienced so its more like my study project then a real app lol
But it helps out and gives you more experience. [import]uid: 30314 topic_id: 16429 reply_id: 61360[/import]

i create 3 objects and after click on the ball and i can drag the first and second object, but the first i cant drag, remaining stop drag the first 2 objects :smiley: [import]uid: 26056 topic_id: 16429 reply_id: 61362[/import]

Yea i see that issue i only tested with 1 square is why i did not notice it
ill re post update code just a min about to try something different. [import]uid: 30314 topic_id: 16429 reply_id: 61365[/import]

lol here is the laugh, i been trying to help you for more then 3hours and I
went over the code very careful and the issue was just adding an extra line
of code lol this one worked like a charm:

[lua]module(…, package.seeall)

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

display.setStatusBar( display.HiddenStatusBar )
local physics = require “physics”
physics.start( true )
physics.setDrawMode( “normal” )
physics.setGravity( 0,0 )

–***************************************************

– Game

–***************************************************

local game = function()

local active = 0
print(active)
local ball1 = display.newCircle( 80, 120, 20 )
ball1:setFillColor( 0, 255, 0 )
physics.addBody(ball1,“kinematic”, { density=2, bounce=0.3, radius=25});
ball1.x = 250; ball1.y = 200

local spawner = display.newRect( 50, 5, 40, 40 )
spawner:setFillColor(150, 0, 0)
physics.addBody(spawner, “static”, {isSensor = true})
spawner:addEventListener(“collision”, spawner)

local function touch(event)
if event.phase == “ended” then
active = active + 1
print(active)
physics.setGravity( 0,9.8 )
ball1.bodyType=“dynamic”
end
end
ball1:addEventListener( “touch”, touch )

local block1 = {}

local spawnID = 0
local function spawnMyObject (event)
if event.phase == “began” then

spawnID = spawnID + 1
if #block1 < 3 then
block1[#block1+1] = display.newRect( 50, 50, 50, 50 )
block1[#block1]:setFillColor( 255, 255, 255, 100 )
block1[#block1].id = spawnID
physics.addBody(block1[#block1], “kinematic”, { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true

function spawner:collision (event)
if event.phase == “began” then
for i = 1, #block1, 1 do
if block1[i].id == event.other.id then
block1[i]:removeSelf()
table.remove(block1,i)
return true
end
end
end
end

local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase and active == 0 then
display.getCurrentStage():setFocus( t )
t.isFocus = true

t.x0 = event.x - t.x
t.y0 = event.y - t.y

event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

event.target.bodyType = “dynamic”

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– Switch body type to kinematic, to avoid gravity
if ( not event.target.isPlatform ) then
event.target.bodyType = “kinematic”

end – last if

end-- 2º if “move” end
end --if begin

end – startdrag end

if active == 0 then
block1[#block1]:addEventListener(“touch”, startDrag)
elseif active == 1 then
block1[#block1]:removeEventListener(“touch”, startDrag)
end

end – if #block end

end – 1º if “begin” end

end --spawnMyObject end

spawner:addEventListener(“touch”, spawnMyObject)

end–pausegame ()

game()

return localGroup
end [import]uid: 30314 topic_id: 16429 reply_id: 61368[/import]

yes is really it :slight_smile: thanks very much for the help it is very useful :slight_smile:
i am very grateful :slight_smile: [import]uid: 26056 topic_id: 16429 reply_id: 61369[/import]

Youre welcome Im glad i was of some help best of luck on your app! [import]uid: 30314 topic_id: 16429 reply_id: 61370[/import]

thank you very much again :slight_smile: [import]uid: 26056 topic_id: 16429 reply_id: 61371[/import]