I am aware this is probably a silly mistake but I have been staring at my code fr an hour trying to fix this error, can anyone help me.
–
– scene1.lua
– Your code here
local composer = require(“composer”)local scene = composer.newScene(“MainGame”)
function scene:create( event )
local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect( "background.jpg", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
sceneGroup:insert( background )local backgroundDamage = display.newImageRect( “backgrounddamage.jpg”, 360, 570 )
backgroundDamage.x = display.contentCenterX
backgroundDamage.y = display.contentCenterY
backgroundDamage.alpha = 0
sceneGroup:insert( backgroundDamage )local monster = display.newImageRect( “wolf.png”, 315, 240)
monster.x = display.contentCenterX
monster.y = display.contentCenterY
sceneGroup:insert( monster )local score = 0
local scoreDisplay = display.newText( "Score: " … score, display.contentCenterX, 10, native.systemFont, 40)
scoreDisplay:setFillColor( 255, 255, 255 )
sceneGroup:insert( scoreDisplay )local playerHealthNumber = 100
local monsterHealth = display.newText( “100”, display.contentCenterX, 100, native.systemFont, 40)
monsterHealth:setFillColor( 255, 255, 255 )
sceneGroup:insert( monsterHealth )local playerHealthBarRed = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
playerHealthBarRed:setFillColor( 255, 0, 0 )
sceneGroup:insert( playerHealthBarRed )local playerHealthBar = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
playerHealthBar:setFillColor( 255, 255, 255 )
sceneGroup:insert( playerHealthBar )local playerHealth = display.newText( “100”, display.contentCenterX, 500, native.systemFont, 40)
playerHealth:setFillColor( 0, 0, 0 )
sceneGroup:insert( playerHealth)local sword = display.newImageRect( “swordbutton.png”, 50, 50)
sword.x = display.contentCenterX - 75
sword.y = display.contentCenterY + 200
sceneGroup:insert( sword )local sheild = display.newImageRect( “sheildbutton.png”, 50, 50)
sheild.x = display.contentCenterX + 75
sheild.y = display.contentCenterY + 200
sceneGroup:insert( sheild )local magic = display.newImageRect( “magicbutton.png”, 50, 50)
magic.x = display.contentCenterX
magic.y = display.contentCenterY + 200
sceneGroup:insert( magic )local magicAni = display.newImageRect( “fire.png”, 400, 400)
magicAni.x = display.contentCenterX
magicAni.y = display.contentCenterY - 30
magicAni.alpha = 0
sceneGroup:insert( magicAni )local sheildAni = display.newImageRect( “sheild.png”, 200, 200)
sheildAni.x = display.contentCenterX
sheildAni.y = display.contentCenterY
sheildAni.alpha = 0
sceneGroup:insert( sheildAni )local magicNumber = 10
local sheildNumber = 10
local swordDamage = 10local magicUses = display.newRect( 185, 460, 20, 20 )
magicUses:setFillColor( 255, 255, 0 )
sceneGroup:insert( magicUses )local magicUsesRemaining = display.newText( magicNumber, 185, 460, native.systemFont, 20)
magicUsesRemaining:setFillColor( 0, 0, 0 )
sceneGroup:insert( magicUsesRemaining )local sheildUses = display.newRect( 260, 460, 20, 20 )
sheildUses:setFillColor( 255, 255, 0 )
sceneGroup:insert( sheildUses )local sheildUsesRemaining = display.newText( sheildNumber, 260, 460, native.systemFont, 20)
sheildUsesRemaining:setFillColor( 0, 0, 0 )
sceneGroup:insert( sheildUsesRemaining )local monsterTable = { ‘slime’ , ‘wolf’ }
local playerTurn = true
local monsterType = “slime”
local monsterMove = 0
local monsterDefeated = false
composer.setVariable( “shopChoice”, “none” )
local function shop()
local function checkLevel()
if (score == 5) then
table.insert(monsterTable, “minotaur”)
end
endcheckLevel() if (shopChoice == "magic") then magicNumber = 10 magicUsesRemaining.text = tostring(magicNumber) else if (shopChoice == "sheild") then sheildNumber = 10 sheildUsesRemaining.text = tostring(sheildNumber) else if (shopChoice == "sword") then swordDamage = swordDamage + 5 else if (shopChoice == "potion") then if (playerHealthNumber > 50) then playerHealthNumber = 100 else playerHealthNumber = playerHealthNumber + 50 end display.remove( playerHealthBar ) display.remove( playerHealth ) playerHealthBar = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 ) playerHealth = display.newText( playerHealthNumber, display.contentCenterX, 500, native.systemFont, 40) playerHealth:setFillColor( 0, 0, 0 ) end
end
-----------------------------------------------------------------------------------------------
local function spawnMonster()
if (monsterType == “slime”) then
monster.fill = { type = “image”, filename = “slime.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
print(“monster spawned”)
elseif (monsterType == “minotaur”) then
monster.fill = { type = “image”, filename = “minotaur.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
elseif (monsterType == “wolf”) then
monster.fill = { type = “image”, filename = “wolf.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
end
endlocal function spawnRandomMonster()
print(“spawnRandomMonster”)
monsterType = monsterTable[ math.random( #monsterTable ) ]
spawnMonster()
monster.alpha = 100
monsterHealth.alpha = 100
monsterMove = 0
if (monsterType == “slime”) then
monsterHealth.text = 100
elseif (monsterType == “minotaur”) then
monsterHealth.text = 150
elseif (monsterType == “wolf”) then
monsterHealth.text = 50
end
endlocal function playerHealthCheck()
playerHealthNumber = tonumber(playerHealth.text)
display.remove( playerHealthBar )
display.remove( playerHealth )
playerHealthBar = display.newRect( display.contentCenterX, 498, playerHealthNumber * 2.5, 40 )
playerHealth = display.newText( playerHealthNumber, display.contentCenterX, 500, native.systemFont, 40)
playerHealth:setFillColor( 0, 0, 0 )
print(“playerHealthCheck”)
if (playerHealthNumber < 1) then
composer.setVariable( “finalScore”, score )
composer.gotoScene(“GameOver”)
end
endlocal function monsterHealthCheck()
playerHealthCheck()
local monsterHealthNumber = tonumber(monsterHealth.text)
print(“monsterHealthCheck”)
if (monsterHealthNumber < 1) then
monsterDefeated = true
monster.alpha = 0
monsterHealth.alpha = 0
score = score + 1
display.remove( scoreDisplay )
scoreDisplay = display.newText( "Score: " … score, display.contentCenterX, 10, native.systemFont, 40)
timer.performWithDelay( 100, spawnRandomMonster )
if (score % 5) == 0 then
composer.gotoScene(“Shop”)
end
end
endlocal function monsterMoveUpdate()
if (monsterMove == 4) then
monsterMove = monsterMove - 3
print(“check”)
elseif (monsterMove < 5) then
monsterMove = monsterMove + 1
end
endlocal function sheildAnimation()
sheildAni.alpha = 0
endlocal function magicAnimation()
magicAni.alpha = 0
endlocal function playerDamageAnimation()
backgroundDamage.alpha = 0
endlocal function updateMagicNumber()
print(“updateMagicNumber”)
magicNumber = magicNumber - 1
magicUsesRemaining.text = tostring(magicNumber)
endlocal function updateSheildNumber()
print(“updateSheildNumber”)
sheildNumber = sheildNumber - 1
sheildUsesRemaining.text = tostring(sheildNumber)
endlocal function tapSword()
monsterMoveUpdate()
if (monsterType == “slime”) then
monster.fill = { type = “image”, filename = “slimedamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2) then
monsterHealth.text = monsterHealth.text - swordDamage
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 3 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - swordDamage
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “minotaur”) then
monster.fill = { type = “image”, filename = “minotaurdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - swordDamage
playerHealth.text = playerHealth.text - 15
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 2 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - swordDamage
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “wolf”) then
monster.fill = { type = “image”, filename = “wolfdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - swordDamage
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 4) then
monsterHealth.text = monsterHealth.text - swordDamage
timer.performWithDelay( 300, monsterHealthCheck )
else
end
end
endlocal function tapMagic()
if magicNumber > 0 then
monsterMoveUpdate()
magicAni.alpha = 100
updateMagicNumber()
timer.performWithDelay( 300, magicAnimation )
if (monsterType == “slime”) then
monster.fill = { type = “image”, filename = “slimedamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2) then
monsterHealth.text = monsterHealth.text - 20
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 3 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - 20
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “minotaur”) then
monster.fill = { type = “image”, filename = “minotaurdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - 20
playerHealth.text = playerHealth.text - 15
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 2 or monsterMove == 4) then
monsterHealth.text = monsterHealth.text - 20
timer.performWithDelay( 300, monsterHealthCheck )
else
end
elseif (monsterType == “wolf”) then
monster.fill = { type = “image”, filename = “wolfdamage.png” }
monster.x = display.contentCenterX
monster.y = display.contentCenterY
timer.performWithDelay( 300, spawnMonster )
if(monsterMove == 1 or monsterMove == 2 or monsterMove == 3) then
monsterHealth.text = monsterHealth.text - 20
playerHealth.text = playerHealth.text - 5
timer.performWithDelay( 300, playerDamageAnimation )
backgroundDamage.alpha = 100
timer.performWithDelay( 300, monsterHealthCheck )
elseif(monsterMove == 4) then
monsterHealth.text = monsterHealth.text - 20
timer.performWithDelay( 300, monsterHealthCheck )
else
end
end
end
endlocal function tapSheild()
if sheildNumber > 0 then
updateSheildNumber()
if (monsterType == “slime” or monsterType == “minotaur” or monsterType == “wolf”) then
monsterMoveUpdate()
sheildAni.alpha = 100
timer.performWithDelay( 500, sheildAnimation )
end
end
endsword:addEventListener( “tap”, tapSword )
magic:addEventListener( “tap”, tapMagic )
sheild:addEventListener( “tap”, tapSheild )spawnRandomMonster()
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phaseif ( phase == “will” ) then
– any code placed here will run when the scene is still “off-screen”, but about to be displayed to the user. In many cases, this will be empty.
elseif ( phase == “did” ) then
– any code placed here will run as soon as the scene is displayed on screen. This is where you would start any animations, start playing background audio, start timers, etc.
end
endfunction scene:hide( event )
local sceneGroup = self.view
local phase = event.phaseif ( phase == “will” ) then
– any code placed here will run when the scene is still on screen, but is about to go off screen. This is where you would stop timers, audio, and animations that you created in the show event.
elseif ( phase == “did” ) then
– any code placed here will run as soon as the scene is no longer visible. In many cases, this will be empty.
end
endfunction scene:destroy( event )
local sceneGroup = self.view
end
– Scene event function listeners
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene