Multiple Sprites In A Table

I have posted this previously but although helpful responses It didn’t answer my question.

I am really stumped on this one and having read,searched the internet and used the #corona ICQ channel for what seems like days I have got no further.

Maybe this just cant be done but it seems very unlikely.

Anyway to the question:

I have a grid formation of images, think space invaders style.

I have 5 columns and 11 rows, each row displays a different object(image).

The images are put on screen using a loop.

The images move across the screen using an enterFrame listener.

I have 2 options either would do the job, option 2 preferred, however option 1 would be fine also.

  1. I want to change the images every 10 pixels or so for another image then back again after 10 pixels. So 2 images per row.

   (think space invaders again)

    I can do this but not implement it into the loop.

  1. A better option would be to change the images for Sprites, I can then adjust the spritesheet data for the animation.

    I am confident in the use of Sprite sheets.

I use an external spritesheet for sprite data and require into main lua using the below:

[lua]display.newSprite(mySprites.sheets.spritesheet, mySprites.sequences.spritesheet)[/lua]

[lua]

 – enemy.lua –

function enemys:init(xloc, yloc, row )

    self.image = display.newImage(“images/enemy”…row…".png" )  – this is where I call my different images –
    self.image:setReferencePoint( CenterReferencePoint )
    self.image.x = xloc
    self.image.y = yloc
    self.image.name = “enemy”
    self.movement =  0.5
    self.leftHit = 0
    self.rightHit = 0
    self.numberOfWallHits = 0
    self.floorHits = 0

end

[/lua]

[lua]

–main.lua–

