How to add collision and dragging to randomly displayed objects?

Hi. I want to add collision as well as make the randomly displayed objects dragable.The objects fall from the top.Here’s how I tried but not working.I haven’t added code to drag randomly displayed objects here but I have tried.Only objects are being displayed.Help me with this issue.Thanks.

local function box()

rand = math.random( 10 )

if (rand < 5) then 

local crate1 = display.newImageRect( “but3.jpg”, 100, 100 )

crate1.x, crate1.y = 53, -80

physics.addBody( crate1, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1.myName = “crate1”

local crate3 = display.newImageRect( “crate.png”, 100, 100 );

crate3.x, crate3.y = 267, -80

physics.addBody( crate3, { density=0, friction=0, bounce=0 } )

physics.setGravity( 0, 1)

elseif(rand < 9 ) then 

local crate1 = display.newImageRect( “but3.jpg”, 100, 100 );

crate1.x, crate1.y = 53, -80

physics.addBody( crate1, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1.myName = “crate1”

local crate2 = display.newImageRect( “crate.png”, 100, 100 );

crate2.x, crate2.y = 160, -80

physics.addBody( crate2, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

else

local crate2 = display.newImageRect( “crate.png”, 100, 100 );

crate2.x, crate2.y = 160, -80

physics.addBody( crate2, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

local crate3 = display.newImageRect( “crate.png”, 100, 100 );

crate3.x, crate3.y = 267, -80

physics.addBody( crate3, { density=0, friction=0, bounce=0 } )

physics.setGravity( 0, 1)

end

end

local function gameLoop()

box()

 end

 gameLoopTimer = timer.performWithDelay( 3000, gameLoop, 0 )

 local platform = display.newRect( 0, 0, 60, 30 )

platform.surfaceType = “superbounce”

platform.x, platform.y = display.contentCenterX, display.contentCenterY+80

physics.addBody( platform, “static”, { bounce=0.0, friction=0.3 } )

platform.myName = “platform”

–Code to drag Platform

 local function dragplatform( event )

    local platform = event.target

    local phase = event.phase

    if ( “began” == phase ) then

        – Set touch focus on the platform

        display.currentStage:setFocus( platform )

        – Store initial offset position

        platform.touchOffsetX = event.x - platform.x

         platform.touchOffsetY = event.y - platform.y

    elseif ( “moved” == phase ) then

        – Move the platform to the new touch position

        platform.x = event.x - platform.touchOffsetX

        platform.y = event.y - platform.touchOffsetY

    elseif ( “ended” == phase or “cancelled” == phase ) then

        – Release touch focus on the platform

        display.currentStage:setFocus( nil )

    end

    return true  – Prevents touch propagation to underlying objects

end

platform:addEventListener( “touch”, dragplatform )

–HERE IS THE CODE FOR COLLISION.

local function onCollision( event )

if ( event.phase == “began” ) then

local obj1 = event.object1

local obj2 = event.object2

if  ( obj1.myName == “platform” and obj2.myName == “crate1” ) 

then

display.remove( obj1 )

           

end 

end

local function box()

rand = math.random( 10 )

if (rand < 5) then 

local crate1 = display.newRect( 0,0,100, 100 );

crate1.x, crate1.y = 53, -80

crate

physics.addBody( crate1, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1.myName = “crate1”

local crate3 = display.newRect( 0,0,100, 100 );

crate3.x, crate3.y = 267, -80

physics.addBody( crate3, { density=0, friction=0, bounce=0 } )

physics.setGravity( 0, 1)

 crate1:addEventListener(“touch”,dragplatform)

crate3:addEventListener(“touch”,dragplatform)

elseif(rand < 9 ) then 

local crate1 = display.newRect( 0,0,100, 100 );

crate1.x, crate1.y = 53, -80

physics.addBody( crate1, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1.myName = “crate1”

local crate2 = display.newRect( 0,0,100, 100 );

crate2.x, crate2.y = 160, -80

physics.addBody( crate2, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1:addEventListener(“touch”,dragplatform)

crate2:addEventListener(“touch”,dragplatform)

else

local crate2 = display.newRect( 0,0,100, 100 );

crate2.x, crate2.y = 160, -80

physics.addBody( crate2, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate2:addEventListener(“touch”,dragplatform)

local crate3 = display.newRect( 0,0,100, 100 );

crate3.x, crate3.y = 267, -80

physics.addBody( crate3, { density=0, friction=0, bounce=0 } )

physics.setGravity( 0, 1)

crate3:addEventListener(“touch”,dragplatform)

end

end

replace this code for dragging falling objects 

add this code for collision

Runtime:addEventListener(“collision”, onCollision

set myName to each crate object and check for collision in onCollision function…

First of all use code formating. It is hard to read your code.

What does it mean it is not working? 

  1. I think you need forward declaration.

    – top of file local crate1 local crate2

Read about local and global variable https://docs.coronalabs.com/daily/guide/start/introLua/index.html#variables

  1. I don’t see physics.start(). You need it to everything work. To how  setup physics engine you can read https://docs.coronalabs.com/daily/guide/physics/physicsSetup/index.html

  2. You need instruction below  for handle global  collision

    Runtime:addEventListener( “collision”, onCollision)

Read about collision handling https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#handling

  1. One physics.setGravity( 0, 1) is enough. Put it on begin of function or file.

EDIT : Some useful links about dragging objects

https://coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

https://forums.coronalabs.com/topic/35265-drag-object-within-bounds/

http://stackoverflow.com/questions/21755916/corona-lua-dragging-object-without-physics

Hope this help :slight_smile:

local function box()

rand = math.random( 10 )

if (rand < 5) then 

local crate1 = display.newRect( 0,0,100, 100 );

crate1.x, crate1.y = 53, -80

crate

physics.addBody( crate1, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1.myName = “crate1”

local crate3 = display.newRect( 0,0,100, 100 );

crate3.x, crate3.y = 267, -80

physics.addBody( crate3, { density=0, friction=0, bounce=0 } )

physics.setGravity( 0, 1)

 crate1:addEventListener(“touch”,dragplatform)

crate3:addEventListener(“touch”,dragplatform)

elseif(rand < 9 ) then 

local crate1 = display.newRect( 0,0,100, 100 );

crate1.x, crate1.y = 53, -80

physics.addBody( crate1, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1.myName = “crate1”

local crate2 = display.newRect( 0,0,100, 100 );

crate2.x, crate2.y = 160, -80

physics.addBody( crate2, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate1:addEventListener(“touch”,dragplatform)

crate2:addEventListener(“touch”,dragplatform)

else

local crate2 = display.newRect( 0,0,100, 100 );

crate2.x, crate2.y = 160, -80

physics.addBody( crate2, { density=0, friction=0.3, bounce=0 } )

physics.setGravity( 0, 1)

crate2:addEventListener(“touch”,dragplatform)

local crate3 = display.newRect( 0,0,100, 100 );

crate3.x, crate3.y = 267, -80

physics.addBody( crate3, { density=0, friction=0, bounce=0 } )

physics.setGravity( 0, 1)

crate3:addEventListener(“touch”,dragplatform)

end

end

replace this code for dragging falling objects 

add this code for collision

Runtime:addEventListener(“collision”, onCollision

set myName to each crate object and check for collision in onCollision function…

First of all use code formating. It is hard to read your code.

What does it mean it is not working? 

  1. I think you need forward declaration.

    – top of file local crate1 local crate2

Read about local and global variable https://docs.coronalabs.com/daily/guide/start/introLua/index.html#variables

  1. I don’t see physics.start(). You need it to everything work. To how  setup physics engine you can read https://docs.coronalabs.com/daily/guide/physics/physicsSetup/index.html

  2. You need instruction below  for handle global  collision

    Runtime:addEventListener( “collision”, onCollision)

Read about collision handling https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html#handling

  1. One physics.setGravity( 0, 1) is enough. Put it on begin of function or file.

EDIT : Some useful links about dragging objects

https://coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

https://forums.coronalabs.com/topic/35265-drag-object-within-bounds/

http://stackoverflow.com/questions/21755916/corona-lua-dragging-object-without-physics

Hope this help :slight_smile: