Hello,
i am working with tables in corona, and had learned about it, and the code is running almost perfectly, have 1/2 errors.
what is working good well:
- i only can create until 3 block
what isnt working well:
- when i remove a object i need to remove on the table, because, when i create a object it is counting +1 on the table, and when i remove the object i want to remove it on table, -1, the algorithm do it but not well, i create 3 objects and i remove 1 object, i should stay with #table = 2 and can only create one more object because the limit is 3, but it is the part that dont work well
Try the code and will understand what is wrong
display.setStatusBar( display.HiddenStatusBar )
local physics = require "physics"
physics.start( true )
physics.setDrawMode( "normal" )
physics.setGravity( 0,0 )
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
-- Game
--\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
local pausegame = 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 )
---------------it is the important part because is where is the lua table that i am working, to add and remove objects------------
local block1 = {}
function spawner:collision (event)
event.other:removeSelf()
block1 = block1[#block1-1]
end
local function spawnMyObject (event)
if event.phase == "began" then
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
-----------------end the important part---------------
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]:removeEventListener("touch", startDrag)
end
end -- if #block end
end -- 1º if "begin" end
end --spawnMyObject end
spawner:addEventListener("touch", spawnMyObject)
end--pausegame ()
pausegame()
thank you very much
[import]uid: 26056 topic_id: 16082 reply_id: 316082[/import]
try it