Help with my game...

So here’s all the code im using:


--# Main
-- Slider

supportedOrientations(PORTRAIT)
displayMode(OVERLAY)
displayMode(FULLSCREEN)

function setup()

    shipPosition = vec2(WIDTH/2,50)

    shipsprite = readImage("Space Art:Red Ship")

    shipspriteL = readImage("Space Art:Red Ship Bank Left")

    shipspriteR = readImage("Space Art:Red Ship Bank Right")

    bulletsprite = readImage("Tyrian Remastered:Bullet Fire C")

    enemysprite = {}
    enemysprite[1] = readImage("Tyrian Remastered:Space Ice 4")
    enemysprite[2] = readImage("Tyrian Remastered:Space Ice 9")
    enemysprite[3] = readImage("Tyrian Remastered:Plane Boss")

    dx = 0

    backingMode(RETAINED)

    bullets = {}
    lastBulletTime = 0
    enemies = {}
    lastEnemyTime = 0
    score = 0
    scoreStartTime = 0
end

function draw()
    fill(0, 0, 0, 75)
    rect(-1,-1,WIDTH+2,HEIGHT+2)

    if ElapsedTime-lastEnemyTime > 2-math.min(1,(ElapsedTime-scoreStartTime)/100) then

        makeEnemy()

    end

    for i,v in pairs(enemies) do

        if v.body.collided == true then

            v.body:destroy()

            table.remove(enemies,i)

            else

            if v.body.position.y < -v.body.radius then

                score = 0
                scoreStartTime = ElapsedTime

                elseif v.body.position.x < -v.body.radius or
                v.body.position.x > WIDTH + v.body.radius then

                v.body:destroy()

                table.remove(enemies,i)

            end
        end

    end

    for i,v in ipairs(enemies) do

        sprite(v.img,v.body.position.x,v.body.position.y)

    end

    if touch and ElapsedTime-lastBulletTime > 1 then

        makeBullet()

    end

    for i,v in pairs(bullets) do

        if v.collided == true then

            v:destroy()

            table.remove(bullets,i)

        end

    end

    for i,v in ipairs(bullets) do

        sprite("Tyrian Remastered:Energy Orb 2",v.position.x,v.position.y)

    end

    dx = Gravity.x * 15

    if math.abs(dx) < 1 then

        dx = 0

    end

    shipPosition.x = shipPosition.x + dx

    shipPosition.x = math.max(0,math.min(shipPosition.x,WIDTH))

    if dx == 0 then

        sprite(shipsprite,shipPosition.x,shipPosition.y)

        elseif dx > 0 then

        sprite(shipspriteR,shipPosition.x,shipPosition.y)

        else

        sprite(shipspriteL,shipPosition.x,shipPosition.y)

    end
    
       fill(255,255,255,255)
    textMode(CENTER)
    textAlign(CENTER)
    fontSize(20)
    font("Zapfino")
    text("Score: "..score,WIDTH/1.3,HEIGHT-20)

end
 


--# Mainscreen
-- Main Screen


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)        -- set for portrait mode

function setup()
    func=menu
end

function draw()
    func()
end

function menu()
    background(0, 0, 0, 255)  
    
    w=WIDTH/2            -- set width value
    eh=HEIGHT-400        -- set height of different options
    nh=HEIGHT-600        -- eh easy, nh normal, hh hard
    hh=HEIGHT-800
     
    fontSize(48)            -- set font size
    fill(39, 255, 0, 255)            -- set font color
    
    menuTitle1="(Name Of Game)"            -- main title
    menuTitle2="Select Option"  
    text(menuTitle1,w,HEIGHT-100)     -- display titles 
    text(menuTitle2,w,HEIGHT-200) 
    font("AmericanTypewriter-Bold")     
 
    fill(227, 255, 0, 255)           -- set easy values
    
    t1="Play Game"
    text(t1,w,eh)
    w1,h1=textSize(t1)     -- width and height of easy text 
     
    fill(255, 0, 0, 255)
    t2="High Scores"            -- set normal values
    text(t2,w,nh)
    w2,h2=textSize(t2)      -- width and height of normal text
    
    fill(0, 15, 255, 255)
    t3="Credits"                -- set hard values
    text(t3,w,hh)  
    w3,h3=textSize(t3)      -- width and height of hard text  
