Using objects in a table multiple times

Hi Everyone,

I’m trying a little experiment while I take some time off from my main project, but I’ve become a little stuck. I hoping somebody can help.

My idea is a simple find the object game. I want an image in the HUD to show which object to find, and a duplicate one in the play space that the person taps. Here’s what I have so far.

os.execute('cls')  
  
require "sprite"  
  
local itemTable = {}  
  
local numOfItems = 1  
local k  
  
local function spawn()   
 local ItemSheet = sprite.newSpriteSheet("BonusItems.png", 80, 80)   
 local ItemSpriteSet = sprite.newSpriteSet(ItemSheet, 1, 8)   
 local ItemInstance = sprite.newSprite(ItemSpriteSet)  
 ItemInstance.currentFrame = math.random(2)  
  
 return ItemInstance  
end  
  
for k = 1, numOfItems do  
 itemTable[k] = spawn()  
 itemTable[k].x= math.random(50, display.contentWidth-50) itemTable[k].y = math.random(180,display.contentHeight-50)  
end  
  
local function hudItem()  
 local hudIcon = itemTable[1]  
 hudIcon.x = display.contentWidth\*0.5 hudIcon.y = 80  
end  
  
hudItem()  

As you can see, what I planned is to have a sprite sheet with various objects on. I set a random frame, spawn x amount, and put them in a table.

I’ve got the object spawning randomly on the screen no problem. Getting a duplicate is proving more difficult. All that happens is I get the image appearing once, at the co-ordinates in “hudItem()”. Can I call from a table multiple times, and have differing properties for each one? So for example, I want to spawn say, an apple, randomly on the screen, that can be tapped, but have a duplicate one at a very specific location just to view (along the top of the screen for instance).

Alternatively, if there’s a much better way to do this, I’d love to hear it.

Many thanks [import]uid: 88980 topic_id: 22862 reply_id: 322862[/import]