hello people, i am trying to make a simples drag objects, with more things, that is when i touch on the ball the gamer cant drag or cant create more objects, and it isnt happen, if i create a object and after click on the ball it shouldnt drag or create another object.
local physics = require "physics"
physics.start()
physics.setGravity (0,0)
local block1 = {}
local active = 0
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
-- Drag and colision objects
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
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 ball = function()
function ball1:touch( event )
if(event.phase == "ended") then
active = active + 1
ball1.bodyType="dynamic"
physics.setGravity (0,9.8)
end
end
ball1:addEventListener( "touch", ball1 )
end
ball()
--Objects and Drag
local function spawnMyObject (event)
if event.phase == "began" then
--remove function
function spawner:collision (event)
event.other:removeSelf()
end
--objects and physics
if #block1 \< 3 then
block1[#block1+1] = display.newRect( 50, 50, 50, 50 )
block1[#block1]:setFillColor( 255, 255, 255, 100 )
physics.addBody(block1[#block1], "kinematic", { density=2, friction=0, bounce=0, shape=barril })
block1[#block1].isFixedRotation = true
local function startDrag( event )
local t = event.target
local phase = event.phase
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
-- Stop current motion, if any
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
--return true
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
if active == 0 then
spawner:addEventListener("touch", spawnMyObject)
elseif active == 1 then
spawner:removeEventListener("touch", spawnMyObject)
end
P.S: if you need main.lua to test the code it is on a thread on the top [import]uid: 26056 topic_id: 15519 reply_id: 58148[/import]