Touch with proximity/collision detection

Hi guys, complete newbie here.
I'm having problems with a simple concept in Corona. I have an idea for a simple game where the player touches a character and drags/moves him to a box shape (like a building) to finish the level.
I have the characters spawning and touch enabled but can't figure out how to get the box object to respond to the player when I drag him onto it.

Any help will be appreciated. Thanks

if you are using physics you can detect collision between the character and box.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
 
 
local circle = display.newCircle(150,150,40)
circle.alpha = .5
physics.addBody(circle ,"static",{isSensor=true})
 
 
local square = display.newRect(50,50,20,20)
physics.addBody(square ,"dynamic")
 
 
local function squareCollision(event)
        if event.phase =="began" then
          print("inside circle")
        end
end
 
local function moveSquare( event )
        local t = event.target
        local phase = event.phase
        if "began" == phase then
                -- Make target the top-most object
                local parent = t.parent
                parent:insert( t )
                display.getCurrentStage():setFocus( t )         
                t.isFocus = true
        elseif t.isFocus then
                if "moved" == phase then
                      t.x = event.x 
                      t.y = event.y  
                                elseif "ended" ==phase then
                      display.getCurrentStage():setFocus( nil )         
                       t.isFocus = false                        
                end                               
        end 
        return true
end
 
 
square:addEventListener("touch",moveSquare)
square:addEventListener("collision",squareCollision)
views:1408 update:2011/10/17 21:25:02
corona forums © 2003-2011