—UPDATED!! Sorry, I was wrong, the code is not working. Anyone have any ideas on how I can fix my code below so I know when my ball stops moving? Thank you =)
[code]
–Who cares how much battery they have left, let’s hide it
display.setStatusBar( display.HiddenStatusBar )
–Cannot forget those external files and make sure they are in the folder with your main.lua
local movieclip = require(“movieclip”);
local physics = require(“physics”);
–If the ball is going to bounce off the walls you will need some physics
physics.start()
–No gravity means it will not drop to the floor.
physics.setGravity(0,0)
–Create the walls
local wallThickness = 10
– Left wall
local wall = display.newRect( 0, 0, wallThickness, display.contentHeight )
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Top wall
wall = display.newRect(0,0, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Right wall
wall = display.newRect(display.contentWidth - wallThickness, 0, wallThickness, display.contentHeight)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
– Bottom wall
wall = display.newRect(0, display.contentHeight - wallThickness, display.contentWidth, wallThickness)
physics.addBody(wall, “static”, {friction=0, bounce = 1})
–Create the ball
function newBall()
ball = movieclip.newAnim({ “ball_blue.png”, “ball_green.png”, “ball_red.png”, “ball_pink.png” })
ball:play{ startFrame=1, endFrame=1, loop=0, remove=true }
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2
physics.addBody( ball, { density=0.9, friction=0.3, bounce=0} )
ball:setLinearVelocity(30,50)
end
–To test if our velocity listener is working we need to stop the ball
function stopball()
print(“ball stopped Function run”)
ball:setLinearVelocity(0,0)
end
–Our Velocity Tracker to see when the ball has stopped
enterFrame = new function VelocityTracker()
local vx,vy = event.target:getLinearVelocity()
if vx == 0 and vy == 0 then
print(“Yes, the ball stopped!! Win!”)
end
end
–Let’s create our ball now
newBall()
–add the listener
ball:addEventListener( “enterFrame”, ball)
–After 3000 miliseconds we will stop our ball
local ballstop = timer.performWithDelay( 3000, stopball, 1 )
–VIOLA!!! It works!!!
[/code] [import]uid: 76665 topic_id: 12584 reply_id: 46167[/import]