assigning values to an array of images help please

Hi,

I am going to be quite precise in what I have been battling for what seems like eons.

I have a set of 5 sprites.

They are put into a loop which is passed to another loop which creates a grid of enemies (think space invaders).

When enemies are shot the score is increased.

Fine no problems.

My problem is that the enemies when shot need to each display a different score.

So

row 1 should display 100pts

row 2 should display 100pts

row 3 should display 200pts

row 4 should display 200pts

row 5 should display 300pts

The issue is I created the sprites in a loop, I tried to assign values and pass as an argument to the create function but no good. I am afraid I just dont get it.

Please look at the code to understand the problem as it should become clear, so any solutions would be awesome please.

The code below is what I started with when I have just a single score.

Here is the modified version I had a crack at: http://pastebin.com/2NXevHz5

The modified versio is nearly working but as you can see when the loop is ran it will always report a score of 500 for ALL enemies.

[lua]

SPRITE SHEET + FUNCTION

local public = {}

local sheets = {}
local sequences = {}

public.sheets = sheets
public.sequences = sequences

sheets.invaders = {}

public.createSpriteStuff = function()

    local sheetData = {
        width = 53,  
        height = 38, 
        numFrames = 2,
    }

  local i
  for i=1,5 do
    sheets.invaders[i] = graphics.newImageSheet(“images/sprite”…i…".png", sheetData )

  end

  sequences.normRun =
    {
        name = “normalRun”,
        start = 1,
        count = 2,
        time = 1710,
        loopCount = 0,
        loopDirection = “forward”
    }

end

return public

MAIN.LUA

function createInvader(xloc, yloc, row)

  scene = display.newGroup()
  if levelDrop > 350 then
     levelDrop = 350
    end

  for j = 1, 5 do
    for i = 1, 11 do
      allEnemys[#allEnemys + 1] = enemys:new()
      allEnemys[#allEnemys]:init(i * 60, j * 70 + levelDrop, j)  spacing
      allEnemys[#allEnemys]:start()

    end
  end
end

ENEMY.LUA

function enemys:new()
  local group = {}
   return setmetatable( group, enemys_mt )
end

function enemys:init(xloc, yloc, row )

    self.image = display.newSprite(budSprites.sheets.invaders[row], budSprites.sequences.normRun)
    self.image:setReferencePoint( CenterReferencePoint )
    self.image.x = xloc
    self.image.y = yloc
    self.image.name = “enemy”  
    self.image:play()
end

–SCORE IN ENEMY.LUA–

local currentScore = getScores( )
                    currentScore = currentScore  --JUST ADD THE VALUE HERE.e.g.+ myValue
                    setScores( currentScore )

          end

[lua]

Also if anybody has worked out how to wrap code in nice tags do let me know, it dosnt work for me no more.

Let me get this straight (because I hate reading lots of other people’s code)…

You want to load 5 different images. Those images are used (perhaps randomly) to create a grid of (for example) 5 by 5 columns and rows. Each image in the grid has a point for when it is shot. The rows at the bottom have a lower point value than the rows at the top, with increasing values in-between.

Is that correct?

If so, I’m going to see if I can write that as a free-standing piece.

yes, that is correct, but I have the whole thing working albeit for the scores being different.

The issue I believe is the way I put he sprites into the array.

If I was able to reference the sprite I could do it , hence wanting to know how I could do so .e.g.

sprite1.png - sprite2.png etc by assigning a value to each then just pulling the value when a collision happens and adding it to the score.

My code only has the important bits. thank you

From what I see, it looks like you’re not assigning a “.hitPoints = xxx” property to each space invader you’re creating.

ok, I have an update.

I went down many paths with this and in the end had to ask for guidance and help.

What I learned from this is, think simple first, dont fall into the trap thinking the issue is harder to solve than it is.

I am not saying things are easy to solve, they are once you have the answer, the thought process and experience of the guider makes it look that way.

the values are simply applied to “j” the var for building the rows with a simple table, I was working with the init stage trying to pass values from there …doh!

This is how it was fixed to achieve the required result:

[lua]

–MAIN.LUA–

local scores = { 500, 400, 300, 200, 100 }

  for j = 1, 5 do
    for i = 1, 11 do

      local anEnemy = enemys:new()
      anEnemy.myValue = scores[j]  – assigned here–

      allEnemys[#allEnemys + 1] = anEnemy
      allEnemys[#allEnemys]:init(i * 60, j * 70 + levelDrop, j)
      allEnemys[#allEnemys]:start()
 

–AND IN THE SCORE UPDATE–

–ENEMY.LUA–

local myValue = self.myValue
           
            currentScore = currentScore + myValue
            setScores( currentScore )

[/lua]

Let me get this straight (because I hate reading lots of other people’s code)…

You want to load 5 different images. Those images are used (perhaps randomly) to create a grid of (for example) 5 by 5 columns and rows. Each image in the grid has a point for when it is shot. The rows at the bottom have a lower point value than the rows at the top, with increasing values in-between.

Is that correct?

If so, I’m going to see if I can write that as a free-standing piece.

yes, that is correct, but I have the whole thing working albeit for the scores being different.

The issue I believe is the way I put he sprites into the array.

If I was able to reference the sprite I could do it , hence wanting to know how I could do so .e.g.

sprite1.png - sprite2.png etc by assigning a value to each then just pulling the value when a collision happens and adding it to the score.

My code only has the important bits. thank you

From what I see, it looks like you’re not assigning a “.hitPoints = xxx” property to each space invader you’re creating.

ok, I have an update.

I went down many paths with this and in the end had to ask for guidance and help.

What I learned from this is, think simple first, dont fall into the trap thinking the issue is harder to solve than it is.

I am not saying things are easy to solve, they are once you have the answer, the thought process and experience of the guider makes it look that way.

the values are simply applied to “j” the var for building the rows with a simple table, I was working with the init stage trying to pass values from there …doh!

This is how it was fixed to achieve the required result:

[lua]

–MAIN.LUA–

local scores = { 500, 400, 300, 200, 100 }

  for j = 1, 5 do
    for i = 1, 11 do

      local anEnemy = enemys:new()
      anEnemy.myValue = scores[j]  – assigned here–

      allEnemys[#allEnemys + 1] = anEnemy
      allEnemys[#allEnemys]:init(i * 60, j * 70 + levelDrop, j)
      allEnemys[#allEnemys]:start()
 

–AND IN THE SCORE UPDATE–

–ENEMY.LUA–

local myValue = self.myValue
           
            currentScore = currentScore + myValue
            setScores( currentScore )

[/lua]