Change Score Text when Scored

Hi,

I´m making a small game where a player has to flick a ball through a hoop. Every time he does so, he should get 10 extra points.

Unfortunatly every time he scores, the scoreText variable changes as it should, but the original score var, is still displayed. So the “0” points with which you start out are still displayed and overlap with the points scored when the player flicks the ball through the hoop.

The problem occurred when I linked the game.lua file with the menu.lua file using the Director Class. First the error module said “score” could not be indexed as a global variable, so I took it out of the “bullet” module and put it in the main.lua file. Then the error described above occurred. I tried putting it in the game.lua file, but it gave the same result.

So my question is , where do I have to insert the variables so that the score changes when needed, and the original “0” score dissapears ?

This is the main.lua file:
[lua]_W = display.contentWidth
_H = display.contentHeight

score = 0
scoreText = display.newText( " " … score,0, 15, Helvetica, 50)

local director = require (“director”)

local mainGroup = display.newGroup()

local function main ()
mainGroup:insert(director.directorView)

director:changeScene(“menu”)

return true

end

main ()[/lua]

This is the part of the bullet.lua file (a module of game.lua) responsible for score changing. Putting the score variables in that function didn´t help either.
[lua] function bullet:collision (event)

if event.other.myName == “bar” then
bullet.isLoaded = true
end
if bullet.isLoaded == true and event.other.myName == “basket” then

score = score + 10
scoreText.text = " " … score
end
end
bullet:addEventListener(“collision”, bullet)[/lua]
Any ideas ?

Great thanks !!

Jan [import]uid: 126207 topic_id: 29363 reply_id: 329363[/import]

Try explicitly defining your globals like this:

[lua]_G.score = 0
_G.scoreText = display.newText( " " … score,0, 15, Helvetica, 50)[/lua]

…and use the same _G. prefix when addressing the variables in other modules. Sometimes they still work if you don’t use _G and sometimes they don’t, I’ve never been able to figure out why. [import]uid: 93133 topic_id: 29363 reply_id: 118002[/import]

Using _G. is a best practice for globals, but it should have still worked.

