problem with drag physics

Hello,

Well i have applied dragging physics to my game. when i start it up, it already shows a runtime error in the terminal, and after i drag an object it shows the same thing. my question is, could it be my code for the drag physics? here is the code i am using for the dragging.

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
local image = display.newImage( "flowerpiece1.png", 5, 420 )
localGroup:insert(image)
 
local function drag ( event )
local image = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( image )
image.x0 = event.x - image.x
image.y0 = event.y - image.y
event.target.bodyType = "kinematic"
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0 
else  
        if "moved" == phase then  
            image.x = event.x - image.x0  
            image.y = event.y - image.y0  
        elseif "ended" == phase or "cancelled" == phase then  
            display.getCurrentStage():setFocus( nil )  
            event.target.bodyType = "dynamic" 
            image:setLinearVelocity(speedX, speedY)
            end  
    end  
  
    return true  
end  
image:addEventListener("touch", drag)
local speedX = 0  
local speedY = 0  
local prevTime = 0  
local prevX = 0  
local prevY = 0  
  
function trackVelocity(event)  
    local timePassed = event.time - prevTime  
    prevTime = prevTime + timePassed  
  
    speedX = (image.x - prevX)/(timePassed/1000)  
    speedY = (image.y - prevY)/(timePassed/1000)  
  
    prevX = image.x  
    prevY = image.y  
end  
  
Runtime:addEventListener("enterFrame", trackVelocity)  

you should have to add body into the physics

physics.addBody(image,"dynamic") also you need to start the physics

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
        local physics = require "physics"
        physics.start()
        local speedX = 0  
        local speedY = 0  
        local prevTime = 0  
        local prevX = 0  
        local prevY = 0  
        
        local image = display.newRect( 100,100,300,300 )
        physics.addBody(image,"dynamic")
        localGroup:insert(image)
 
        local function drag ( event )
        local image = event.target
        local phase = event.phase
        if "began" == phase then
                display.getCurrentStage():setFocus( image )
                image.x0 = event.x - image.x
                image.y0 = event.y - image.y
                event.target.bodyType = "static"
                event.target:setLinearVelocity( 5, 5 )
                event.target.angularVelocity = 0 
        elseif "moved" == phase then  
                    image.x = event.x - image.x0  
                    image.y = event.y - image.y0  
    elseif "ended" == phase or "cancelled" == phase then  
                    display.getCurrentStage():setFocus( nil )  
                    event.target.bodyType = "dynamic" 
                    image:setLinearVelocity(speedX, speedY)
            end  
          
            return true  
        end  
        image:addEventListener("touch", drag)
        
          
        function trackVelocity(event)  
            local timePassed = event.time - prevTime  
            prevTime = prevTime + timePassed  
          
            speedX = (image.x - prevX)/(timePassed/1000)  
            speedY = (image.y - prevY)/(timePassed/1000)  
          
            prevX = image.x  
            prevY = image.y  
        end  
          
        Runtime:addEventListener("enterFrame", trackVelocity)  
        
end

hello,

i tried that and it still comes up as an error. also, now they all just fall to the ground like there is gravity?

if you dont want them to fall down you can set gravity to 0 by
physics.setGravity(0,0)

or set the body to kinematic or static

or put ground as static body

okay i got them to stay in one place. it still says runtime error though. could it be another lua file?

You know it's really helpful for others to help you if you post the error you are getting as well. Just looking at your long code will just turn lot of other helpful users away.

here is the error i get:

Justin/Desktop/Brainy Puzzles/flower.lua:63>
?: in function <?:214>
Runtime error
/Users/Justin/Desktop/Brainy Puzzles/flower.lua:67: attempt to perform arithmetic on field 'x' (a nil value)
stack traceback:
[C]: ?

views:1359 update:2011/10/1 9:04:19
corona forums © 2003-2011