I want to show the score whenever the player touch but it did not

please help to look at my code:

–Set up the storyboard class

local storyboard = require(“storyboard”);

local scene = storyboard.newScene()

–Forward references so that we can access objects accross functions

local bar;

local spinIt;

– declaring score 

local scoreBonus = 2;

local scorePassLevel = 30;

local scoreLostLevel = 29;

function scene:createScene(e)

local view = self.view

local background = display.newRect(0, 0, 1080, 1080);

background:setFillColor(255,255,255);

local score = display.newText ("Score: ", 40,20, native.systemFont, 20)

score:setTextColor (0,0.5,1)

score.x = 40; score.y = 20;

local score = 0;

function background:touch (e)

if e.phase == “began” then

print (“background touch”)

score = score+1;

end

end

background:addEventListener (“touch”, background)

local font = “HelveticaNeue” or native.systemFont;

local txt = display.newText(“Level 1”,0,0,font,24);

    txt:setTextColor(0,0.5,1);

    txt.x = 200; txt.y = 20;

    

    --Remember: this is local to the entire scene (line 6)

    bar = display.newImageRect(“images/bar.png”,100,100);

bar.x = _W * 0.5;

bar.y = _H - 80;

function txt:tap(e)

storyboard.gotoScene(“level3”,{

effect = “slideUp”, – transition effect to implement

time = 250 – duration of the transition effect in milliseconds

});

end

txt:addEventListener(“tap”,txt);

view:insert(background);

–view:insert(score);

    view:insert(txt);

    view:insert(bar);

end

function scene:enterScene(e)

function spinIt(e)

bar.rotation = (bar.rotation - 1) % 360;

end

Runtime:addEventListener(“enterFrame”,spinIt);

end

function scene:exitScene(e)

–Stop listeners, timers, and animations (transitions)

Runtime:removeEventListener(“enterFrame”,spinIt);

storyboard.purgeScene(“scene2”);–Remove all scene1 display objects

end

scene:addEventListener(“createScene”, scene);

scene:addEventListener(“enterScene”, scene);

scene:addEventListener(“exitScene”, scene);

–There are more events you can listen for;

–See the Corona docs

return scene

best regards

Seng Keat

You need to update the string on the text object.

local prefix = "Score: " local score = 0 -- the actual score local scoreDisplay = display.newText(prefix..score, 40, 20, native.systemFont, 20) -- scoreDisplay must be used. you had two score variables, so one overwrites the other! -- .. bridges the gap between two strings or numbers! function background:touch (e) if e.phase == "began" then print ("background touch") score = score+1; scoreDisplay.text = prefix..score end end background:addEventListener ("touch", background)

Hi Richard9

It worked now 

thanks for your quick response

Keat

You need to update the string on the text object.

local prefix = "Score: " local score = 0 -- the actual score local scoreDisplay = display.newText(prefix..score, 40, 20, native.systemFont, 20) -- scoreDisplay must be used. you had two score variables, so one overwrites the other! -- .. bridges the gap between two strings or numbers! function background:touch (e) if e.phase == "began" then print ("background touch") score = score+1; scoreDisplay.text = prefix..score end end background:addEventListener ("touch", background)

Hi Richard9

It worked now 

thanks for your quick response

Keat