Multiple object Drag

I have to objects that go in to different hitSpots (left wing and right wing to a rocket ship)

[lua]local wingl = display.newImageRect(“wingL.png”, 67, 67)
wingl.x = 100; wingl.y = 360

local wingr = display.newImageRect(“wingR.png”, 67, 67)
wingr.x = 200; wingr.y = 360

local hitspotR = display.newImageRect(“hitSpot.png”, 44, 74)
hitspotR.x = 183; hitspotR.y = 690
hitspotR.alpha = .3

local hitspotL = display.newImageRect(“hitSpot.png”, 44, 74)
hitspotL.x = 120; hitspotL.y = 690
hitspotL.alpha = .3[/lua]
What I am having trouble with is getting the right wing to stay at its hit spot when the mouse is released… Here is the code I written to make the left wing work…

[lua]function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end

local function dragWing(_e)
local t = _e.target

local phase = _e.phase
if _e.phase == “began” then

local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

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

t.x = _e.x - t.x0
t.y = _e.y - t.y0

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

if hitTestObjects(t, hitspotL) == true then

display.getCurrentStage():setFocus( nil )
t.isFocus = false

else
print ( “missed target” )
wingl.x = leftX
wingl.y = leftY

wingr.x = rightX
wingr.y = rightY
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end

return true

end[/lua]

If anybody can point my in the right direction that would be AWESOME!!

Thanks [import]uid: 51459 topic_id: 15147 reply_id: 315147[/import]

Where are leftX, leftY, rightX and rightY defined?

What code are you using for the right wing? This code will only test the left hitspot:

if hitTestObjects(t, hitspotL ) == true then

[import]uid: 19626 topic_id: 15147 reply_id: 56046[/import]

I defined leftX , leftY, rightX, and rightY on top. For the right wing I have a hitSpotR.

should I then wright something like this if hitTestObjects(t, hitspotL, hitspotR) == true then [import]uid: 51459 topic_id: 15147 reply_id: 56050[/import]

Here is the full code so you can take a look… [lua]local leftX = 100
local leftY = 360

local rightX = 200
local rightY = 360

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2
local body = display.newImageRect(“Body.png”, 46, 149)
body.x = 150; body.y = 650

local planet = display.newImageRect(“Planet.png”, 114, 114)
planet.x = 150; planet.y = 150

local wingl = display.newImageRect(“wingL.png”, 67, 67)
wingl.x = 100; wingl.y = 360

local wingr = display.newImageRect(“wingR.png”, 67, 67)
wingr.x = 200; wingr.y = 360

local hitspotR = display.newImageRect(“hitSpot.png”, 44, 74)
hitspotR.x = 183; hitspotR.y = 690
hitspotR.alpha = .3

local hitspotL = display.newImageRect(“hitSpot.png”, 44, 74)
hitspotL.x = 120; hitspotL.y = 690
hitspotL.alpha = .3
function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end

local function dragWing(_e)
local t = _e.target

local phase = _e.phase
if _e.phase == “began” then

local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

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

t.x = _e.x - t.x0
t.y = _e.y - t.y0

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

if hitTestObjects(t, hitspotL) == true then

display.getCurrentStage():setFocus( nil )
t.isFocus = false

else
print ( “missed target” )
wingl.x = leftX
wingl.y = leftY

wingr.x = rightX
wingr.y = rightY
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end

return true

end

wingl:addEventListener(“touch”, dragWing)
wingr:addEventListener(“touch”, dragWing)[/lua] [import]uid: 51459 topic_id: 15147 reply_id: 56051[/import]

It would be more like:

[lua]local function dragWing(_e)
local t = _e.target

local phase = _e.phase
if _e.phase == “began” then

local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

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

t.x = _e.x - t.x0
t.y = _e.y - t.y0

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

if (hitTestObjects(t, hitspotL) == true) or (hitTestObjects(t, hitspotR) == true) then

display.getCurrentStage():setFocus( nil )
t.isFocus = false

else
print ( “missed target” )
wingl.x = leftX
wingl.y = leftY

wingr.x = rightX
wingr.y = rightY
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end

return true

end[/lua] [import]uid: 19626 topic_id: 15147 reply_id: 56056[/import]

Thank you so much Rob!!! You are the best!! Good luck with your truck.

My truck wouldn’t start a couple weeks back because the battery was to dirty and it didn’t recognize the connection.

Thanks!

Jake [import]uid: 51459 topic_id: 15147 reply_id: 56066[/import]

My truck problems is fuel related. Its either a clogged fuel filter, bad fuel sending unit or a bad fuel pump.
[import]uid: 19626 topic_id: 15147 reply_id: 56079[/import]

Hey Rob how can you help me with a print statement telling if the right wing has been “repaired” or left wing

In this area [lua] elseif “ended” == phase or “cancelled” == phase then

if (hitTestObjects(t, hitspotL) == true) or (hitTestObjects(t, hitspotR) == true) then

print(“target hit”) [/lua]

Right now its tracking both. I want to separate it so that if I want to remove the wing when it has been placed in the hit spot