I see two possibilities:

  1. Since score is global, you might be setting it back to 0 somewhere else.
  2. More likely is your condition in the if statement is failing.
 print(""bullet.isLoaded is: ", bullet.isLoaded, "colliding with", event.other.myName)  
 if bullet.isLoaded == true and event.other.myName == "basket" then  
 score = score + 10  
 scoreText.text = " " .. score  
 print("Hey, I just updated the score. It's now:", score)  
 end  

Drop those two prints in and see what it says in your console log to make sure you’re really getting inside that IF.
[import]uid: 19626 topic_id: 29363 reply_id: 118013[/import]

Thanks guys. I´ll try both things out and keep you updated. It worked perfectly before I implented the Director Class code. Maybe I should wrap the global variables in a group or something ? [import]uid: 126207 topic_id: 29363 reply_id: 118025[/import]

Allright, I tried both suggestions:

> Using the _G.score and _G.scoreText unfortunately didn´t change the error.

> I implented both print statements, both fired perfectly on both collissions.

The problem still is that the changing scoreText overlaps with the 0 score, which doesn´t dissapear ;- (.

I have other global variables to fire the bullet, and they work fine.

I´ll paste the entire code below, if you would come up with something be sure to let me know !
– Main.lua

[lua]_W = display.contentWidth
_H = display.contentHeight

local director = require (“director”)

local mainGroup = display.newGroup()

local function main ()
mainGroup:insert(director.directorView)

director:changeScene(“menu”)

return true

end

main ()[/lua]

– Menu.lua

[lua]module(…,package.seeall)

function new()

local localGroup = display.newGroup()

local bg = display.newImageRect(“images/bg.png”,_W,_H)
bg:setReferencePoint(display.CenterReferencePoint)
bg.x = _W/2
bg.y =_H/2

local play_btn = display.newImageRect(“images/play_btn.png”, _W/10,75)
play_btn:setReferencePoint(display.CenterReferencePoint)
play_btn.x = _W/2
play_btn.y = _H/2
play_btn.scene = “game”

function changeScene(e)

if (e.phase == “ended”) then
director:changeScene(e.target.scene)
end

end

localGroup:insert(bg)
localGroup:insert(play_btn)

play_btn:addEventListener(“touch”, changeScene)

return localGroup
end[/lua]

– game.lua

[lua]module(…,package.seeall)

function new ()

local localGroup = display.newGroup()
_W = display.contentWidth
_H = display.contentHeight
velocity = 70
halfpoint = 0

local physics = require(“physics”)
physics.start( true )

local bullets = {}

local function spawnBullets(total)
for i = 1, total do
bullets[i] = bullet.newBullet()
– flag bullets for removal later
bullets[i].remove = true

end
end

local bullet = require(“bullet”)
local bar = require (“bar”)
local basket = require (“basket”)
spawnBullets(5)
return localGroup
end[/lua]

– Bullet.lua

[lua]module(…, package.seeall)

function newBullet()

local bullet = display.newImage(“images/bullet.png”)
bullet.x = 100; bullet.y = _H - 100

force_multiplier = 4

score = 0
scoreText = display.newText( " " … score,0, 15, Helvetica, 50)
– set up some properties

bullet.myName = “bullet”

physics.addBody(bullet, “kinematic”, {density = 0.2, friction = 0.2, bounce = 0.5, radius = 20})
bullet.linearDamping = 0.3
bullet.angularDamping = 0.8
bullet.isBullet = true – force continuoius collision detection, to stop really fast shots from moving through
–bullet.isSensor = true
bullet.isLoaded = false

function bullet:touch(e)
local t = e.target

if (e.phase == “began”) then
display.getCurrentStage():setFocus(t)
self.isFocus = true

self.bodyType = “kinematic”

– Stop current motion if any
self:setLinearVelocity(0,0)
self.angularVelocitiy = 0

myLine = nil
elseif(self.isFocus) then
if(e.phase == “moved”)then

if(myLine)then
myLine.parent:remove(myLine)
end
myLine = display.newLine(self.x,self.y,e.x,e.y)
myLine:setColor(255255,255,50)
myLine.width =8

elseif(e.phase == “ended” or e.phase == “cancelled”)then

display.getCurrentStage():setFocus(nil)
self.isFocus=false

–audio.play(shot)

if(myLine)then
myLine.parent:remove(myLine)
end

– Launch the bullet
self.bodyType=“dynamic”
self:applyForce((self.x - e.x)*force_multiplier, (self.y - e.y)*force_multiplier, self.x,self.y)

function bullet:collision (event)

if event.other.myName == “bar” then
bullet.isLoaded = true
print("bullet.isLoaded is: ", bullet.isLoaded, “colliding with”, event.other.myName)
end
if bullet.isLoaded == true and event.other.myName == “basket” then

score = score + 10
scoreText.text = " " …score
print(“Hey, I just updated the score. It’s now:”, score)
end
end

end
end

end
bullet:addEventListener(“touch”, bullet)
bullet:addEventListener(“collision”, bullet)
Runtime:addEventListener(“enterFrame”,bullet)
return bullet
end[/lua]
[import]uid: 126207 topic_id: 29363 reply_id: 118034[/import]

In this code you are creating a new copy of scoreText everytime you create a new bullet and making it zero. You should only create scoreText once and it should be outside the bullet creation code. Perhaps the same place you create the background. [import]uid: 19626 topic_id: 29363 reply_id: 118036[/import]

< SOLVED>
Originally the score values we´re in the game.lua file, below Velocity and Halfpoint. But when I put the code there I got an error : "attemt to perform an arithemtic on global `score´ ".

But you are right. I thought I tried to put it everywhere, but now I pasted the Score code just above the “function newBullet line”. It works fine now, except for one strange thing. Normally only ten points should be added if the score conditions are met, but somehow it gives you twice the score ? So if you flick the bullet through the hoop, it jumps from 0 to ten and immediately to 20.
I´ll work on that some more, but thanks again, first and most important problem is solved .-).

[import]uid: 126207 topic_id: 29363 reply_id: 118043[/import]

You probably need to check for the event.phase of the collision, otherwise it will fire at the beginning and end of the collision. [import]uid: 93133 topic_id: 29363 reply_id: 118046[/import]