EASY way to do score?

I’m pretty new to this, and I would like to add score on touch of an object. Can anyone help me? Where can I create the base number that will change? Thanks in advance.

Here’s my code:
[lua]scoreText = display.newText( “Score”,350, 15, Font, 60)

local function redTouch(event)
if event.phase == “began” then
score = score + 1
scoreText.text = score
end
end

local function spawnRed()
local red = display.newImage(“red.png”)
physics.addBody( red, {density = 0.5, friction = 0.3} )
red.x = 208
red.y = -30
red:addEventListener( “touch”, redTouch )[/lua] [import]uid: 25216 topic_id: 11756 reply_id: 311756[/import]

Try this:

[code]

local score = 0

scoreText = display.newText( " " … score,350, 15, Font, 60)

local function redTouch(event)
if event.phase == “began” then
score = score + 1
scoreText.text = " " … score
end
end

local red = display.newImage(“red.png”)
physics.addBody( red, {density = 0.5, friction = 0.3} )
red.x = 208
red.y = -30
red:addEventListener( “touch”, redTouch )
[/code] [import]uid: 18445 topic_id: 11756 reply_id: 42815[/import]

Please don’t make duplicate threads; just edit the original. I’ve deleted your other.

Peach [import]uid: 52491 topic_id: 11756 reply_id: 42870[/import]

Thanks! I don’t think this works for multiple objects though.
My objects are assigned as blue, yellow, green, red, and purple.
I was using this:
[lua]local onGoal = function (event)
local score = 0
if event.phase == “began” then
if self.name == “blue” then
score = score + 0.5
elseif self.name == “red” then
score = score + 1
elseif self.name == “green” then
score = score + 2
elseif self.name == “yellow” then
score = score + 2
elseif self name == “purple” then
score = score + 2
end
end
end
scoreText = display.newText( " " … score, 350, 15, Font, 60)[/lua]
It doesn’t seem to be working. I’m using this with touch listeners. For example, blue:addEventListener( "touch", onGoal ).
Can you help me with this? :confused:
[import]uid: 25216 topic_id: 11756 reply_id: 43090[/import]

Each colour has an amount that it adds to your score, right? Consider adding that value as a property of your display object. Then, in your listener, get a reference to the target and retrieve the “score amount” to add to the score variable. [import]uid: 26769 topic_id: 11756 reply_id: 43091[/import]

Yeah, each different color adds a different amount of score. What do you mean as a property? Like [lua] red = 2 [/lua]?
Could you clarify a bit?
Thanks for helping me! :slight_smile: I’m such a noob [import]uid: 25216 topic_id: 11756 reply_id: 43109[/import]

Ok so this is pretty weird. I have my score and text all set up, but the score isn’t even coming up on my screen!
I have it at the top of the document.
[lua]local score = 0
local scoreText = display.newText( " " …score, 350, 15, Font, 60 )[/lua] [import]uid: 25216 topic_id: 11756 reply_id: 43132[/import]

Yay! I got it! Somehow, it fixed its self and now it works great! Now the only problem is that the actual elements that fall from the sky, overlap the text. Is there an easy way to fix this? [import]uid: 25216 topic_id: 11756 reply_id: 43134[/import]

To control the draw order, you need to use displayGroups.

Create one group for all the game elements, and one for the GUI (score) elements. Then add them to the master displayGroup in the correct order lowest first.

As a hierarchy you should have this:

Master Display Group
|_Game group
|_GUI group

[lua]–create the groups
local Master = display.newGroup();
local Game = display.newGroup();
local GUI = display.newGroup();

–any object you want on a group is added like so
GUI:insert(scoreObject);

–as a last step, add the children display groups to the master
Master:insert(Game); – this will be on the bottom
Master:insert(GUI); – this will be above the Game group[/lua]

Hope that helps some. [import]uid: 5317 topic_id: 11756 reply_id: 43142[/import]

@DLuksa

Yes, that’s basically what I mean.

Where you have:

local red = display.newImage("red.png")  

you could give the object - red - a scoreAmount property:

local red = display.newImage("red.png")  
red.scoreAmount = 1  

Then in your event listener you can access this property something like this:

[code]
local function onObjectTap(event)

if (event.phase == “began”) then
local target = event.target

score = score + target.scoreAmount
end

end
[/code] [import]uid: 26769 topic_id: 11756 reply_id: 43281[/import]