DISREGARD bouncing ball sticking instead

I’m lost, I don’t suppose someone could help me understand how to adjust this code? I’ve taken the Time Animation template with the metal bg and bouncing ball and I’m trying to adjust it so that the ball enters the stage already moving, instead of having to be dragged. But instead of bouncing it is sticking to the wall :slight_smile: Thanks if you can help.

Kevin

[lua]local screenW, screenH = display.contentWidth, display.contentHeight
local friction = 1
local gravity = 0
local speedX, speedY, prevX, prevY, lastTime, prevTime = 0, 0, 0, 0, 0, 0

local background = display.newImage( “metal_bg.jpg”, true )
background.x = screenW / 2
background.y = screenH / 2

local ball = display.newCircle( 0, 0, 40)
ball:setFillColor(255, 255, 255, 166)
ball.x = screenW * 0.5
ball.y = ball.height

– this is the new code to get the ball moving when it enters frame –

local moveDirectionX= math.random(-2,2)
local moveDirectionY= math.random(-2,2)

function ball:enterFrame(event)
ball.x= ball.x+moveDirectionX
ball.y = ball.y+moveDirectionY

end

Runtime:addEventListener(“enterFrame”,ball)

– this is where the new code ends, everything else here is from the template –

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

speedY = speedY + gravity

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

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
elseif 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
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 = (ball.x - prevX)/timePassed
speedY = (ball.y - prevY)/timePassed

prevX = ball.x
prevY = ball.y
end

ball:addEventListener(“touch”, startDrag)
Runtime:addEventListener(“enterFrame”, onMoveCircle)
[lua] [import]uid: 96383 topic_id: 16481 reply_id: 316481[/import]

Please enclose the code between
[text][lua][/text] and [text][/lua][/text] tags
so that code can be read better! :slight_smile: [import]uid: 64174 topic_id: 16481 reply_id: 61512[/import]

Thanks, I’ve so enclosed (lots to learn, even forum posting is complicated :slight_smile:

My slightly fine tuned question is, how do I stop referencing ball.x and ball.y values in

[lua]function ball:enterFrame(event)[/lua]

and begin using the ball.x,y values in

[lua]function onMoveCircle(event) [/lua]

as soon as the ball reaches the edge of the screen? [import]uid: 96383 topic_id: 16481 reply_id: 61513[/import]