Having an issue with dragging

I’m trying to make a basketball that can bounce off the ground but can also be dragged. However, after adding a bunch of code from the DragPlatforms source code and making it applicable to the basketball, the screen on my simulator just shows black. Why is this? Here’s all the drag code I am using:

[code]
local function startDrag( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

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

event.target.bodyType = “kinematic”

event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0

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

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

event.target.bodyType = “dynamic”
end

end
end

return true
end

basketball:addEventListener( “touch”, startDrag ) [import]uid: 82408 topic_id: 14292 reply_id: 314292[/import]

just check your terminal window for error messages… [import]uid: 71210 topic_id: 14292 reply_id: 52798[/import]

I’ve figured out how to use terminal finally. I tried using terminal on the program that was having issues, and got this message: Syntax error: … ‘eof’ expected near ‘end’

I don’t entirely understand. I know that something is expected to be closer to the ‘end’ function, but what does the ‘eof’ mean?

P.S. ‘eof’ is in the < > brackets. [import]uid: 82408 topic_id: 14292 reply_id: 53252[/import]

I just replaced all my code with the drag code from “TimeAnimation”. It now works, but the ball is behaving erratically. I try dragging it and it begins to fall with gravity or fly upwards as though gravity was negative (though it somewhat stays with my touch while I’m moving the mouse). Perhaps it’s not entirely kinematic while I’m dragging? Here’s my drag code (it’s really long, just so you know):

[code]
function onMoveCircle(event)
local timePassed = event.time - lastTime
lastTime = lastTime + timePassed

speedY = speedY + gravity

basketball.x = basketball.x + speedX*timePassed
basketball.y = basketball.y + speedY*timePassed

if basketball.x >= screenW - basketball.width*.5 then
basketball.x = screenW - basketball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
elseif basketball.x <= basketball.width*.5 then
basketball.x = basketball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
elseif basketball.y >= screenH - basketball.height*.5 then
basketball.y = screenH - basketball.height*.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 --change direction
elseif basketball.y <= basketball.height*.5 then
basketball.y = basketball.height*.5
speedY = speedY*friction
speedY = speedY*-1 --change direction
end
end

– A general function for dragging objects
local function startDrag( event )
local t = event.target
local phase = event.phase

if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Stop current motion, if any
Runtime:removeEventListener(“enterFrame”, onMoveCircle)
– Start tracking velocity
Runtime:addEventListener(“enterFrame”, trackVelocity)

elseif t.isFocus then
if “moved” == phase then

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

elseif “ended” == phase or “cancelled” == phase then
lastTime = event.time

Runtime:removeEventListener(“enterFrame”, trackVelocity)
Runtime:addEventListener(“enterFrame”, onMoveCircle)

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

– Stop further propagation of touch event!
return true
end

function trackVelocity(event)
local timePassed = event.time - prevTime
prevTime = prevTime + timePassed

speedX = (basketball.x - prevX)/timePassed
speedY = (basketball.y - prevY)/timePassed

prevX = basketball.x
prevY = basketball.y
end

basketball:addEventListener( “touch”, startDrag )
Runtime:addEventListener( “enterFrame”, onMoveCircle ) [import]uid: 82408 topic_id: 14292 reply_id: 53256[/import]

cannot see the physics part of ur game… can I see the addbody code for the basketball ? [import]uid: 71210 topic_id: 14292 reply_id: 53308[/import]

Sure. I’ll give you everything before the drag code:

[code]
local physics = require(“physics”)
physics.start()
system.activate( “multitouch” )

basketball = display.newImage( “Basketball.png” )
basketball.x = display.contentWidth / 2; basketball.y = display.contentHeight / 2
basketball.width = 100; basketball.height = 100

physics.addBody( basketball, { density = 1, bounce = 0.5, radius = 45 } )

print( “basketball” )

ground = display.newImage( “sludge.tif” )
ground.x = display.contentWidth / 2; ground.y = 550

physics.addBody( ground, “static”, { density = 1, bounce = 0.1 } )

physics.setDrawMode( “hybrid” ) [import]uid: 82408 topic_id: 14292 reply_id: 53313[/import]

try putting
[lua]t.bodyType =“static” [/lua] in began phase and [lua]t.bodyType =“static” [/lua] in ended phase [import]uid: 71210 topic_id: 14292 reply_id: 53367[/import]

I have seen the code that you have posted… and it should work without using physics… just copy the below code and try running it
[lua]local screenW, screenH = display.stageWidth, display.stageHeight
local friction = 0.8
local gravity = .9
local speedX, speedY = 0, 0
local prevX, prevY = 0, 0

local ball = display.newCircle( 0, 0, 25)
ball:setFillColor(255, 255, 0)
ball.x = screenW*.5
ball.y = 20

function onMoveCircle(event)
speedY = speedY + gravity

ball.x = ball.x + speedX
ball.y = ball.y + speedY

if( ball.x >= screenW - ball.width*.5 ) then
ball.x = screenW - ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
elseif( ball.x <= ball.width*.5) then
ball.x = ball.width*.5
speedX = speedX*friction
speedX = speedX*-1 --change direction
end
if( ball.y >= screenH - ball.height*.5 ) then
ball.y = screenH - ball.height*.5
speedY = speedY*friction
speedX = speedX*friction
speedY = speedY*-1 --change direction
elseif( ball.y <= ball.height*.5 ) then
ball.y = ball.height*.5
speedY = speedY*friction
speedY = speedY*-1 --change direction
end
end

– A general function for dragging objects
local function startDrag( event )
local t = event.target

local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

– Stop current motion, if any
Runtime:removeEventListener(“enterFrame”, onMoveCircle)
– Start tracking velocity
Runtime:addEventListener(“enterFrame”, trackVelocity)

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

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

Runtime:removeEventListener(“enterFrame”, trackVelocity)
Runtime:addEventListener(“enterFrame”, onMoveCircle)

end
end

– Stop further propagation of touch event!
return true
end

function trackVelocity()
speedX = ball.x - prevX
speedY = ball.y - prevY

prevX = ball.x
prevY = ball.y
end

ball:addEventListener(“touch”, startDrag)
Runtime:addEventListener(“enterFrame”, onMoveCircle) [/lua] [import]uid: 71210 topic_id: 14292 reply_id: 53370[/import]

I figured it out. Using the t.bodyType things you gave me above, I was able to get it to work so that it would be static while I was dragging and dynamic when I let go. Thanks for your kind help! :wink: Hope a lot of people have someone like you to help them out when they need it. [import]uid: 82408 topic_id: 14292 reply_id: 53440[/import]