Hello Guys,
how can I move two objects together with a single touch drag?
This is the code I used but does not work. (move individually)
Is there a solution?
local lcontre_a_PART1 = display.newImage("Lcon3_a_PART1.png")
local lcontre_a_PART2 = display.newImage("Lcon3_a_PART2.png")
lcontre_a_PART1.x= 320
lcontre_a_PART2.y= 40
lcontre_a_PART2.x=lcontre_a_PART1.x -40
lcontre_a_PART2.y=lcontre_a_PART2.y +80
physics.addBody( lcontre_a_PART1, "static", { density=0, friction=0, bounce=0,} )
lcontre_a_PART1.isFixedRotation = true
physics.addBody( lcontre_a_PART2, "static", { density=0, friction=0, bounce=0,} )
lcontre_a_PART2.isFixedRotation = true
local function moveObject( event )
local t = event.target
local phase = event.phase
if "began" == phase then
t.bodyType = "dynamic"
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store touch position on object
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 then
t.bodyType = "static"
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
lcontre_a_PART1:addEventListener("touch",moveObject)
lcontre_a_PART2:addEventListener("touch",moveObject)
try this
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
| local lcontre_a_PART1 = display.newCircle(30,30,15)
local lcontre_a_PART2 = display.newCircle(90,90,15)
local function moveObject( event )
local t = event.target
local phase = event.phase
if "began" == phase then
t.bodyType = "dynamic"
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store touch position on object
t.x0 = event.x - lcontre_a_PART1.x
t.y0 = event.y - lcontre_a_PART1.y
t.x1 = event.x - lcontre_a_PART2.x
t.y1 = event.y - lcontre_a_PART2.y
elseif t.isFocus then
if "moved" == phase then
lcontre_a_PART1.x = event.x - t.x0
lcontre_a_PART1.y = event.y - t.y0
lcontre_a_PART2.x = event.x - t.x1
lcontre_a_PART2.y = event.y - t.y1
elseif "ended" == phase then
t.bodyType = "static"
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
lcontre_a_PART1:addEventListener("touch",moveObject)
lcontre_a_PART2:addEventListener("touch",moveObject) |
thank you very much
... and if I had another pair of objects that move together, but unlike the other?
write separate function for those 2.
or else you may try this.
i passed the pair of each object with that object itself.
then u can use the same function for all object pairs.
but this will work if each object have only 1 paired object.
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
| local lcontre_a_PART1 = display.newCircle(30,30,15)
local lcontre_a_PART2 = display.newCircle(90,90,15)
lcontre_a_PART1.pair = lcontre_a_PART2
lcontre_a_PART2.pair = lcontre_a_PART1
local function moveObject( event )
local t = event.target
local phase = event.phase
if "began" == phase then
t.bodyType = "dynamic"
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
t.isFocus = true
-- Store touch position on object
t.x0 = event.x - t.x
t.y0 = event.y - t.y
t.x1 = event.x - t.pair.x
t.y1 = event.y - t.pair.y
elseif t.isFocus then
if "moved" == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
t.pair.x = event.x - t.x1
t.pair.y = event.y - t.y1
elseif "ended" == phase then
t.bodyType = "static"
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
lcontre_a_PART1:addEventListener("touch",moveObject)
lcontre_a_PART2:addEventListener("touch",moveObject) |
views:1425 update:2011/10/16 9:47:44