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.

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)   

or could it be something else in my coding? Thanks in advance!
[import]uid: 19836 topic_id: 9701 reply_id: 309701[/import]

you should have to add body into the physics

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

[lua] 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[/lua]
[import]uid: 12482 topic_id: 9701 reply_id: 35342[/import]

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? [import]uid: 19836 topic_id: 9701 reply_id: 35496[/import]

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 [import]uid: 12482 topic_id: 9701 reply_id: 35660[/import]

okay i got them to stay in one place. it still says runtime error though. could it be another lua file? [import]uid: 19836 topic_id: 9701 reply_id: 35784[/import]

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. [import]uid: 48521 topic_id: 9701 reply_id: 35816[/import]

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]: ?
[import]uid: 19836 topic_id: 9701 reply_id: 35997[/import]