Can someone help me with score code?

Hi! How can I make score update after object pass a y=100, I mean that i want something like a 

if spike(this is my object).y < 100 then

score = score + 1
scoreNumber.text = score

or something like a

if spike.y == 100 then

score = score + 1
scoreNumber.text = score

I’m pretty new to lua language and I dont know if I need a EventListener there and how to write this. I tried

if spike.y == 100 then

print (“something”)

and it doesnt say anything

Here’s the issue with both of your blocks of code.

With the first one, if you keep calling the code while object.y <= 100 then it will keep adding numbers to the score as long as the object’s .y is less than 100.

With your second one, it will do the work only when spike.y is exactly 100.  Depending on how you’re moving spike, y might not ever be exactly 100.

The solution is to use two tests, one the y position of the object and a flag to indicate if you’re already scored that point.  When you create spike, do something like:

spike.hasBeenScored = false

Then in your movement code where you’re testing to see if .y  has passed the 100 point spot you could do:

if spike.y <= 100 and spike.hasBeenScored == false then

     score = score + 1

     scoreNumber.text = score

     spike.hasBeenScored = true

end

Rob

Define a variable

playerPoints = 0

Then display the text:

scoreDisplay = display.newText( ('Points: ' .. playerPoints), 330, -10, native.systemFont, 20 ) scoreDisplay:setFillColor( 255, 255, 255 ) --This will make it white scoreDisplay:translate(display.contentHeight /2, 30)

Then have an update function:

function update( ) scoreDisplay.text = 'Points: ' .. playerPoints .. '

if (spike.y =< 100) then

playerPoints = playerPoints + 5

This isn’t my own code, but is derived and edited from the ‘cannon game’ code that my college uses as a tutorial.

You might also want an event Listener.  But I’m not sure what exactly.  In our code it is this:

Runtime:addEventListener('enterFrame', update)

Here’s the issue with both of your blocks of code.

With the first one, if you keep calling the code while object.y <= 100 then it will keep adding numbers to the score as long as the object’s .y is less than 100.

With your second one, it will do the work only when spike.y is exactly 100.  Depending on how you’re moving spike, y might not ever be exactly 100.

The solution is to use two tests, one the y position of the object and a flag to indicate if you’re already scored that point.  When you create spike, do something like:

spike.hasBeenScored = false

Then in your movement code where you’re testing to see if .y  has passed the 100 point spot you could do:

if spike.y <= 100 and spike.hasBeenScored == false then

     score = score + 1

     scoreNumber.text = score

     spike.hasBeenScored = true

end

Rob

Define a variable

playerPoints = 0

Then display the text:

scoreDisplay = display.newText( ('Points: ' .. playerPoints), 330, -10, native.systemFont, 20 ) scoreDisplay:setFillColor( 255, 255, 255 ) --This will make it white scoreDisplay:translate(display.contentHeight /2, 30)

Then have an update function:

function update( ) scoreDisplay.text = 'Points: ' .. playerPoints .. '

if (spike.y =< 100) then

playerPoints = playerPoints + 5

This isn’t my own code, but is derived and edited from the ‘cannon game’ code that my college uses as a tutorial.

You might also want an event Listener.  But I’m not sure what exactly.  In our code it is this:

Runtime:addEventListener('enterFrame', update)