help with code

first question, how do i comment out multiple lines? say i have a bad function and want to comment out the whole thing without doing – before every line

second question, how do i paste code into quotes?

third question, what does for k.touch in pairs(touches) do mean? what is the k? what is in pairs?

fourth question, how often does function touched run? does it wait for everything in the code to have been executed once before never running again? or something different?

fifth question, why does my code below print collided a second time after i begin a touch after it hits the top edge (so when it hits the bottom edge, it has printed THREE times)

here is my code below. it is based on tokdog’s simple physics code in his collection

function setup()
    ball = physics.body(CIRCLE, 50)
    PlatForm = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
    PlatForm2 = physics.body(EDGE, vec2(0, 0), vec2(WIDTH, 0))
    ball.y = 250
    ball.x = WIDTH/2
    ball.gravityScale = -5
    ball.restitution = 0
    touches = {}
end

function draw()
    background(0, 0, 0, 255)
    fill(255, 200, 50, 255)
    ellipse(ball.x, ball.y, 50)
        for k,touch in pairs(touches) do
        ball.gravityScale = -ball.gravityScale
    end
end

function collide(c)
    print("Collided!")
end

function touched(touch)
    if touch.state == BEGAN then
        ball.gravityScale = 5
    elseif touch.state == ENDED then
        ball.gravityScale = -5
        touches[touch.id] = nil
    end
end

how do i comment out multiple lines?

put --[[ on the line above the start, and --]] on the line below the end

--[[
   any code in here
   is commented out
--]]

how do i paste code into quotes?

put ~~~ on a blank line before the code, and the same agian on a line after the code

what does for k.touch in pairs(touches) do mean? what is the k? what is in pairs?

google Lua for loop

how often does function touched run? does it wait for everything in the code to have been executed once before never running again? or something different?

https://coolcodea.wordpress.com/2014/12/28/188-understanding-touch/

why does my code below print collided a second time after i begin a touch after it hits the top edge (so when it hits the bottom edge, it has printed THREE times)

sorry, this question isn’t clear

@Ignatz, the fourth question: It prints Collided! 3 times after a touch

ok thanks guys! :slight_smile: sorry for mispelling your tokout at least i got the character number right.

~~ how would you make it so the gravityScale switched between values when i touched the screen? ~~ I had the touches ended set to 5 oops it worked fine!

sixth question, the ball gets stuck on the edges if you leave it there for about a second. why?

seventh question, how can i print collided with top edge or collided with bottom edge?

eight question, i want to print how fast and what direction it was going when it collided, i think thats called velocity?

The code doesn’t really make any sense to me, I don’t why you’d write it like this.

im a big newbie. really thats it, im pasting bits of code from everywhere and hoping i start understanding if, or someone explains this to me.

edit: oh i think you meant why it was i used — instead of ~~~. sorry i fixed it now!

@xThomas I modified your code a little. Look this over and try to understand whats happening so you can learn from it. Looking at others code is one way to learn.

function setup()
    PlatForm = physics.body(EDGE, vec2(0, HEIGHT), vec2(WIDTH, HEIGHT))
    PlatForm2 = physics.body(EDGE, vec2(0, 0), vec2(WIDTH, 0))
    ball = physics.body(CIRCLE, 25)
    ball.y = HEIGHT-100
    ball.x = WIDTH/2
    ball.restitution =.9
    physics.continuous=true
    str="touch screen to reverse gravity"
    bcount=0
    ecount=0
end

function draw()
    background(0, 0, 0, 255)
    fill(255, 200, 50, 255)
    ellipse(ball.x, ball.y, 50)
    text(str,WIDTH/2,HEIGHT/2)
end

function collide(c)    
    if c.state==BEGAN then
        bcount=bcount+1
        print("collide began",bcount)
    end
    if c.state==ENDED then
        ecount=ecount+1
        print("collide ended",ecount)
    end
end

function touched(touch)
    if touch.state == BEGAN then
        ball.gravityScale = -1
        str="gravity reversed (up)"
    elseif touch.state == ENDED then
        ball.gravityScale = 1
        str="gravity normal (down)"
    end
end