I am new to this programming stuff (except HTML lol)

I was following the tutorial spin regards to the circle on the screen and moving it around. I got the fairly easily.

I started on the even more advanced section which starts to add velocitys but the code won’t run. If anyone would mind having a loom through and see where I have made an error it would be greatly appreciated.

-- ball tutorial
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
x, y = wid / 2, HEIGHT / 2
bc = color(248,250,13)
tc = color(195,17,17)
inTouch = false
dx, dy = 0, 0
r = 30
end
-- This function gets called once every frame
function draw()
background(0)
local c
if inTouch then
x, y = tx, ty
c = tc
else
x, y = x + dx, y + dy
c = bc
if x < r then
dx = - dx
x = 2 * r - x
r = (r - x)
end
if x > r then
dy = - dy
y = 2 * r - y
end
if y > height - r then
dy = - dy
y = 2 * (HEIGHT - r) - y
end
end
fill(c)
ellipse(x, y, 2 * r)
end
function touched(touch)
tx, ty = touch.x, touch.y
if touch.state == BEGAN then
inTouch = true
velocity = {}
dx, dy = 0, 0
end
if touch.state == MOVING then
local newvelocity = vec2(touch.deltaX, touch.deltaY)
table.insert(velocity, 1, newvelocity)
end
if touch.state == ENDED then
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
dx = dx + velocity[i].x
dy = dy + velocity[i].y
end
end
if n > 0 then
dx, dy = dx / n, dy / n
end
inTouch = false
end
end

Apologies if the code is written quite scruffily, I’m new as I said

@Cjohnstone87 - that’s ok, being new is something we’ve all been through. You are welcome to post your problems and questions.

When you include code, it will format nicely if you put three ~ on the line before the code, and three more at the end. I edited your post to show you.

I can see a problem right at the start, with this line

x, y = wid / 2, HEIGHT / 2

because “wid” is not defined. You probably meant to use WIDTH.

Ok, the program runs now but the boundaries are not working properly.

-- ball tutorial
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
x, y = WIDTH / 2, HEIGHT / 2
bc = color(248,250,13)
tc = color(195,17,17)
inTouch = false
dx, dy = 0, 0
r = 30
end
-- This function gets called once every frame
function draw()
background(0)
local c
if inTouch then
x, y = tx, ty
c = tc
else
x, y = x + dx, y + dy
c = bc
if x < r then
dx = - dx
x = 2 * r - x
r = (r - x)
end
if x > r then
dy = - dy
y = 2 * r - y
end
if y > HEIGHT - r then
dy = - dy
y = 2 * (HEIGHT - r) - y
end
end
fill(c)
ellipse(x, y, 2 * r)
end
function touched(touch)
tx, ty = touch.x, touch.y
if touch.state == BEGAN then
inTouch = true
velocity = {}
dx, dy = 0, 0
end
if touch.state == MOVING then
local newvelocity = vec2(touch.deltaX, touch.deltaY)
table.insert(velocity, 1, newvelocity)
end
if touch.state == ENDED then
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
dx = dx + velocity[i].x
dy = dy + velocity[i].y
end
end
if n > 0 then
dx, dy = dx / n, dy / n
end
inTouch = false
end
end

Ok, I’ve come up with this mad it works for top,bottom and left.
Right is still broken as it’s missing and I can’t figure it

background(0)
local c
if inTouch then
x, y = tx, ty
c = tc
else
x, y = x + dx, y + dy
c = bc
end
if x < r then
dx = - dx
x = 2 * r - x
end
if x > r then
dy = - dy
y = 2 * r - y
end

I tried this for the missing right hand side but didn’t work

if y < HEIGHT - r then
dx = - dx
y = 2 * (HEIGHT - r) - y
end

But the ball won’t even appear with that string in

-- ball tutorial
supportedOrientations(LANDSCAPE_ANY)
-- Use this function to perform your initial setup
function setup()
x, y = WIDTH / 2, HEIGHT / 2
bc = color(248,250,13)
tc = color(195,17,17)
inTouch = false
dx, dy = 0, 0
r = 30
end
-- This function gets called once every frame
function draw()
background(0)
local c
if inTouch then
x, y = tx, ty
c = tc
else
x, y = x + dx, y + dy
c = bc
end
if x < r then
dx = - dx
x = 2 * r - x
end
if x > WIDTH - r then
dx = - dx
x = 2 * (WIDTH - r) - x
end
if y < r then
dy = - dy
y = 2 * r - y
end
if y > HEIGHT - r then
dy = - dy
y = 2 * (HEIGHT - r) - y
end
fill(c)
ellipse(x, y, 2 * r)
end
function touched(touch)
tx, ty = touch.x, touch.y
if touch.state == BEGAN then
inTouch = true
velocity = {}
dx, dy = 0, 0
end
if touch.state == MOVING then
local newvelocity = vec2(touch.deltaX, touch.deltaY)
table.insert(velocity, 1, newvelocity)
end
if touch.state == ENDED then
local n = 0
for i = 1, 10 do
if velocity[i] then
n = n + 1
dx = dx + velocity[i].x
dy = dy + velocity[i].y
end
end
if n > 0 then
dx, dy = dx / n, dy / n
end
inTouch = false
end
end