display score on screen and increase it with a counter?

Hi,

I’m trying to create a simple text field which displays the score on screen:

score = 0
local scoreText = display.newText( "Score: " … score, 20, 0,nil, 20)
scoreText:setTextColor(255,255,255)

I then use a simple function:

local function getScore() – increments score value every time it is called
score = score + 1
print( “score” … score )
end

The problem is that the function doesn’t pass the data back to the “score” variable.

Can anyone shed any light on this? I’m sure it’s just me not thinking right.

Thanks

Dan [import]uid: 67933 topic_id: 12483 reply_id: 312483[/import]

How are you updating the score? You need to call the function when something else happens.
[import]uid: 45932 topic_id: 12483 reply_id: 45585[/import]

You need to call a function with event listener or timer
something like that:
[lua]score = 0
local scoreText = display.newText( "Score: " … score, 20, 0,nil, 20)
scoreText:setTextColor(255,255,255)

local function getScore() – increments score value every time it is called
score = score + 1
scoreText.text = "Score: " … score
print(“score” … score)
end

timer.performWithDelay(1000, getScore, 0)[/lua] [import]uid: 16142 topic_id: 12483 reply_id: 45587[/import]

@darkconsoles - That’s the second post I’ve seen from you today helping people by writing some code, good on you :slight_smile: [import]uid: 52491 topic_id: 12483 reply_id: 45590[/import]

As a side note, if you didn’t want to use a timer you could instead call getScore() whenever points are triggered :slight_smile: [import]uid: 52491 topic_id: 12483 reply_id: 45591[/import]

Brilliant.

Thanks guys.

I did have a timer set up, but forgot to include that in my original post…

If anyone else encounters the same problem and comes across this post, I hadn’t included the code from line 7 in the above post in my function:

scoreText.text = "Score: " … score

As a side issue, how do you guys copy/paste the coloured code, as in the previous post? I’m using notepad++ if that makes any difference.

Thanks again. [import]uid: 67933 topic_id: 12483 reply_id: 45594[/import]

They are enclosing the code using syntax highlighting.

< code> and < /code> (minus the spaces) [import]uid: 14218 topic_id: 12483 reply_id: 45677[/import]