Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Codea Wiki tutorial #3 fix

edited April 2013 in Questions Posts: 10

I'm trying to make a simple game in codea and looking at the tutorial (under getting started) I've recreated the ball example that has the circle bounce around the screen. However, when the user flicks the ball near the edge of the screen the ball gets stuck to the side of the screen. Is there a way to fix this?

Here is the code from the tutorial that I need in my program:

function setup()
    x = WIDTH/2
    y = HEIGHT/2
    bc = color(248, 250, 13, 255)
    tc = color(195, 17, 17, 255)
    c = bc
    inTouch = false
    dx = 0
    dy = 0
    r = 30
end

function draw()
    background(0, 0, 0, 255)
    fill(c)
    ellipse(x,y,r)
    if inTouch then
        x = tx
        y = ty
        c = tc
    else
        x = x +  dx
        y = y +  dy
        c = bc
    end
    if y > HEIGHT - r then
        dy = - dy
    end
    if y < r then
        dy = - dy
    end
    if x > WIDTH - r then
        dx = - dx
    end
    if x < r then
        dx = - dx
    end
end

function touched(touch)
    tx = touch.x
    ty = touch.y
    if touch.state == BEGAN then
        inTouch = true
        velocity = {}
    end
    if touch.state == MOVING then
        table.insert(velocity,1,vec2(touch.deltaX,touch.deltaY))
    end
    if touch.state == ENDED then
        dx = 0
        dy = 0
        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 = dx / n
        dy = dy / n
        end
        inTouch = false
    end
end

Comments

  • BriarfoxBriarfox Mod
    Posts: 1,542

    Please add ~~~ at the beginning and end of your code.

  • Posts: 2,161

    (@Briarfox: I've edited that in now, @evanlws12 see how I've edited your post for how to include code so that it is readable.)

  • Posts: 489

    Hello @evanlws12. How about this (not tested):

    ...
    if y > HEIGHT - r then
        dy = - dy
        y = HEIGHT - r
    end
    if y < r then
        dy = - dy
        y = r
    end
    if x > WIDTH - r then
        dx = - dx
        x = WIDTH - r
    end
    if x < r then
        dx = - dx
        x = r
    end
    ...
  • IgnatzIgnatz Mod
    edited April 2013 Posts: 5,396

    I have a tutorial on that here

    https://coolcodea.wordpress.com/2013/03/11/3-moving-an-object-around-the-screen/

    Followed by more tutorials on how to use physics to do the same thing

  • Posts: 10

    Thanks everybody for the help, those tutorials will really help me in the future but whenever I implement these techniques to use the touch function, the ball gets stuck again! I still don't know how to make sure it doesn't get stuck on the sides

  • IgnatzIgnatz Mod
    Posts: 5,396

    mpilgrem's code fixes the problem, I believe

  • Posts: 10

    It does! Sorry I think I had put it in the touch function by accident instead of the draw but I tried it again and it works, thank you.

Sign In or Register to comment.