Thanks [import]uid: 51459 topic_id: 15147 reply_id: 56108[/import]

I thought you might want to do that.

local hitLeft = hitTestObjects(t, hitspotL)  
local hitRight = hitTestObjects(t, hitspotR)  
  
if (hitLeft == true) or (hitRight == true) then  
 if hitLeft then  
 print("left")  
 else  
 print("right")  
 end  
 print("target hit")   
  

[import]uid: 19626 topic_id: 15147 reply_id: 56110[/import]

Thank you so much Rob! You are the man! [import]uid: 51459 topic_id: 15147 reply_id: 56115[/import]

Now How can I tell print a statement telling me that both wings have been placed in each hit spot? [import]uid: 51459 topic_id: 15147 reply_id: 56215[/import]

Like for instance if I place the right wing its hit spot and then the left wing in its hit spot I want a print statement telling me that the rocket has been fixed… So that I can replace the body and the wings with a put together rocket ship… [import]uid: 51459 topic_id: 15147 reply_id: 56216[/import]

[lua]local hitLeft = hitTestObjects(t, hitspotL)
local hitRight = hitTestObjects(t, hitspotR)

if (hitLeft == true) or (hitRight == true) then
if hitLeft then
print(“left”)
else
print(“right”)
end
print(“target hit”)

if hitLeft and hitRight then
print(“both wings in place”)
end

end [/lua] [import]uid: 19626 topic_id: 15147 reply_id: 56221[/import]

How come it doesn’t work when I add a removeSelf to it?

[lua]if (hitLeft == true) or (hitRight == true) then
if hitLeft then
print(“left”)
wingl:removeSelf()
–rocketShip:play(“rocketShip leftWing”)
else
print(“right”)
wingr:removeSelf()
–rocketShip:play(“rocketShip rightWing”)
end

if hitLeft and hitRight then
–rocketShip:play(“rocketShip repairedShip”)
print(“both wings in place”)
end
[/lua]

is there any way around this? [import]uid: 51459 topic_id: 15147 reply_id: 56222[/import]

I want to give the player the feeling wings are being attached [import]uid: 51459 topic_id: 15147 reply_id: 56223[/import]

I THINK I SOLVED IT!! give me two shakes of a lames tail to mess around. [import]uid: 51459 topic_id: 15147 reply_id: 56224[/import]

I SOLVED IT!! CHECK IT OUT ROB!! [lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local loqsprite = require(‘loq_sprite’)

local leftX = 100
local leftY = 360

local rightX = 200
local rightY = 360

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

–[[
local body = display.newImageRect(“Body.png”, 46, 149)
body.x = 150; body.y = 650
]]

local rfactory = loqsprite.newFactory(“rocketShip”)
local rocketShip = rfactory:newSpriteGroup()
rocketShip.x = 30; rocketShip.y = 432
rocketShip:play(“rocketShip body”)

atrace(xinspect(rocketShip:getSpriteNames()))

local planet = display.newImageRect(“Planet.png”, 114, 114)
planet.x = 150; planet.y = 150

local wingl = display.newImageRect(“wingL.png”, 67, 67)
wingl.x = 100; wingl.y = 360

local fixL = display.newImageRect(“wingL.png”, 67, 67)
fixL.x = 132; fixL.y = 680
fixL.alpha = 0

local fixR = display.newImageRect(“wingR.png”, 67, 67)
fixR.x = 172; fixR.y = 680
fixR.alpha = 0

local wingr = display.newImageRect(“wingR.png”, 67, 67)
wingr.x = 200; wingr.y = 360

local hitspotR = display.newImageRect(“hitSpot.png”, 44, 74)
hitspotR.x = 183; hitspotR.y = 690
hitspotR.alpha = 0

local hitspotL = display.newImageRect(“hitSpot.png”, 44, 74)
hitspotL.x = 120; hitspotL.y = 690
hitspotL.alpha = 0

function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end

local function dragWing(_e)
local t = _e.target

local phase = _e.phase
if _e.phase == “began” then

local parent = t.parent

display.getCurrentStage():setFocus( t )

t.isFocus = true

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

t.x = _e.x - t.x0
t.y = _e.y - t.y0

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

local hitLeft = hitTestObjects(t, hitspotL)
local hitRight = hitTestObjects(t, hitspotR)

if (hitLeft == true) or (hitRight == true) then
if hitLeft then
print(“fixed left”)
wingl:removeSelf()
fixL.alpha = 1
else
print(“fixed right”)
wingr:removeSelf()
fixR.alpha = 1

end

if hitLeft and hitRight then

print(“both wings in place”)
end

display.getCurrentStage():setFocus( nil )
t.isFocus = false

else
print ( “missed target” )
wingl.x = leftX
wingl.y = leftY

wingr.x = rightX
wingr.y = rightY
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
end

return true

end

wingl:addEventListener(“touch”, dragWing)
wingr:addEventListener(“touch”, dragWing)[/lua] [import]uid: 51459 topic_id: 15147 reply_id: 56227[/import]