function createEnemy(x,y,row)

          scene = display.newGroup()
          allEnemys = {}

          local enemys = require(“modules.enemy”)
          for j = 1, 5 do
          for i = 1, 11 do
          allEnemys[#allEnemys + 1] = enemys:new()
          allEnemys[#allEnemys]:init(i * 60, j* 70 +70,j )       – this takes care of the placing –
          allEnemys[#allEnemys]:start()

          end
end
end[/lua]

So anything, any help, any pointers would be gratefully received.

Thank you

Hi @bubblebobble,

I’m not sure I understand the issue. You just want to make all of these “invaders” into sprites? On line 4 of “enemy.lua”, why not just make these sprites instead of static images?

HI@Brent,

yes you understand correctly, but the question is how to implement it?

Cant be as simple as :

[lua]self.image = display.newSprite(“images/spritesheets”…row…".png" ) [/lua]

How do I implement this:

[lua]display.newSprite(mySprites.sheets.spritesheet, mySprites.sequences.spritesheet)[/lua]

in order to play them?

I think I may be missing something simple here?

It is trying to tie them together I am struggling with

How many sequences do you have? How are you looking them up from your “sequences” table?

@Brent,

I have 5 sequences for the five rows.

My budSprites.lua has this format:(note it only shows one sequence)

[lua]

local public = {}

local sheets = {}
local sequences = {}

public.sheets = sheets
public.sequences = sequences

public.createSpriteStuff = function()

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

    sheets.large = graphics.newImageSheet( “images/sprite1.png”, sheetData )

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

end

return public [/lua]

then to call sprites I use this:

[lua] budSprites.createSpriteStuff() [/lua]

then to call the animation this:

[lua] local object = display.newSprite(budSprites.sheets.large, budSprites.sequences.large)
    object:play()[/lua]

Does that help ?

Is this method not working? It should select the “normalRun” sequence out of the “large” table, since that’s the only sequence within it.

@Brent,yes,this method works perfect when calling sprites normally.

I just dont know how to implement the 5 sprites into the concatenation…row and have them display 

Basically as you said earlier, swap out the static images for sprite sequences.

How would I code the the concat …row…png ?

@Brent,

thank you for help so far.

Obviously just changing this

[lua]self.image = display.newImage(“images/enemy”…row…".png" )[/lua]

to this

[lua]self.image = display.newSprite(budSprites.sheets.invader1, budSprites.sequences.invader1)[/lua]

will display just one sprite in all the rows, now how do I feed the other sprites in?

i.e. invader1, invader2,invader3,invader4,invader5

Concatenation wont work for me in this case,and I am unsure if it would be used in this situation;  I know this isnt going to work but it would inan ideal world :slight_smile:

[lua] self.image = display.newSprite(“budSprites.sheets.invader”…row…“png”," budSprites.sequences.invader"…row…“png”)[/lua]

Obviously …row… would be singular somehow, I think

“scratches head”

In your budSprites.lua, try this :

[lua]

– this creates an array of the sheets

for i = 1, 5 do

  sheets.large[i] = graphics.newImageSheet(“images/sprite” … i … “.png”, sheetData)

end

– keep the sheetData & sequences.large the same as you have them, as you apparently are only changing the image, each image can use the

– same sheetData table and sequences.large table

– then as you want to swap the images

self.image = display.newSprite(sheets.large[1], budSprites.sequences.large)

–or

self.image = display.newSprite(sheets.large[4], budSprites.sequences.large)

[/lua]

However, you may consider combining all the sheets into one sheet.  Then create an array(table) of sequences, instead of an array(table) of sheets

[lua]

sequences.large = {

      { name=“normalRun”, start = 1,  count = 2, time = 2000, loopCount = 0},

      { name=“normalRun”, start = 3,  count = 2, time = 2000, loopCount = 0},  

      { name=“normalRun”, start = 5,  count = 2, time = 2000, loopCount = 0},  

      { name=“normalRun”, start = 7,  count = 2, time = 2000, loopCount = 0},  

      { name=“normalRun”, start = 9,  count = 2, time = 2000, loopCount = 0},  

   }

– the only thing changing on each of these sequences is the ‘start’ value. So as you can see, using an array(table) of sequences like this

– there is a lot of different ways you can manipulate these fields to use the image sheets in creative ways.

self.image = display.newSprite(sheets.large, budSprites.sequences.large[4])

[/lua]

Hope this helps.

*SOLVED*

After much reading and consulting :slight_smile:

I finally have achieved the un-expected, un-expected for me that is :slight_smile:

So, heres what got everything working.

The spritesheet lua was re written to take care of the 5 sprites, it basically ended up as a unique function module for sprites.

A loop was placed in the file which iiterated over the image sheets. 5 blocks in one function .

I was then able to use the parameter [row] in the initializer in enemy lua which was passed to init, main lua .

main.lua wasnt changed.

@cyberpark,

exactly :slight_smile:

Hi @bubblebobble,

I’m not sure I understand the issue. You just want to make all of these “invaders” into sprites? On line 4 of “enemy.lua”, why not just make these sprites instead of static images?

HI@Brent,

yes you understand correctly, but the question is how to implement it?

Cant be as simple as :

[lua]self.image = display.newSprite(“images/spritesheets”…row…".png" ) [/lua]

How do I implement this:

[lua]display.newSprite(mySprites.sheets.spritesheet, mySprites.sequences.spritesheet)[/lua]

in order to play them?

I think I may be missing something simple here?

It is trying to tie them together I am struggling with

How many sequences do you have? How are you looking them up from your “sequences” table?

@Brent,

I have 5 sequences for the five rows.

My budSprites.lua has this format:(note it only shows one sequence)

[lua]

local public = {}

local sheets = {}
local sequences = {}

public.sheets = sheets
public.sequences = sequences

public.createSpriteStuff = function()

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

    sheets.large = graphics.newImageSheet( “images/sprite1.png”, sheetData )

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

end

return public [/lua]

then to call sprites I use this:

[lua] budSprites.createSpriteStuff() [/lua]

then to call the animation this:

[lua] local object = display.newSprite(budSprites.sheets.large, budSprites.sequences.large)
    object:play()[/lua]

Does that help ?

Is this method not working? It should select the “normalRun” sequence out of the “large” table, since that’s the only sequence within it.

@Brent,yes,this method works perfect when calling sprites normally.

I just dont know how to implement the 5 sprites into the concatenation…row and have them display 

Basically as you said earlier, swap out the static images for sprite sequences.

How would I code the the concat …row…png ?

@Brent,

thank you for help so far.

Obviously just changing this

[lua]self.image = display.newImage(“images/enemy”…row…".png" )[/lua]

to this

[lua]self.image = display.newSprite(budSprites.sheets.invader1, budSprites.sequences.invader1)[/lua]

will display just one sprite in all the rows, now how do I feed the other sprites in?

i.e. invader1, invader2,invader3,invader4,invader5

Concatenation wont work for me in this case,and I am unsure if it would be used in this situation;  I know this isnt going to work but it would inan ideal world :slight_smile:

[lua] self.image = display.newSprite(“budSprites.sheets.invader”…row…“png”," budSprites.sequences.invader"…row…“png”)[/lua]

Obviously …row… would be singular somehow, I think

“scratches head”

In your budSprites.lua, try this :

[lua]

– this creates an array of the sheets

for i = 1, 5 do

  sheets.large[i] = graphics.newImageSheet(“images/sprite” … i … “.png”, sheetData)

end

– keep the sheetData & sequences.large the same as you have them, as you apparently are only changing the image, each image can use the

– same sheetData table and sequences.large table

– then as you want to swap the images

self.image = display.newSprite(sheets.large[1], budSprites.sequences.large)

–or

self.image = display.newSprite(sheets.large[4], budSprites.sequences.large)

[/lua]

However, you may consider combining all the sheets into one sheet.  Then create an array(table) of sequences, instead of an array(table) of sheets

[lua]

sequences.large = {

      { name=“normalRun”, start = 1,  count = 2, time = 2000, loopCount = 0},

      { name=“normalRun”, start = 3,  count = 2, time = 2000, loopCount = 0},  

      { name=“normalRun”, start = 5,  count = 2, time = 2000, loopCount = 0},  

      { name=“normalRun”, start = 7,  count = 2, time = 2000, loopCount = 0},  

      { name=“normalRun”, start = 9,  count = 2, time = 2000, loopCount = 0},  

   }

– the only thing changing on each of these sequences is the ‘start’ value. So as you can see, using an array(table) of sequences like this

– there is a lot of different ways you can manipulate these fields to use the image sheets in creative ways.

self.image = display.newSprite(sheets.large, budSprites.sequences.large[4])

[/lua]

Hope this helps.

*SOLVED*

After much reading and consulting :slight_smile:

I finally have achieved the un-expected, un-expected for me that is :slight_smile:

So, heres what got everything working.

The spritesheet lua was re written to take care of the 5 sprites, it basically ended up as a unique function module for sprites.

A loop was placed in the file which iiterated over the image sheets. 5 blocks in one function .

I was then able to use the parameter [row] in the initializer in enemy lua which was passed to init, main lua .

main.lua wasnt changed.