Joystick like behaviour without groups? Plug and Play.

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
local physics = require "physics"
physics.setDrawMode( "hybrid" )
physics.start( ) 
physics.setGravity( 0, 2)
 
local shape = display.newRect( 0, 0, 50, 50 )
shape.isVisible = false
 
local touchArea = display.newRect( 70, 200, 150, 150 )
touchArea.alpha = 0.3
 
function move( event )
        
        local phase = event.phase
        local T     = event.target  
        
        if "began" == phase then
                
                physics.addBody (shape, "dynamic", { friction = 5, bounce = 0, density = 5 }) 
                shape.isVisible = true
                
                display.getCurrentStage():setFocus( T )
                T.isFocus = true
                shape.isVisible = true
 
                shape.x = event.x --ycoord where touch is
                shape.y = event.y --ycoord is where touch is    
                shape.tempJoint = physics.newJoint( "touch", shape, shape.x, shape.y )
                shape.tempJoint.frequency = 2
                shape.tempJoint.maxForce = 10000
                                
        elseif T.isFocus then
    
                if "moved" == phase then
                                
                        shape.tempJoint:setTarget( event.x, event.y )
                                                        
                elseif "ended" == phase or "cancelled" == phase then
                        
                        display.getCurrentStage():setFocus( nil )            
                        shape.tempJoint:removeSelf()
        
                end
                
        end
                
end
 
touchArea:addEventListener("touch", move)

Can you read? "without groups".

Edit:

I've also tried wrapping the moved phase code in:

1
2
3
4
5
6
7
8
if shape.x > touchArea.x - touchArea.height / 2 and shape.x < touchArea.x + touchArea.height / 2 then
 
        if shape.y > touchArea.y - touchArea.height / 2 and shape.y < touchArea.y - touchArea.height / 2 then
 
                shape.tempJoint:setTarget( event.x, event.y )
                
        end
end

here:

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
local physics = require "physics"
physics.setDrawMode( "hybrid" )
physics.start( )
physics.setGravity( 0, 2)
 
local shape = display.newRect( 0, 0, 50, 50 )
shape.isVisible = false
 
local touchArea = display.newRect( 70, 200, 150, 150 )
touchArea.alpha = 0.3
 
local function setObjectBounds(oObject)
 
      local tBounds =  oObject.contentBounds
      oObject.xMin =  tBounds.xMin
      oObject.yMin =  tBounds.yMin --+mlText.rowPixelHeight
      oObject.xMax =  tBounds.xMax
      oObject.yMax =  tBounds.yMax --+mlText.rowPixelHeight
      oObject.objectWidth =  tBounds.xMax -  tBounds.xMin
      oObject.objectHeight =  tBounds.yMax -  tBounds.yMin
 
      tBounds = nil
      return oObject
end
touchArea = setObjectBounds(touchArea)
 
 
function move( event )
 
 
 
        local phase = event.phase
        local T     = event.target
 
        if "began" == phase then
 
                physics.addBody (shape, "dynamic", { friction = 5, bounce = 0, density = 5 })
                shape.isVisible = true
 
                display.getCurrentStage():setFocus( T )
                T.isFocus = true
                shape.isVisible = true
 
                shape.x = event.x --ycoord where touch is
                shape.y = event.y --ycoord is where touch is
                shape.tempJoint = physics.newJoint( "touch", shape, shape.x, shape.y )
                shape.tempJoint.frequency = 2
                shape.tempJoint.maxForce = 10000
 
        elseif T.isFocus then
 
                if "moved" == phase then
                        local tX = event.x
                        local tY = event.y
                        if tX> touchArea.xMax then tX = touchArea.xMax end
                        if tY> touchArea.yMax then tY = touchArea.yMax end
                        if tX< touchArea.xMin then tX = touchArea.xMin end
                        if tY< touchArea.yMin then tY = touchArea.yMin end
 
 
                        shape.tempJoint:setTarget(tX, tY)
 
                elseif "ended" == phase or "cancelled" == phase then
 
                        display.getCurrentStage():setFocus( nil )
                        shape.tempJoint:removeSelf()
 
                end
 
        end
 
end
 
touchArea:addEventListener("touch", move)
views:1719 update:2011/10/4 8:06:35
corona forums © 2003-2011