end

function easy()
    background(0, 0, 0, 255)
    text("Play game screen",WIDTH/2,HEIGHT/2)
    text("tap screen for menu",WIDTH/2,HEIGHT/2-200)
end

function normal()
    background(0, 0, 0, 255)
    text("high scores screen",WIDTH/2,HEIGHT/2)
    text("tap screen for menu",WIDTH/2,HEIGHT/2-200)
end

function hard()
    background(0, 0, 0, 255)
    text("Credits:",WIDTH/2,HEIGHT/2+400)
    text("Made By: Joshua Brown",WIDTH/2,HEIGHT/2+300)
    text("Do not duplicate and resell",WIDTH/2,HEIGHT/2-100)
    text("tap screen to go to menu",WIDTH/2,HEIGHT/2-400)
    fill(146, 0, 255, 255)
end

function touched(t)
    if t.state==BEGAN then
        if func ~= menu then
            func=menu
            return
        end
        if func==menu then
            if t.x>w-w1/2 and t.x<w+w1/2 and t.y>eh-h1/2 and t.y<eh+h1/2 then
                func=easy
            end
            if t.x>w-w2/2 and t.x<w+w2/2 and t.y>nh-h2/2 and t.y<nh+h2/2 then
                func=normal
            end
            if t.x>w-w3/2 and t.x<w+w3/2 and t.y>hh-h3/2 and t.y<hh+h3/2 then
                func=hard
            end  
       end
    end  
end

