Dropping image only on predefined areas as seen on board games

Hi all,

I am struggling to find a resource for “dropping image only on nearest predefined areas” such as board games.

At first image; i can drag & drop an image(green man) anywhere on screen.

Here is the wrong image
http://farm6.static.flickr.com/5173/5530800242_b6655b01e6.jpg

At 2nd image draggable image (green man) must only be dropped to the nearest hole. kind like magnetically.

This is the correct image
http://farm6.static.flickr.com/5132/5530800276_a0890f06b4.jpg

Can you suggest me any reference? about this issue Thanks in advance.

Best Regards,
Ilker OGUZ

[import]uid: 34788 topic_id: 7887 reply_id: 307887[/import]

Here is the wrong image
http://farm6.static.flickr.com/5173/5530800242_b6655b01e6.jpg

This is the correct image
http://farm6.static.flickr.com/5132/5530800276_a0890f06b4.jpg [import]uid: 34788 topic_id: 7887 reply_id: 28006[/import]

use maths to check item is nearby, or simpler use physics collision.

once you know the item is dropped near enough, you can set it’s new position or transition to the new position to make it more fluid [import]uid: 6645 topic_id: 7887 reply_id: 28017[/import]

Thank you jmp909,

You talk with a caveman and it sounds like astronomy :)I am a non-programmer. So i need a related reference. What do you suggest me to read to be able understand your comment. Thanks in advance.

Links or just topics please. Thx. [import]uid: 34788 topic_id: 7887 reply_id: 28022[/import]

you’re going to need to be a programmer if you want to use corona!

i suggest you go through the include example source code. especially Physics, Graphics and Interface folder

[import]uid: 6645 topic_id: 7887 reply_id: 28029[/import]

[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,0)
–physics.setDrawMode(“hybrid”)

local ROWS=5
local COLS=5
local SLOT_SIZE=96

local board
local player

local collidedWith – slot that we collide with
– A basic function for dragging physics objects
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

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

– if we have collided with a slot,
– move the player to the slot
if(collidedWith ~= nil) then

–player.x = collidedWith.x
–player.y = collidedWith.y

– move smoothly to slot
transition.to(player,
{
time=100,
x = collidedWith.x,
y=collidedWith.y
})
end

end
end

– Stop further propagation of touch event!
return true
end
function onPlayerCollision(self,event)

if(event.phase==“began”) then
if(event.other.type==“slot”) then
collidedWith=event.other
end

elseif(event.phase==“ended” or event.phase==“cancelled”) then
if(event.other==collidedWith) then
collidedWith=nil
end
end

end

local function makeSlot()

local slot = display.newCircle(0,0,(SLOT_SIZE)/4)
slot:setFillColor(255,0,0,255)
slot:setReferencePoint(display.CenterReferencePoint)
physics.addBody(slot, “static”, { density=1,
radius=(SLOT_SIZE/4)
})

slot.isSensor=true
slot.type=“slot”
return slot

end

local function makeBoard()

board=display.newGroup()

local counter=0

for y=1, ROWS, 1 do
for x=1, COLS, 1 do

counter = counter + 1

local slot = makeSlot()
slot.x = (x-1) * SLOT_SIZE
slot.y = (y-1) * SLOT_SIZE
slot.id = counter
board:insert(slot)

end

end

board.x = SLOT_SIZE/2
board.y = SLOT_SIZE/2

end

local function makePlayer()

player = display.newRect(0,0,50,50)

player:setFillColor(0,255,255,255)
player.x= 150
player.y = 450

player.type=“player”

physics.addBody(player,“dynamic”, {density=1})
player.isFixedRotation=true

player.collision = onPlayerCollision
player:addEventListener( “collision”, player)

board:insert(player)

end

makeBoard()
makePlayer()

player:addEventListener( “touch”, startDrag )[/lua] [import]uid: 6645 topic_id: 7887 reply_id: 28043[/import]

Wow. Thanks for the working example, jmp909!
Really nice! [import]uid: 42078 topic_id: 7887 reply_id: 28164[/import]

OMG!

Thank you very much jmp909 you are a hero. This was exactly what i was looking for and i can not wait for to dig out. It looks so good. I hope you didn’t spend much time for it.

It seems like you also answered my other topics by this code (which is eliminating unnecsary code)

I won’t forget your help. I am so impatient to integrate this code with graphics. And i am gonna make it bigger.

Thank you :slight_smile: I will reply you soon.

Best regards,
ilkrgz [import]uid: 34788 topic_id: 7887 reply_id: 28166[/import]

no problems. it was something i wanted to work out for myself anyway but it did not take long anyway. my main problem was working out the physics collision not occurring between static and kinematic objects even when the static objects have isSensor=true. but this works for now with the player as “dynamic” [import]uid: 6645 topic_id: 7887 reply_id: 28178[/import]