How to update variables

This has been bugging me for a long time… The score system, making my ball bigger, making my walls close in on me, etc! I’ve got a variable that determines the size of my ball, but how can I change this variable throughout the game?

local ball = display.newImage("Images/Ball.png", xpos, ypos)  
local ballSize = 8  
  
-- Scale  
ball.yScale = ballSize/125  
ball.xScale = ballSize/125  
  
local function onCollisionBall(self, event)  
 if (event.phase == "began" and event.other.class == "enemy") then  
  
 -- Remove enemy  
 event.other:removeSelf()  
  
 -- Get bigger  
 ballSize = ballSize + 1  
  
 return true   
 end  
end  

This is my current code…

Thanks in advance, [import]uid: 14018 topic_id: 5403 reply_id: 305403[/import]

That looks like the variable changes, put in a print() statement if you want to double-check.

Your real issue I assume is that you want the ball to get bigger, in which case you need to call the xScale and Yscale lines again. They aren’t permanently tied to the variable, just instantaneously set to the value of the variable one time.

Which incidentally means that variable is kind of redundant. Just add to xScale directly, rather than setting some other variable. [import]uid: 12108 topic_id: 5403 reply_id: 18079[/import]

Thanks!=)

Edit: But then I’d also have to change the physics radius of the ball while I change the scale. How do you do this? Should I use the same physics.addBody() code and just change the radius?

Edit: And will this technique work for the score? Because the score is stored inside a variable at the moment, and I have no idea how to construct a score system without the usage of a variable that changes? [import]uid: 14018 topic_id: 5403 reply_id: 18082[/import]

. [import]uid: 14018 topic_id: 5403 reply_id: 18103[/import]

I tried wrapping the variable in a function and adding an event listener but it doesn’t work either… The variable gets ignored and the ball won’t scale and everything beyond the function stops working

local function ballSizeFunction(event)

local ballSize = 8

end
Runtime:addEventListener(“enterFrame”, ballSizeFunction)

Also tried this

ball:addEventListener(“enterFrame”, ballSizeFunction)

I really have no idea on how to solve this, all help appreciated [import]uid: 14018 topic_id: 5403 reply_id: 18112[/import]

…bump [import]uid: 14018 topic_id: 5403 reply_id: 18243[/import]

It obviously depends on what you want to do especifically for your game.

If you want the score to be updated only on collisions then you’re on the right track.

Set up a display.newText() in your display group and a “score” variable. You’ll update “score” at the same as the ball size, on the collision function. Then you can also update the ScoreText you set up earlier: ScoreText.text = score. You’ll probably want to convert to string and format nicely.

In the same collision function, you can remove the ball and create a new one with a bigger physics body I guess.

If you want the score to update constantly (thing endless runner) just put it inside an enterframe listener. [import]uid: 10835 topic_id: 5403 reply_id: 18249[/import]

But how do I call the score variable on collision? score = 0 at the start of the app, and showScore shows 0 points. When the main ball collides with something, I update the score to score = score + 20. I assume this updates the variable, but showScore still shows 0… If I add all of the score-code inside a function and add a eventframe listener like so:

function scoreFunction(event)  
if(event.phase == "began") then  
  
score = 0  
  
local showScore = display.newText(score, 18, 13)  
showScore:setTextColor(125, 255, 255)  
showScore.size = 15  
  
scoreGroup:insert(showScore)  
localGroup:insert(showScore)  
  
scoreGroup:toFront()  
end  
end  
Runtime:addEventListener("enterFrame", scoreFunction)  

i feel it should work, because I have it checked all the time? But it doesn’t. None of this shows on the screen… When I start the app, I can’t see the 0 and when I collide to gain score it wont display the new score, etc…

And same goes with the ballSize… If I add the ballSize inside a function with an eventframe listener the program will just ignore the ballsize variable which ruins 90% of my app. Also it ignores everything beyond that function… [import]uid: 14018 topic_id: 5403 reply_id: 18261[/import]

No, that’s all wrong. You are just setting up the text every frame, but you’re not updating anything. Do the setup in a setup function that you call only when the game starts.

You had it right on your first post. Like I mentioned before if you want the score to be updated when the ball hits an enemy then INSIDE your collision function add this:

