I'm trying to make a scoring system that displays an all time score across every scene, when a user completes a scene the score from that scene is added to the all time score.
I get 1 error in the compiler: Attempt to perform "arithmetic" on field "score"
code from main lua: contains no errors but not sure if correct.
-- 1 Clue 1 Word
display.setStatusBar (display.HiddenStatusBar)
local storyboard = require ( "storyboard" )
storyboard.gotoScene("menu")
storyboard.state = {}
storyboard.state.score = 0
storyboard.state.score = display.newText(storyboard.state.score, 250, 20, "ARIAL", 16)
<code>
The error appears in the line: storyboard.state.score = storyboard.state.score + 1
<code>
if(correct == #L1) then
--alert()
print ("CORRECT "..#L1)
line.isVisible = false
storyboard.gotoScene( "scene2", "slideLeft", 500)
storyboard.state.score = storyboard.state.score + 1
storyboard.removeScene( "scene1" )
end
end
end
Why storyboard.state.score assigned twice?
storyboard.state.score = 0
storyboard.state.score = display.newText(storyboard.state.score, 250, 20, "ARIAL", 16)
UPD :
storyboard.state = {score = 0}
score = display.newText(storyboard.state.score, 250, 20, "ARIAL", 16)
storyboard.gotoScene( "scene2", "slideLeft", 500)
storyboard.state.score = storyboard.state.score + 1
storyboard.removeScene( "scene1" )
Related
whenever the specific food hits the monkey, the game restarts but I want to delay it for few seconds and show some text before restarting but i cant seem to. It does not delay,
local function monkeyCollision( self, event )
if event.phase == "began" then
if event.target.type == "monkey" and event.other.type == "food" then
print( "chomp!" )
event.other.alpha = 0
event.other:removeSelf()
addToScore(5)
-- get points!
else
print("ow!")
monkey.alpha = 0
monkey:removeSelf()
displayScore = display.newText( "The total score is " .. score , 0, 0, "Helvetica", 30 )
displayScore.x = screenLeft +150
displayScore.y = screenRight-100
displayre = display.newText( " The game is going restart", 0, 0, "Helvetica", 25 )
displayre.x = screenLeft +150
displayre.y = screenRight-200
storyboard.gotoScene("play", "fade", 1000)
end
Why not put it in a timer:
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
That will delay 5 seconds before calling storybaord.gotoScene()
Add a timer as Rob did like
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
But you also have a problem now. What if you hit another food after you already hit one? This would make multiple timers go off and probably glitch because it will remove a monkey which was already removed...
local lostGame = false
local function monkeyCollision( self, event )
if event.phase == "began" then
if event.target.type == "monkey" and event.other.type == "food" then
print( "chomp!" )
event.other.alpha = 0
event.other:removeSelf()
addToScore(5)
-- get points!
else
if lostGame == false then
print("ow!")
monkey.alpha = 0
monkey:removeSelf()
displayScore = display.newText( "The total score is " .. score , 0, 0, "Helvetica", 30 )
displayScore.x = screenLeft +150
displayScore.y = screenRight-100
displayre = display.newText( " The game is going restart", 0, 0, "Helvetica", 25 )
displayre.x = screenLeft +150
displayre.y = screenRight-200
timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)
lostGame = true
end
end
end
end
By adding a variable to check if you have already lost, you can prevent it from running the code to leave while you are in the delay.
I'm writing the code that would save the scores to a "state" table and it would continuously accumulate until it reaches the result page. I use the table because it says table is better than using 'globals'. I'm having problems with the accumulation as the scoreText resets to 0 when jumping to the next scene.
main.lua
display.setStatusBar( display.HiddenStatusBar )
local storyboard = require "storyboard"
storyboard.state = {}
storyboard.state.score = 0
storyboard.gotoScene( "scene_splash" )
Question1.lua
local scoreText
function scene:enterScene(event)
...
scoreText = display.newText( "0", 0, 0, native.systemFont, 32 )
scoreText:setFillColor( 0,0, 0 )
scoreText.x = 87
scoreText.y = 28
group:insert( scoreText )
if (event.other == balloons[1]) then
scene.updateScore()
print('Ball is colliding')
balloon1.isVisible = false
balloonText1.isVisible = false
audio.play(pop)
storyboard.gotoScene("correctAnswer1", "fade", 1000)
end
end
function scene.updateScore()
storyboard.state.score = storyboard.state.score + 50
scoreText.text = storyboard.state.score
end
Question2.lua
local scoreText
function scene:enterScene(event)
...
if (event.other == balloons[3]) then
scene.updateScore()
print('Ball is colliding')
balloon3.isVisible = false
balloonText3.isVisible = false
audio.play(pop)
storyboard.gotoScene("correctAnswer2", "fade", 1000)
end
end
function scene.updateScore()
storyboard.state.score = storyboard.state.score + 50
scoreText.text = storyboard.state.score
end
...
I made a minor mistake in a line and i changed it to:
scoreText = display.newText( storyboard.state.score, 0, 0, native.systemFont, 32 )
The problem is that the enterScene event does not have an other field so event.other is nil so the block of code where you call score.update does not get executed in either of the question scenes. You can check this by putting some print statements in update. The condition should be something else.
The restart game function doesn't seem to work and I don't know why. The balloons, scores resets but the game doesn't resets, i can't shoot the balloons again. (plus the askUser, yesBtn and noBtn doesn't go invisible either)
function createBalloons(a, b)
for i = 1, a do
for j = 1, b do
local balloon = display.newImage ('balloon_fat_red.png', 270+ (i * 30), 80 + (j * 50))
balloonText = display.newText(hiragana_array[x+1], 300, 125)
balloonTextt = display.newText(hiragana_array[x+2], 300, 175)
balloonTexttt = display.newText(hiragana_array[x+3], 300, 225)
balloonText:setFillColor( 1,1, 0 )
balloonTextt:setFillColor( 1,1, 0 )
balloonTexttt:setFillColor( 1,1, 0 )
balloon.name = 'balloon'
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
end
end
target.text = #balloons
end
function restartLvl()
for i = 1, #balloons do
display.remove(balloons[i])
print ("restart level")
end
score.text = '0'
ballRemain.text = '3'
balloons = {}
createBalloons(1, 3)
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
print("time from start: ", (system.getTimer()-gameTime))
print('send mail')
sendMail()
end
This is what it shows in the simulator.
I'm not seeing any problem with that code, my guess is the problem is elsewhere. You're going to have to dig a little more, maybe put some more print statements. For example maybe isVisible is being reset to true by another function, after the restartLvl, like in an enterFrame handler.
I ran this code and it gave me an error attempt to index global 'self' (a nil value), in this scene i'm creating the Question1 of the game, inside which includes creating cannon, balloons and other game elements. I checked but i'm not sure whats wrong here.
function scene.createScene()
local group = self.view ---line 27 where i got the error
scoreText = display.newText( "0", 0, 0, globals.font.bold, 32 )
scoreText.x = display.contentCenterX
scoreText.y = 32
group:insert( scoreText )
background = display.newImage( "bkg_clouds.png")
group:insert(background)
background.x = 240
background.y = 195
questionText = display.newText('a', display.contentCenterX, display.contentWidth/4, native.systemFont, 40)
group:insert(questionText)
infoBar = display.newImage ("infoBar.png")
group:insert(infoBar)
background.x = 200
background.y = 100
restartBtn = display.newImage ("restartBtn.png")
group:insert(restartBtn)
background.x = 470
background.y = 300
cannon = display.newImage ("cannon.png")
group:insert(cannon)
background.x = 10
background.y = 270
cannon.anchorX = 0.5
cannon.anchorY = 0.5
restartBtn.isVisible = true
function createBalloons(a, b)
for i = 1, a do
for j = 1, b do
local balloon = display.newImage ('balloon_fat_red.png', 465+ (i * 30), 80 + (j * 50))
balloon.balloonText1 = display.newText(hiragana_array[x+1], 495, 125)
balloon.balloonText2 = display.newText(hiragana_array[x+2], 495, 175)
balloon.balloonText3 = display.newText(hiragana_array[x+3], 495, 225)
balloon.balloonText1:setFillColor( 1,1, 0 )
balloon.balloonText2:setFillColor( 1,1, 0 )
balloon.balloonText3:setFillColor( 1,1, 0 )
balloon.name = 'balloon'
physics.addBody(balloon)
balloon.bodyType = 'static'
table.insert(balloons, balloon)
end
end
target.text = #balloons
end
function cannonCharge:touch(e)
if(e.phase == 'began') then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
end
end
function charge()
local degreesPerFrame = 1
cannon.rotation = cannon.rotation - degreesPerFrame
impulse=impulse-0.2
if(cannon.rotation < -46) then
cannon.rotation = -46
impulse = -3.2
end
end
function shot:touch(e)
if(e.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = false
cannon.rotation = 0
local cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
group:insert(cannonBall)
-- Shoot cannon ball
cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
end
end
function ballCollision(e)
if (e.other.name == 'balloon') then
scene.updateScore()
e.target:removeSelf()
print ('remove balloon text')
e.other:removeSelf()
audio.play(pop)
end
end
cannonBall:applyLinearImpulse(dir, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
scene.view:insert( ballCollision )
end
You probably need function scene:createScene(). Note the colon instead of the dot in your original code.
Your function should be like this.
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
-----------------------------------------------------------------------------
end
Here is the reference:
http://docs.coronalabs.com/api/library/storyboard/
I have a menu lua and when i choose 1 of the category (the "songselect" lua) and go back to the menu lua i got this error:
Runtime error
...ocuments\corona projects\singingbeemenu\director.lua:151: attempt to
call field 'unloadMe' (a nil value)
stack traceback:
[C]: in function 'unloadMe'
...ocuments\corona projects\singingbeemenu\director.lua:151: in function
'_listener
'
this is 1 of my category the songselect.lua
module(..., package.seeall)
display.setStatusBar( display.HiddenStatusBar )
function new()
local localGroup = display.newGroup()
local tableView = require("tableView")
local ui = require("ui")
--------------------------------------------------------------------------
local screenOffsetW, screenOffsetH = display.contentWidth - display.viewableContentWidth, display.contentHeight - display.viewableContentHeight
local songList
local backBtn
local detailScreenText
local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(0, 0, 0)
local data = {}
--iPad: setup a color fill for selected items
local selected = display.newRect(0, 0, 50, 50) --add acolor fill to show the selected item
selected:setFillColor(67,141,241,180) --set the color fill to light blue
selected.isVisible = false --hide color fill until neede
-----------------------------------------------
data[1] = {}
data[1].title = "Crazy Song"
data[1].subtitle = "by Bruno Earth"
data[1].image = "note.png"
data[2] = {}
data[2].title = "Enter Sunman"
data[2].subtitle = "by Mentalica"
data[2].image = "note.png"
local topBoundary = display.screenOriginY + 40
local bottomBoundary = display.screenOriginY + 0
songList = tableView.newList{
data=data,
default="listItemBg.png",
over="listItemBg_over.png",
top=topBoundary,
bottom=bottomBoundary,
callback = function( row )
local g = display.newGroup()
local img = display.newImage(row.image)
g:insert(img)
img.x = math.floor(img.width*0.5 + 6)
img.y = math.floor(img.height*0.5)
local title = display.newText( row.title, 0, 0, native.systemFontBold, 16 )
title:setTextColor(0,0,0)
g:insert(title)
title.x = title.width*0.5 + img.width + 6
title.y = 30
local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 14 )
subtitle:setTextColor(80,80,90)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.y = title.y + title.height + 6
return g
end
}
local function scrollToTop()
songList:scrollTo(topBoundary-1)
end
local navBar = display.newImage("navBar.png")
navBar.x = display.contentWidth*.5
navBar.y = math.floor(display.screenOriginY + navBar.height*0.5)
local navHeader = display.newText("Song Lists", 0, 0, native.systemFontBold, 16)
navHeader:setTextColor(255, 255, 255)
navHeader.x = display.contentWidth*.5
navHeader.y = navBar.y
--Setup the back button
local backToMenu = function(event)
print (event.phase)
if event.phase == 'ended' then
print ("ok")
director:changeScene(event.target.scene, "fade")
print ("back!")
end
end
backBtn = display.newImage("backButton.png")
backBtn.x = math.floor(backBtn.width/2) + backBtn.width + screenOffsetW
backBtn.y = navBar.y
backBtn.scene = "menu"
backBtn:addEventListener("touch", backToMenu)
--backBtn.alpha = 0
local listBackground = display.newRect( 0, 0, songList.width, songList.height )
listBackground:setFillColor(255,255,255)
songList:insert(1,listBackground)
return localGroup
end
is it impossible the go back method in lua?
can anyone can help me and give an idea why i got the error?
thanks in advance...
I got this, i change my director.lua to the latest version...
Try this Lua file to solve your problem.