Here is a simple ball and paddle example for new coders. Slide your finger right/left to move paddle.
displayMode(FULLSCREEN) -- set full screen mode
function setup()
-- create screen edge
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT)) -- left
e2=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT)) -- top
e3=physics.body(EDGE,vec2(WIDTH,HEIGHT),vec2(WIDTH,0)) -- right
e4=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0)) -- bottom
-- create multiple balls
ballTable={} -- ball table
for z=1,6 do -- create 6 balls
b=physics.body(CIRCLE,20) -- create a circle physics body
b.position=vec2(math.random(WIDTH),math.random(HEIGHT)) -- random x,y
b.linearVelocity=vec2(500,400) -- set initial speed
b.gravityScale=0 -- turn off gravity for balls
b.restitution=1 -- set bouncyness of balls
b.friction=0 -- set friction to 0
table.insert(ballTable,b) -- put ball info ball table
end
-- create paddle with a size 100 x 10
p=physics.body(POLYGON,vec2(-50,5),vec2(50,5),vec2(50,-5),vec2(-50,-5))
p.position=vec2(WIDTH/2,HEIGHT/2) -- set initial x,y position
p.gravityScale=0 -- turn off gravity for paddle
p.type=STATIC -- set so paddle won't react to collision
end
function draw()
background(40, 40, 50) -- clear screen to defined color
fill(255) -- white fill color for balls
noStroke() -- don't draw outline around ball or paddle
for nbr,ball in pairs(ballTable) do -- loop thru ball table
ellipse(ball.x,ball.y,40) -- draw balls at x,y position
end
fill(255,0,0) -- red fill color for paddle
p.x=CurrentTouch.x -- paddle position from touch
rect(p.x-50,p.y-5,100,10) -- draw paddle
end