[lua] score = score + 25
showScore.text = score[/lua]

With that everytime the ball collides with the enemy the score increases by 25. Then you update the text parameter of your show score, so the text being displayed by the display.newText() object you create in your set up function will be updated to the current score. [import]uid: 10835 topic_id: 5403 reply_id: 18263[/import]

Gahh…

function scoreFunction(event)  
  
-- Funktion för score  
score = 0  
  
-- Visa poäng  
local showScore = display.newText(score, 18, 13)  
showScore:setTextColor(125, 255, 255)  
showScore.size = 15  
  
-- Lägg in i grupp  
scoreGroup:insert(showScore)  
localGroup:insert(showScore)  
  
-- Skicka poänggrupp längst fram  
scoreGroup:toFront()  
end  
Runtime:addEventListener("enterFrame", scoreFunction)  

With this code, the score now shows as 0 at startup, but after couple of seconds the application drops to like 5fps. I guess it’s because I’ve got the Runtime in there, but if I change it to showScore:addEventListener, the score will once again not show…

Also, when I’ve got this Runtime and the score’s showing, I add this to the collision function:

-- Collision  
local function onCollisionBall(self, event)  
 if (event.phase == "began" and event.other.class == "enemy") then  
  
 -- Remove enemy  
 event.other:removeSelf()  
  
 -- Score  
 score = score + 10  
 showScore.text = score  
  
 return true   
 end  
end  
ball.collision = onCollisionBall  
ball:addEventListener("collision", ball)  

But it won’t update the score [import]uid: 14018 topic_id: 5403 reply_id: 18265[/import]

The second function should be working. The problem is your first function.

I suggest you thoroughly reread the docs, but I’ll try to steer you in the right direction.

Think carefully about what you’re doing on the first function. What you want to do is call all of that ONCE and ONCE only. Why are you using and enterframe listener? Essentially what you’re doing is reset the score to 0 on every frame. That’s why it appears as if it’s not updating the score on the collision function. Because as soon as it increases you set it back to 0 the next frame.

Just call your score function normally when you’re setting up the scene. Also it doesn’t need any parameters, there’s no event associated with it.

function scoreFunction()
bla bla
end

And at the end of your code have something like:
scoreFunction()

Then on your gameover function when you hit the retry button call scoreFunction() again.

I hope it clears it up a little.

[import]uid: 10835 topic_id: 5403 reply_id: 18271[/import]

function scoreFunction()  
  
-- Funktion för score  
score = 0  
  
-- Visa poäng  
local showScore = display.newText(score, 18, 13)  
showScore:setTextColor(125, 255, 255)  
showScore.size = 15  
  
-- Lägg in i grupp  
scoreGroup:insert(showScore)  
localGroup:insert(showScore)  
  
-- Skicka poänggrupp längst fram  
scoreGroup:toFront()  
end  
scoreFunction()  

Now this works. When I load the game I see the 0 there, but now I’m back to my original problem, updating that variable… When I collide with my enemies I’m not gaining score…

-- Collision  
local function onCollisionBall(self, event)  
 if (event.phase == "began" and event.other.class == "enemy") then  
  
 -- Remove enemy  
 event.other:removeSelf()  
  
 -- Score  
 score = score + 10  
 showScore.text = score  
  
 return true   
 end  
end  
ball.collision = onCollisionBall  
ball:addEventListener("collision", ball)  

This is not my day mate I appreciate your help [import]uid: 14018 topic_id: 5403 reply_id: 18283[/import]

I’m assuming the collision is working and it’s firing correctly. I usually put print statements in such functions. Something like print(“Collision ocurred”). So you know it’s in fact working. You could also try printing the score.

Try declaring showScore outside the function. Otherwise it’ll be local only to that function and you won’t be able to use its values outside.

At the beginning of your code:
local showScore

And inside the function, delete where it says “local”. See if that does it. [import]uid: 10835 topic_id: 5403 reply_id: 18291[/import]

Thanks for all your help mate! I tried putting showScore outside of the function but it was still the same, then I tried removing everything that had anything to do with showScore from the function, leaving only score = 0 inside, and it works now! [import]uid: 14018 topic_id: 5403 reply_id: 18295[/import]