Make a score and show it

Hello,

I’m new here. I’m trying to build my own game in Corona SDK. I have follow some tuts and i think i’m understanding a bit of Lua. I want to show the score everytime when a happy face “destroy” a bad face. This is the code:

display.setStatusBar(display.HiddenStatusBar) local physics = require "physics" physics.start() physics.setGravity(0, 0) local background = display.newImage("background.jpg") local happy = display.newImage("happy.png") happy.x = 45 happy.y = 45 physics.addBody(happy, "dynamic") local angry = display.newImage("angry.png") angry.x = 420 angry.y = 280 physics.addBody(angry, "static") local angry2 = display.newImage("angry.png") angry2.x = 400 angry2.y = 220 physics.addBody(angry2, "static") function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener("touch", touchScreen) function moveAngry() transition.to(angry, {time=1000, x=math.random(10,600), y=math.random(10,250), onComplete=moveAngry}) transition.to(angry2, {time=1000, x=math.random(10,600), y=math.random(10,250)}) end moveAngry() function onCollision(event) --print("Collide!") happy:removeSelf() end Runtime:addEventListener("collision", onCollision)

See this tutorial:

https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/

Rob

Can you explain what you want in a little more detail?

–SonicX278

Jeah, i want everytime when the angry destroys the happy, the score will be +1

Hey there. I seriously reccomend reading the tutorial Rob posted above.

It covers everything you are asking in detail.

Merry Christmas.

I can write the code for you in about 3 hours. I’m a bit busy atm.

–SonicX278

Hey, So this is what you wanted.

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody(angry, "static") angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" local angry2 = display.newCircle( 0, 0, 10 ) angry2.x = 400 angry2.y = 220 physics.addBody(angry2, "static") angry2:setFillColor( 1, 0, 0 ) angry2.myName = "angry2" function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) transition.to(angry2, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ) }) end moveAngry() local function functions() display.remove(happy) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "happy" and event.other.myName == "angry" then functions() elseif event.target.myName == "happy" and event.other.myName == "angry2" then functions() end end end happy:addEventListener( "collision", onCollision )

–SonicX278

I have a question, so Im making a game and I have score, but I can’t get it to display right. Heres my code.

PLEASE HELP!!!

 local scoreTotal = 0

 local function updateScoreTotal(event)

    scoreTotal = scoreTotal + event.target.coinValue

    scoreTotal.text = scoreTotal

 end

 

 local scoreTotal = display.newText(scoreTotal,40,0,native.systemFrontBold,50)

What do you mean its not displaying right? 

–SonicX278

You’ve got a couple of problems going on.

First, you cannot use the same variable for two different purposes. When you do:

local scoreTotal = 0

you’re trying to use scoreTotal as a number.  But a few lines later you do:

local scoreTotal = display.newText(scoreTotal,40,0,native.systemFrontBold,50)

Now you’re trying to use scoreTotal as a display text object. This overwrites the original use. Then inside your function, you now try to add a number to a display object, which you cannot, then you try to access scoreTotal’s object attribute.

You need separate variables: one to track the value of the score and one to hold the display text object.

Next, you have a function called updateScoreTotal() but I don’t see where you ever call it. You’re passing in a variable named “event” and your code assumes that’s a table with a pointer to some other object (event.target) and that object has a value called coinValue.

I don’t see where you’re doing any of that. But the fact that you’re trying to use event.target.coinValue, its almost acting some touch handler.

Rob

See this tutorial:

https://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/

Rob

Can you explain what you want in a little more detail?

–SonicX278

Jeah, i want everytime when the angry destroys the happy, the score will be +1

Hey there. I seriously reccomend reading the tutorial Rob posted above.

It covers everything you are asking in detail.

Merry Christmas.

I can write the code for you in about 3 hours. I’m a bit busy atm.

–SonicX278

Hey, So this is what you wanted.

display.setStatusBar(display.HiddenStatusBar) local physics = require( "physics" ) physics.start() physics.setGravity(0, 0) local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local background = display.newRect( centerX, centerY, actualW, actualH ) background:setFillColor( 0, 0, 1 ) local score = 0 local scoreTxt = display.newText( "0", 0, 0, native.systemFontBold, 40 ) scoreTxt.x = centerX + actualW/2 - 20 scoreTxt.y = centerY - actualH/2 + 20 local function updateScore(event) score = score + 1 scoreTxt.text = string.format("%d", score ) end local happy = display.newCircle( 0, 0, 10 ) happy.x = 45 happy.y = 45 physics.addBody( happy, "dynamic" ) happy:setFillColor( 0, 1, 0 ) happy.myName = "happy" local angry = display.newCircle( 0, 0, 10 ) angry.x = 420 angry.y = 280 physics.addBody(angry, "static") angry:setFillColor( 1, 0, 0 ) angry.myName = "angry" local angry2 = display.newCircle( 0, 0, 10 ) angry2.x = 400 angry2.y = 220 physics.addBody(angry2, "static") angry2:setFillColor( 1, 0, 0 ) angry2.myName = "angry2" function touchScreen(event) if event.phase == "began" then transition.to(happy, {time=1000, x=event.x, y=event.y}) end end Runtime:addEventListener( "touch", touchScreen ) function moveAngry() transition.to(angry, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ), onComplete=moveAngry }) transition.to(angry2, {time=1000, x=math.random( centerX - actualW/2, centerX + actualW/2 ), y=math.random( centerY - actualH/2, centerY + actualH/2 ) }) end moveAngry() local function functions() display.remove(happy) updateScore() end function onCollision(event) if event.phase == "began" then if event.target.myName == "happy" and event.other.myName == "angry" then functions() elseif event.target.myName == "happy" and event.other.myName == "angry2" then functions() end end end happy:addEventListener( "collision", onCollision )

–SonicX278

I have a question, so Im making a game and I have score, but I can’t get it to display right. Heres my code.

PLEASE HELP!!!

 local scoreTotal = 0

 local function updateScoreTotal(event)

    scoreTotal = scoreTotal + event.target.coinValue

    scoreTotal.text = scoreTotal

 end

 

 local scoreTotal = display.newText(scoreTotal,40,0,native.systemFrontBold,50)

What do you mean its not displaying right? 

–SonicX278

You’ve got a couple of problems going on.

First, you cannot use the same variable for two different purposes. When you do:

local scoreTotal = 0

you’re trying to use scoreTotal as a number.  But a few lines later you do:

local scoreTotal = display.newText(scoreTotal,40,0,native.systemFrontBold,50)

Now you’re trying to use scoreTotal as a display text object. This overwrites the original use. Then inside your function, you now try to add a number to a display object, which you cannot, then you try to access scoreTotal’s object attribute.

You need separate variables: one to track the value of the score and one to hold the display text object.

Next, you have a function called updateScoreTotal() but I don’t see where you ever call it. You’re passing in a variable named “event” and your code assumes that’s a table with a pointer to some other object (event.target) and that object has a value called coinValue.

I don’t see where you’re doing any of that. But the fact that you’re trying to use event.target.coinValue, its almost acting some touch handler.

Rob