--# MakeBullet
function makeBullet()

    local bullet = physics.body(CIRCLE,5)
    bullet.position = vec2(shipPosition.x,90)
    bullet.bullet = true
    bullet.gravityScale = 0
    bullet.linearVelocity = vec2(dx,750)
    bullet.type1 = "bullet"
    bullets[#bullets+1] = bullet
    lastBulletTime = ElapsedTime
    sound(SOUND_SHOOT, 28355)
    end
    
    
function touched(t)

    if t.state == BEGAN and not touch then

        touch = t

        if ElapsedTime-lastBulletTime > .25 then

            makeBullet()

        end

        elseif t.state == ENDED then

        touch = nil

    end
end



--# MakeEnemy
function makeEnemy()

    local enemy = {}
    enemy.type = math.random(#enemysprite)
    enemy.img = enemysprite[enemy.type]
    local size = math.max(enemy.img.width,enemy.img.height)
    enemy.body = physics.body(CIRCLE,size/2)
    enemy.body.position = vec2(math.random(0+size/2,WIDTH-size/2),HEIGHT+size/2)
    enemy.body.bullet = true
    enemy.body.gravityScale = 0
    enemy.body.linearVelocity = vec2(0,-math.random(30,125))
    enemy.body.type1 = "enemy"
    enemies[#enemies+1] = enemy
    lastEnemyTime=ElapsedTime
end

function collide(contactinfo)

    if (contactinfo.bodyA.type1 == "enemy" or
    contactinfo.bodyB.type1 == "enemy") and
    (contactinfo.bodyA.type1 == "bullet" or
    contactinfo.bodyB.type1 == "bullet") then
sound(SOUND_EXPLODE, 10733)
        -- sprite("Tyrian Remastered:Explosion Huge")
        contactinfo.bodyA.collided = true
        contactinfo.bodyB.collided = true
        score = score + 1
    end

end

When I ran the program, I got this:

“error: [string “function makeBullet()…”]:21: attempt to perform arithmetic on global ‘lastBulletTime’ (a nil value)”.
So the spawn bullet function is a global value. I was wondering if anyone could help me change-up the code a little bit so that the main screen comes first, and then the game plays when the words “Play Game” is touched

You have 2 setup functions and only the second one gets run which means none of the initialization in the first setup is happening.

You also have 2 draw functions so only the 2nd one will run.

EDIT: You also have 2 touched functions.

I changed your code so it works, kind of. You have to modify easy, normal, and hard to what you really want them to do.


--# Main
-- Slider

supportedOrientations(PORTRAIT)
displayMode(OVERLAY)
displayMode(FULLSCREEN)

function setup()
    shipPosition = vec2(WIDTH/2,50)
    shipsprite = readImage("Space Art:Red Ship")
    shipspriteL = readImage("Space Art:Red Ship Bank Left")
    shipspriteR = readImage("Space Art:Red Ship Bank Right")
    bulletsprite = readImage("Tyrian Remastered:Bullet Fire C")
    enemysprite = {}
    enemysprite[1] = readImage("Tyrian Remastered:Space Ice 4")
    enemysprite[2] = readImage("Tyrian Remastered:Space Ice 9")
    enemysprite[3] = readImage("Tyrian Remastered:Plane Boss")
    dx = 0
    backingMode(RETAINED)
    bullets = {}
    lastBulletTime = 0
    enemies = {}
    lastEnemyTime = 0
    score = 0
    scoreStartTime = 0
    func=menu
end

function draw()
    func()    
end

function touched(t)
    if func==menu then
        if t.state==BEGAN then
            if t.x>w-w1/2 and t.x<w+w1/2 and t.y>eh-h1/2 and t.y<eh+h1/2 then
                func=game
            end
            if t.x>w-w2/2 and t.x<w+w2/2 and t.y>nh-h2/2 and t.y<nh+h2/2 then
                func=highScore
            end
            if t.x>w-w3/2 and t.x<w+w3/2 and t.y>hh-h3/2 and t.y<hh+h3/2 then
                func=credits
            end  
       end
    elseif func==game then
        if t.state==BEGAN and t.y>900 then
            func=pause
            physics.pause()
            return
        end
        if t.state == BEGAN and not touch then            
            touch = t           
        elseif t.state == ENDED then            
            touch = nil            
        end
    elseif func==highScore then
        if t.state==BEGAN then
            func=menu
        end
    elseif func==credits then
        if t.state==BEGAN then
            func=menu
        end
    elseif func==pause then
        if t.state==BEGAN and t.y<900 then
            func=game
            physics.resume()
        end
    end
end

function menu()
    background(0, 0, 0, 255)  
    w=WIDTH/2            -- set width value
    eh=HEIGHT-400        -- set height of different options
    nh=HEIGHT-600        -- eh easy, nh normal, hh hard
    hh=HEIGHT-800
    fontSize(48)            -- set font size
    fill(39, 255, 0, 255)            -- set font color
    menuTitle1="(Name Of Game)"            -- main title
    menuTitle2="Select Option"  
    text(menuTitle1,w,HEIGHT-100)     -- display titles 
    text(menuTitle2,w,HEIGHT-200) 
    font("AmericanTypewriter-Bold")     
    fill(227, 255, 0, 255)           -- set easy values
    t1="Play Game"
    text(t1,w,eh)
    w1,h1=textSize(t1)     -- width and height of easy text 
    fill(255, 0, 0, 255)
    t2="High Scores"            -- set normal values
    text(t2,w,nh)
    w2,h2=textSize(t2)      -- width and height of normal text
    fill(0, 15, 255, 255)
    t3="Credits"                -- set hard values
    text(t3,w,hh)  
    w3,h3=textSize(t3)      -- width and height of hard text  
end

function highScore()
    background(0, 0, 0, 255)
    text("high scores screen",WIDTH/2,HEIGHT/2)
    text("tap screen for menu",WIDTH/2,HEIGHT/2-200)
end

function credits()
    background(0, 0, 0, 255)
    text("Credits:",WIDTH/2,HEIGHT/2+400)
    text("Made By: Joshua Brown",WIDTH/2,HEIGHT/2+300)
    text("Do not duplicate and resell",WIDTH/2,HEIGHT/2-100)
    text("tap screen to go to menu",WIDTH/2,HEIGHT/2-400)
    fill(146, 0, 255, 255)
end

function game()
    fill(0, 0, 0, 75)
    rect(-1,-1,WIDTH+2,HEIGHT+2)
    if ElapsedTime-lastEnemyTime > 
                2-math.min(1,(ElapsedTime-scoreStartTime)/100) then
        makeEnemy()
    end
    for i,v in pairs(enemies) do
        if v.body.collided == true then
            v.body:destroy()
            table.remove(enemies,i)
        elseif v.body.position.y < -v.body.radius then
            score = 0
            scoreStartTime = ElapsedTime
        elseif v.body.position.x < -v.body.radius or
            v.body.position.x > WIDTH + v.body.radius then
            v.body:destroy()
            table.remove(enemies,i)
        end
    end
    for i,v in ipairs(enemies) do
        sprite(v.img,v.body.position.x,v.body.position.y)
    end
    if touch and ElapsedTime-lastBulletTime > 1 then
        makeBullet()
    end
    for i,v in pairs(bullets) do
        if v.collided == true then
            v:destroy()
            table.remove(bullets,i)
        end
    end
    for i,v in ipairs(bullets) do
        sprite("Tyrian Remastered:Energy Orb 2",v.position.x,v.position.y)
    end
    dx = Gravity.x * 15
    if math.abs(dx) < 1 then
        dx = 0
    end
    shipPosition.x = shipPosition.x + dx
    shipPosition.x = math.max(0,math.min(shipPosition.x,WIDTH))
    if dx == 0 then
        sprite(shipsprite,shipPosition.x,shipPosition.y)
        elseif dx > 0 then
        sprite(shipspriteR,shipPosition.x,shipPosition.y)
    else
        sprite(shipspriteL,shipPosition.x,shipPosition.y)
    end
    fill(255,255,255,255)
    textMode(CENTER)
    textAlign(CENTER)
    fontSize(20)
    font("Zapfino")
    text("Score: "..score,WIDTH/1.3,HEIGHT-20)
    text("PAUSE",200,HEIGHT-30)
end

function pause()
    text("RESUME",WIDTH/2,HEIGHT/2)    
end

--# MakeBullet
function makeBullet()
    local bullet = physics.body(CIRCLE,5)
    bullet.position = vec2(shipPosition.x,90)
    bullet.bullet = true
    bullet.gravityScale = 0
    bullet.linearVelocity = vec2(dx,750)
    bullet.type1 = "bullet"
    bullets[#bullets+1] = bullet
    lastBulletTime = ElapsedTime
    sound(SOUND_SHOOT, 28355)
end

--# MakeEnemy
function makeEnemy()
    local enemy = {}
    enemy.type = math.random(#enemysprite)
    enemy.img = enemysprite[enemy.type]
    local size = math.max(enemy.img.width,enemy.img.height)
    enemy.body = physics.body(CIRCLE,size/2)
    enemy.body.position = vec2(math.random(0+size/2,WIDTH-size/2),HEIGHT+size/2)
    enemy.body.bullet = true
    enemy.body.gravityScale = 0
    enemy.body.linearVelocity = vec2(0,-math.random(30,125))
    enemy.body.type1 = "enemy"
    enemies[#enemies+1] = enemy
    lastEnemyTime=ElapsedTime
end

function collide(contactinfo)
    if (contactinfo.bodyA.type1 == "enemy" or
            contactinfo.bodyB.type1 == "enemy") and
            (contactinfo.bodyA.type1 == "bullet" or
            contactinfo.bodyB.type1 == "bullet") then
        sound(SOUND_EXPLODE, 10733)
        -- sprite("Tyrian Remastered:Explosion Huge")
        contactinfo.bodyA.collided = true
        contactinfo.bodyB.collided = true
        score = score + 1
    end
end

@The_Waffle_Man I changed the above code to include your other touch functions.

Thank you so much Dave1707! I really appreciate it. I’m sure you noticed that I had a “credits” and “high score” section as well. I will try to get them to work the way I want them to, and if I can’t, I hope you don’t mind me asking you again for help. :smiley:

@The_Waffle_Man I changed the code again and added the pause you requested in another post.

@dave1707 could I get the code via private message please?

@The_Waffle_Man The code that I posted above has all the changes in it. Instead of adding more code to this discussion, I just replaced my above code with the changes.

oh haha. I knew that :stuck_out_tongue: