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