position within radius or area

Is it possible to create an area in the middle of the device screen that has a radius of lets say 20px and then execute actions once on object falls “within” that radius?

For example - Objects that are spawned with random postions. The player drags these objects to an area on the screen, once there the object is removed and is rewarded points.

Right now I have it set so if the object is dragged to an exact x/y coordinate it is removed - I don’t want it this exact for obvious reasons.

Thanks for any insight! [import]uid: 15797 topic_id: 15507 reply_id: 315507[/import]

you can actually look at contentBounds of the object
see the sample below
[lua]local circle = display.newCircle(150,150,40)
circle.alpha = .5

local square = display.newRect(50,50,20,20)

local function moveSquare(event)
square.x = event.x
square.y=event.y
if event.phase ==“ended” then
local bounds = circle.contentBounds
if square.x > bounds.xMin and square.x < bounds.xMax and square.y > bounds.yMin and square.y < bounds.yMax then
print(“inside circle”)
end
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
local bounds = circle.contentBounds
if square.x > bounds.xMin and square.x < bounds.xMax and square.y > bounds.yMin and square.y < bounds.yMax then
print(“inside circle”)
end
end
end
return true
end
square:addEventListener(“touch”,moveSquare)[/lua] [import]uid: 71210 topic_id: 15507 reply_id: 57285[/import]

WOW! 1. Thank you for your prompt response and 2. It worked like a charm! I’ve spent the past couple days trying to find a solution (I’m very new to corona). I don’t exactly understand how or why it works - the first instance of the moveSquare event makes sense to me, but I’m not sure what the second does exactly. If you have time to explain that would be great, but otherwise thanks again! [import]uid: 15797 topic_id: 15507 reply_id: 57290[/import]

the ended phase will be executed when you take the finger from the screen.that is when you put the square somewhere. in the ended phase am checking the content bounds of the circle to see whether the object is placed within the bounds of the circle. contentbound is actually part of corona api so am not so sure how its actually calculated or whether it can perfectly identify the full surface area of the circle. but i think it will do the job for you… :slight_smile: [import]uid: 71210 topic_id: 15507 reply_id: 57355[/import]