Help with tutorial

Hi,

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 :slight_smile:

Thanks in advance

@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.

Ahh thank you :slight_smile:

I also thnk there is a problem with the line r=r-x but I can’t figure out what

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

The ball does however bounce off the bottom

@Cjohnstone87 - the if statement in draw that starts

if inTouch then

has its end statement way down the bottom. Shouldn’t it be higher up?

You need four edge checks, two for x (left and right) and two for y (top and bottom). You only have 3, and one of them is faulty

if x < r then  --left

if x > r then  --????
        
if y > HEIGHT - r then  --top

--where is the bottom check?

Have a think about it…it’s important you understand the code you are writing

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 :frowning:

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

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

@Cjohnstone87 - I think you may have your checks the wrong way round.

x values should be left to right, and y values are up and down

So your 4 tests are (showing just the if test in each case)

    if x<r then  --left

    if x>WIDTH-r then  --right

    if y<r then  -- bottom

    if y>HEIGHT-r then --top

Thanks very much. Your tips helped a treat :slight_smile:

-- 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
    

Working fine now :slight_smile: onwards and upwards