Trying to get an 'enemy counter' working.

I already have player lives dealt with, now I am trying to display the current number of enemies via a table of images (numbers) and have it updating the image to correspond with the current value of ‘EnemyCount’.

I have been reading this article and trying to reverse-engineer it , but with no luck: http://omnigeek.robmiracle.com/2011/05/30/implementing-a-health-status-bar-in-corona-sdk/

I have a variable specifying how many enemies are in the current level EnemyCount = 3 for example, and whenever an enemy is destroyed I have it decreasing the value EnemyCount = EnemyCount - 1

This is as far as I got:
[lua]
local numberTable =
    {
    “GUI_assets/num_1.png”,
    “GUI_assets/num_2.png”,
    “GUI_assets/num_3.png”,
    “GUI_assets/num_0.png”,
    }
local numberIndex

function updateECN()
    
    numberIndex = EnemyCount
    print(numberIndex)
    current_EC = display.newImage(numberTable[numberIndex])
    current_EC.x = 900
    current_EC.y = 30    
end
Runtime:addEventListener(“enterFrame”, updateECN)
[/lua]

It changes the number, but the previous image is still being displayed.
Idk why, but I can’t think of the logic behind this seemingly simple concept. :stuck_out_tongue:

Anyone have a suggestion?

-Saer

(Forgive the poor line-spacing. I used the [l u a] text format option instead of the blue arrow things  :stuck_out_tongue: )

Well I’ve got it almost working!
The problem now is that it won’t show the initial count, only when the count is decreased for the first time does the number image appear.
Otherwise, when I shoot an enemy and the EnemyCount decreases, the number image is removed and replaced by the appropriate number image.

Code:
[lua]

local current_EC
local numberIndex = EnemyCount

local numberTable =
    {
    “GUI_assets/num_1.png”,
    “GUI_assets/num_2.png”,
    “GUI_assets/num_3.png”,
    “GUI_assets/num_0.png”,
    }

function updateECN()
    
    display.remove(current_EC)
    current_EC = nil    
    
    numberIndex = EnemyCount
    print(numberIndex)
    current_EC = display.newImage(numberTable[numberIndex])
    current_EC.x = 900
    current_EC.y = 30  
    
end

function checkECN()
    
    if numberIndex ~= EnemyCount then
    
           timer.performWithDelay(50, updateECN)
    end
end    
Runtime:addEventListener(“enterFrame”, checkECN)
[/lua]

Any thoughts?

-Saer

Edit: I know the first image ins’t appearing because of these two lines:
display.remove(current_EC)
current_EC = nil  

But I’m not sure how else to format it…?

Got it!
The problem was my forward declaration of numberIndex: local numberIndex = EnemyCount
I just changed it to local numberIndex and that fixed it. :smiley:

-Saer
 

Well I’ve got it almost working!
The problem now is that it won’t show the initial count, only when the count is decreased for the first time does the number image appear.
Otherwise, when I shoot an enemy and the EnemyCount decreases, the number image is removed and replaced by the appropriate number image.

Code:
[lua]

local current_EC
local numberIndex = EnemyCount

local numberTable =
    {
    “GUI_assets/num_1.png”,
    “GUI_assets/num_2.png”,
    “GUI_assets/num_3.png”,
    “GUI_assets/num_0.png”,
    }

function updateECN()
    
    display.remove(current_EC)
    current_EC = nil    
    
    numberIndex = EnemyCount
    print(numberIndex)
    current_EC = display.newImage(numberTable[numberIndex])
    current_EC.x = 900
    current_EC.y = 30  
    
end

function checkECN()
    
    if numberIndex ~= EnemyCount then
    
           timer.performWithDelay(50, updateECN)
    end
end    
Runtime:addEventListener(“enterFrame”, checkECN)
[/lua]

Any thoughts?

-Saer

Edit: I know the first image ins’t appearing because of these two lines:
display.remove(current_EC)
current_EC = nil  

But I’m not sure how else to format it…?

Got it!
The problem was my forward declaration of numberIndex: local numberIndex = EnemyCount
I just changed it to local numberIndex and that fixed it. :smiley:

-Saer