Multiple object Drag

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

1
2
3
4
5
6
7
8
9
10
11
12
13
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

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

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

Here is the full code so you can take a look..

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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)

It would be more like:

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
45
46
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

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

My truck problems is fuel related. Its either a clogged fuel filter, bad fuel sending unit or a bad fuel pump.

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

1
2
3
4
5
  elseif "ended" == phase or "cancelled" == phase then
        
          if (hitTestObjects(t, hitspotL) == true) or (hitTestObjects(t, hitspotR) == true) then
                         
                               print("target hit") 

I thought you might want to do that.

1
2
3
4
5
6
7
8
9
10
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") 

Thank you so much Rob! You are the man!

Now How can I tell print a statement telling me that both wings have been placed in each hit spot?

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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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  
                   

I want to give the player the feeling wings are being attached

I THINK I SOLVED IT!! give me two shakes of a lames tail to mess around.

I SOLVED IT!! CHECK IT OUT ROB!!

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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)
views:1731 update:2011/10/4 8:06:35
corona forums © 2003-2011