changing image function of score

hi,

I would like to change a background image depending on the score(“number”)

but my problem is that my background superimposes them because the functions are repeated with the runtime.

What is the best way to do what i would like ?

thanks for your response :slight_smile:

local numberglass = 1 local flagGlass = 0 local imageGlass = {} imageGlass[1]="eclat.png" imageGlass[2]="eclat2.png" imageGlass[3]="eclat3.png" imageg=imageGlass[numberglass] local function glassLevel1() if flagGlass == 0 then flagGlass = 1 local glass = display.newImage(imageGlass[numberglass]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 end return glass, flagGlass, flagGlassRemove end local function glassLevel2() if flagGlass == 1 then flagGlass = 0 display.remove(glass) glass = nil &nbsp; local glass = display.newImage(imageGlass[numberglass + 1]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 end return glass, flagGlass end local function glassLevel3() if flagGlass == 0 then flagGlass = 1 display.remove(glass) glass = nil local glass = display.newImage(imageGlass[numberglass + 1]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 end return glass, flagGlass end local function testNumber (event) print("number", number) &nbsp;&nbsp;&nbsp; if number \< 10 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel1() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if number \>= 10 and number \< 20 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel2() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if number \>= 20 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel3() &nbsp;&nbsp;&nbsp; end end Runtime:addEventListener("enterFrame", testNumber)

Anybody ?

I wouldn’t use enterFrame. I would call the testNumber function whenever there’s an update in the score, not every frame.

hi@atrizhong,

I tried what you said but I still have the same concern; this is probably due to the fact that my score function is bound at runtime. But I do not see how to update my score otherwise.

I can not use the collision as described in the forum (“tips and optimization”) because my score is associated with a range of x = 10 to x = 100 …

--In your collision handler (when enemy gets hit with a bullet you fired local function onCollision(event) --Where Bullet collides with enemy score = score + 1 score.text = "score" .. score end

here below what i tried :

local flagGlass = 0 local imageGlass = {} imageGlass[1]="eclat2.png" imageGlass[2]="eclat.png" imageGlass[3]="eclat2.png" imageg=imageGlass[numberglass] local function removeGlass() print("removeGlass") display.remove(glass) glass = nil end local function glassLevel1() flagGlass = 1 print("glassLevel1") local glass = display.newImage(imageGlass[1]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 return glass, flagGlass end local function glassLevel2() flagGlass = 0 print("glassLevel2") local glass = display.newImage(imageGlass[2]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 return glass, flagGlass end local function glassLevel3() flagGlass = 1 print("glassLevel3") local glass = display.newImage(imageGlass[3]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 return glass, flagGlass end local function score() &nbsp;&nbsp;&nbsp; number = mathRound(number + 1) &nbsp;&nbsp;&nbsp; textScore.text = number &nbsp;&nbsp;&nbsp; if number \> 10 and number \<= 20 and flagGlass == 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel1() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if number \>20 and number \<= 30 and flagGlass == 1&nbsp; then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removeGlass() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel2() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if number \> 30 and flagGlass == 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removeGlass() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel3() &nbsp;&nbsp;&nbsp; end return textScore end

my runtime testPosition to trigger the function score

local function testPosition (event) &nbsp;&nbsp;&nbsp; if CircleonTransition.x \< sideAlertLeftScore then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; score() &nbsp;&nbsp;&nbsp; end Runtime:addEventListener("enterFrame", testPosition)

any idea ?

Anybody ?

I wouldn’t use enterFrame. I would call the testNumber function whenever there’s an update in the score, not every frame.

hi@atrizhong,

I tried what you said but I still have the same concern; this is probably due to the fact that my score function is bound at runtime. But I do not see how to update my score otherwise.

I can not use the collision as described in the forum (“tips and optimization”) because my score is associated with a range of x = 10 to x = 100 …

--In your collision handler (when enemy gets hit with a bullet you fired local function onCollision(event) --Where Bullet collides with enemy score = score + 1 score.text = "score" .. score end

here below what i tried :

local flagGlass = 0 local imageGlass = {} imageGlass[1]="eclat2.png" imageGlass[2]="eclat.png" imageGlass[3]="eclat2.png" imageg=imageGlass[numberglass] local function removeGlass() print("removeGlass") display.remove(glass) glass = nil end local function glassLevel1() flagGlass = 1 print("glassLevel1") local glass = display.newImage(imageGlass[1]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 return glass, flagGlass end local function glassLevel2() flagGlass = 0 print("glassLevel2") local glass = display.newImage(imageGlass[2]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 return glass, flagGlass end local function glassLevel3() flagGlass = 1 print("glassLevel3") local glass = display.newImage(imageGlass[3]) glass.x = 240 glass.y = 160 glass.xScale = 0.5 glass.yScale = 0.5 return glass, flagGlass end local function score() &nbsp;&nbsp;&nbsp; number = mathRound(number + 1) &nbsp;&nbsp;&nbsp; textScore.text = number &nbsp;&nbsp;&nbsp; if number \> 10 and number \<= 20 and flagGlass == 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel1() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if number \>20 and number \<= 30 and flagGlass == 1&nbsp; then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removeGlass() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel2() &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if number \> 30 and flagGlass == 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; removeGlass() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glassLevel3() &nbsp;&nbsp;&nbsp; end return textScore end

my runtime testPosition to trigger the function score

local function testPosition (event) &nbsp;&nbsp;&nbsp; if CircleonTransition.x \< sideAlertLeftScore then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; score() &nbsp;&nbsp;&nbsp; end Runtime:addEventListener("enterFrame", testPosition)

any idea ?