Little help

I’m making my submission for the 100 line challenge, everything is done but I can’t get the controls right…

It’s a ship that dodges asteroids. When you touch and hold the left side of the screen the ship moves left, when you touch the right side of the scren the ship moves right… However I can’t get the ship to stop moving when you stop touching the screen.

Any idea what I can do to fix it? I tried to do if CurrentTouch.state==ENDED but it isn’t working.


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
    ast={}
    ship=readImage("Space Art:Red Ship")
    asteroid=readImage("Space Art:Asteroid Large")
    astFreq=.66
    astSpeed=-12
    score=0 -- starting score
    timer=0
    rectMode(CENTER)
    if readLocalData("HS")== nil then
        saveLocalData("HS", 0)
    end
    music("A Hero's Quest:Battle",true,.5)
    gameState=0
    font("Futura-CondensedExtraBold")
    fontSize(64)
    fill(0, 255, 0, 255)
    shipX=WIDTH/2
    shipY=HEIGHT/4
end
function spawn()
    table.insert(ast,vec2(math.random(96,WIDTH-96),1152))
end
function draw()
    if gameState==0 then
        background(0, 0, 0, 255)
        text("PLAY",WIDTH/2,HEIGHT/2)
        if CurrentTouch.state==BEGAN then
            if CurrentTouch.x>WIDTH/2-256 and CurrentTouch.x<WIDTH/2+256
            and CurrentTouch.y>HEIGHT/2-128 and CurrentTouch.y<HEIGHT/2+128 then
                gameState=1
            end
        end
    end
    if gameState==1 then
        background(0, 0, 0, 255)
        pushStyle()
        text("Score: "..score, WIDTH/2, HEIGHT/1.2)
        popStyle()
    for a,b in pairs(ast) do
        sprite(asteroid,b.x,b.y,192,256)
        b.y=b.y+astSpeed
    end
    timer=timer+DeltaTime
    if timer>astFreq then
        spawn()
        spawn()
        spawn()
        timer=0 
        end
    for a,b in pairs(ast) do
        if b.y<-128 then
            table.remove(ast,a)
            score=score+10
        end
    end
    if CurrentTouch.x>0 and CurrentTouch.x<WIDTH/2 then
            shipX=shipX-9
        end
    if CurrentTouch.x>WIDTH/2 and CurrentTouch.x<WIDTH then
            shipX=shipX+9
        end
    if CurrentTouch.state==ENDED then
            shipX=shipX
        end
    sprite(ship,shipX,shipY,64,96)
    for a,b in pairs(ast) do
            if b.y-96<=shipY+48 and b.y+160>=shipY-48 and
            b.x-96<=shipX+32 and b.x+96>=shipX-32 then
                gameState=2
            end
        end
    if shipX-32<0 or shipX+32>WIDTH then
            gameState=2
        end
    end
    if gameState==2 then
        background(0, 0, 0, 255)
        text("RETRY",WIDTH/2,HEIGHT/4)
        if score > readLocalData("HS") then
            saveLocalData("HS", score)
            end
        text("Score: "..score, WIDTH/2,HEIGHT/1.2)
        text("HighScore: "..readLocalData("HS"),WIDTH/2,HEIGHT/2)
        if CurrentTouch.state==BEGAN then
            if CurrentTouch.x>WIDTH/2-256 and CurrentTouch.x<WIDTH/2+256
            and CurrentTouch.y>HEIGHT/4-128 and CurrentTouch.y<HEIGHT/4+128 then
                score=0
                gameState=0
                shipX=WIDTH/2
                for i,t in pairs(ast) do ast[i]= nil
                end
            end
        end
    end
end

You needed to check if the touch’s state was not ENDED or CANCELLED in the two if statements for moving the ship. That last one doesn’t do anything. I also removed a total of 12 lines by putting all your variables on one line (it’s not a cheat, commas are a thing built in to Lua), if you want you can keep it or you can just not copy over that part. Either way it’s something new to some people, that they can learn from.


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
    ast, ship, asteroid, astFreq, astSpeed, score, timer, gameState, shipX, shipY={}, readImage("Space Art:Red Ship"), readImage("Space Art:Asteroid Large"), .66, -12, 0, 0, 0, WIDTH / 2, HEIGHT / 4
    rectMode(CENTER)
    if readLocalData("HS")== nil then
        saveLocalData("HS", 0)
    end
    music("A Hero's Quest:Battle",true,.5)
    font("Futura-CondensedExtraBold")
    fontSize(64)
    fill(0, 255, 0, 255)
end
function spawn()
    table.insert(ast,vec2(math.random(96,WIDTH-96),1152))
end
function draw()
    if gameState==0 then
        background(0, 0, 0, 255)
        text("PLAY",WIDTH/2,HEIGHT/2)
        if CurrentTouch.state==BEGAN then
            if CurrentTouch.x>WIDTH/2-256 and CurrentTouch.x<WIDTH/2+256
            and CurrentTouch.y>HEIGHT/2-128 and CurrentTouch.y<HEIGHT/2+128 then
                gameState=1
            end
        end
    end
    if gameState==1 then
        background(0, 0, 0, 255)
        pushStyle()
        text("Score: "..score, WIDTH/2, HEIGHT/1.2)
        popStyle()
    for a,b in pairs(ast) do
        sprite(asteroid,b.x,b.y,192,256)
        b.y=b.y+astSpeed
    end
    timer=timer+DeltaTime
    if timer>astFreq then
        spawn()
        spawn()
        spawn()
        timer=0 
        end
    for a,b in pairs(ast) do
        if b.y<-128 then
            table.remove(ast,a)
            score=score+10
        end
    end
    if CurrentTouch.x <= WIDTH / 2 and CurrentTouch.state ~= ENDED and CurrentTouch.state ~= CANCELLED then
            shipX=shipX-9
        end
    if CurrentTouch.x>WIDTH/2 and CurrentTouch.state ~= ENDED and CurrentTouch.state ~= CANCELLED then
            shipX=shipX+9
        end
    sprite(ship,shipX,shipY,64,96)
    for a,b in pairs(ast) do
            if b.y-96<=shipY+48 and b.y+160>=shipY-48 and
            b.x-96<=shipX+32 and b.x+96>=shipX-32 then
                gameState=2
            end
        end
    if shipX-32<0 or shipX+32>WIDTH then
            gameState=2
        end
    end
    if gameState==2 then
        background(0, 0, 0, 255)
        text("RETRY",WIDTH/2,HEIGHT/4)
        if score > readLocalData("HS") then
            saveLocalData("HS", score)
            end
        text("Score: "..score, WIDTH/2,HEIGHT/1.2)
        text("HighScore: "..readLocalData("HS"),WIDTH/2,HEIGHT/2)
        if CurrentTouch.state==BEGAN then
            if CurrentTouch.x>WIDTH/2-256 and CurrentTouch.x<WIDTH/2+256
            and CurrentTouch.y>HEIGHT/4-128 and CurrentTouch.y<HEIGHT/4+128 then
                score=0
                gameState=0
                shipX=WIDTH/2
                for i,t in pairs(ast) do ast[i]= nil
                end
            end
        end
    end
end

Awesome didn’t know you could do that with variables.

Is there any easy way to make a round collision box for the asteroids? The square collision box I have makes the ship collide on the corners of the invisible box.

Thanks for the help!

You could make all the positions stored as vec2s, and then use vec2:dist(vec2) to calculate the distance between them.