Creating multiple variables with the same image (using a table)

Hi there, 

Can someone please advise how I change the below code from having multiple local variables for the same image, to having only one local variable using a table? I have looked throughout the forums for a simple fix but seem to get lost in the different uses for tables.


local arrow1 = display.newImage(“Arrow.png”)                

local arrow2 = display.newImage(“Arrow.png”)               

local arrow3 = display.newImage(“Arrow.png”)                

                     

arrow1.x = display.contentWidth/2

arrow1.y = display.contentHeight/2-200

arrow2.x = display.contentWidth/2

arrow2.y = display.contentHeight/2

arrow3.x = display.contentWidth/2

arrow3.y = display.contentHeight/2+200


Any help would be greatly appreciated :smiley:

Cheers

Daine

Here’s a free video tutorial called Billions of Stars that shows how to do that:

http://masteringcoronasdk.com/free-corona-sdk-tutorial/billions-of-stars/

 Jay

Fantastic! Exactly what I was looking for

Thanks Jay :slight_smile:

Be sure and watch the 2nd video – it’s the one that shows the actual “store it in a table” part.

Glad it helps. :slight_smile:

 Jay

Hi there,

I went through the first video, however what it did not tell me is how to display arrow1, 2 and 3 in specific co-ords and so I can assign “tap” functions to each.

I presume the second video explains that, however I cannot get access to it :frowning: (seems to be a common issue reading the comments) :slight_smile:

All I need to know is how to create a table like so:


local table = {“Arrow1.png”, “Arrow2.png”, “Arrow3.png”}


Then display each item (possibly more than once on the screen, with each one having its own function)


display.newImage (table[1])    <----- I am guessing this is correct.


How do I get the above image to display multiple times on the same screen without having to use local variables each time?

I want Arrow1 to be displayed in 3 places, with each individual picture having its own unique Tap function. However I cannot use

local firstArrow1 = display.newImage (“Arrow1.png”)

local secondArrow1 = display.newImage (“Arrow1.png”)

local third… etc

as this will create too many local variables and the app will not run once over 200 :frowning:

If this does not make sense let me know and I will try to simplify the mumble jumble… 

Cheers

Daine

1400+ people have access to that second video so problems accessing it aren’t all that common. :stuck_out_tongue: When there are problems, it typically boils down to the person not clicking the link in the verification email they were sent. If you didn’t see it look in your spam or junk folder in case it got put there. Or tell me how you signed up and I can manually okay you.

You’re going to want to create an empty table and every time you make a new display object put it inside that table. Then you can reference 14 or 1400 objects with that: arrows[347].x = centerX

On a related note, are you sure you want a unique tap function for each one? One function can handle multiple objects and you end up with fewer lines of code. I would be surprised if your objects are all doing something completely unique (it could be, it’s just not typical).

 Jay

D, I don’t use Graphics 2, this is for Graphics 1, but it should give you an idea how to spawn the objects with each different color object doing it’s own function.

[lua]

local Table = {}
local ArrowGroup = display.newGroup()
local ButtonTarget

local function spawnArrows()
    
    local function onEventListener(event)
        if event.phase == “began” then
            ButtonTarget = event.target --The ButtonTarget will now take on all the attributes of the touched object
            ButtonTarget.xScale = 1.2
            ButtonTarget.yScale = 1.2
            
        elseif event.phase == “moved” then
            ButtonTarget.xScale = 1
            ButtonTarget.yScale = 1
            
        elseif event.phase == “ended” then
            ButtonTarget.xScale = 1
            ButtonTarget.yScale = 1
            
            if ButtonTarget.Function == 1 then
                Table[ButtonTarget.ID].isVisible = false  --make the object disappear or do something
                print("ButtonTarget.Function == ", ButtonTarget.Function)
                print("ButtonTarget.ID == ", ButtonTarget.ID)
                
            elseif ButtonTarget.Function == 2 then
                Table[ButtonTarget.ID].isVisible = false
                print("ButtonTarget.Function == ", ButtonTarget.Function)
                print("ButtonTarget.ID == ", ButtonTarget.ID)
                
            elseif ButtonTarget.Function == 3 then
                Table[ButtonTarget.ID].isVisible = false
                print("ButtonTarget.Function == ", ButtonTarget.Function)
                print("ButtonTarget.ID == ", ButtonTarget.ID)   
            end
            
        end
        return true
    end
    
    
    display.remove(ArrowGroup)
    ArrowGroup = nil
    ArrowGroup = display.newGroup()
    
    local Count = 0
    
    Table = {}
    
    for i = 1, 3 do  --spawn 3 different object sizes
        
        if i == 1 then
            
            for k = 1,  3 do  --spawn 3 of these objects at random locations
                
                Count = Count + 1
                
                Table[Count] = display.newRoundedRect( 0, 0, 20, 20, 5)
                Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
                Table[Count].x = math.random(1, 300)
                Table[Count].y = math.random(1, 460)
                Table[Count].strokeWidth = 3
                Table[Count]:setStrokeColor(79, 148, 205)
                Table[Count]:setFillColor(0, 255, 0)
                Table[Count].ID = Count  --This will ID the object so it can be tracked and do something, be removed, explode or move for instance
                Table[Count].Function = 1  – This will identify the objects function that will be read in the onEventListener function
                Table[Count]:addEventListener(“touch”, onEventListener)
                ArrowGroup:insert(Table[Count])
            end
            
        elseif i == 2 then
            for k = 1,  5 do  --spawn 5 of these objects
                
                Count = Count + 1
                
                Table[Count] = display.newRoundedRect( 0, 0, 30, 45, 5)
                Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
                Table[Count].x = math.random(1, 290)
                Table[Count].y = math.random(1, 450)
                Table[Count].strokeWidth = 3
                Table[Count]:setStrokeColor(79, 148, 205)
                Table[Count]:setFillColor(225, 0, 0)
                Table[Count].ID = Count
                Table[Count].Function = 2
                Table[Count]:addEventListener(“touch”, onEventListener)
                ArrowGroup:insert(Table[Count])
            end
            
        elseif i == 3 then
            for k = 1,  2 do  --spawn 2 of these items
                
                Count = Count + 1
                
                Table[Count] = display.newRoundedRect( 0, 0, 40, 40, 5)
                Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
                Table[Count].x = math.random(1, 280)
                Table[Count].y = math.random(1, 440)
                Table[Count].strokeWidth = 3
                Table[Count]:setStrokeColor(79, 148, 205)
                Table[Count]:setFillColor(0, 0, 255)
                Table[Count].ID = Count
                Table[Count].Function = 2
                Table[Count]:addEventListener(“touch”, onEventListener)
                ArrowGroup:insert(Table[Count])
            end  
        end
    end   
    return ArrowGroup
end

spawnArrows()  --call the function

[/lua]

Hope this helps,

Nail

Cheers, 

I didn’t get any emails so not sure what happened there, but it is working now :slight_smile:

Here’s a free video tutorial called Billions of Stars that shows how to do that:

http://masteringcoronasdk.com/free-corona-sdk-tutorial/billions-of-stars/

 Jay

Fantastic! Exactly what I was looking for

Thanks Jay :slight_smile:

Be sure and watch the 2nd video – it’s the one that shows the actual “store it in a table” part.

Glad it helps. :slight_smile:

 Jay

Hi there,

I went through the first video, however what it did not tell me is how to display arrow1, 2 and 3 in specific co-ords and so I can assign “tap” functions to each.

I presume the second video explains that, however I cannot get access to it :frowning: (seems to be a common issue reading the comments) :slight_smile:

All I need to know is how to create a table like so:


local table = {“Arrow1.png”, “Arrow2.png”, “Arrow3.png”}


Then display each item (possibly more than once on the screen, with each one having its own function)


display.newImage (table[1])    <----- I am guessing this is correct.


How do I get the above image to display multiple times on the same screen without having to use local variables each time?

I want Arrow1 to be displayed in 3 places, with each individual picture having its own unique Tap function. However I cannot use

local firstArrow1 = display.newImage (“Arrow1.png”)

local secondArrow1 = display.newImage (“Arrow1.png”)

local third… etc

as this will create too many local variables and the app will not run once over 200 :frowning:

If this does not make sense let me know and I will try to simplify the mumble jumble… 

Cheers

Daine

1400+ people have access to that second video so problems accessing it aren’t all that common. :stuck_out_tongue: When there are problems, it typically boils down to the person not clicking the link in the verification email they were sent. If you didn’t see it look in your spam or junk folder in case it got put there. Or tell me how you signed up and I can manually okay you.

You’re going to want to create an empty table and every time you make a new display object put it inside that table. Then you can reference 14 or 1400 objects with that: arrows[347].x = centerX

On a related note, are you sure you want a unique tap function for each one? One function can handle multiple objects and you end up with fewer lines of code. I would be surprised if your objects are all doing something completely unique (it could be, it’s just not typical).

 Jay

D, I don’t use Graphics 2, this is for Graphics 1, but it should give you an idea how to spawn the objects with each different color object doing it’s own function.

[lua]

local Table = {}
local ArrowGroup = display.newGroup()
local ButtonTarget

local function spawnArrows()
    
    local function onEventListener(event)
        if event.phase == “began” then
            ButtonTarget = event.target --The ButtonTarget will now take on all the attributes of the touched object
            ButtonTarget.xScale = 1.2
            ButtonTarget.yScale = 1.2
            
        elseif event.phase == “moved” then
            ButtonTarget.xScale = 1
            ButtonTarget.yScale = 1
            
        elseif event.phase == “ended” then
            ButtonTarget.xScale = 1
            ButtonTarget.yScale = 1
            
            if ButtonTarget.Function == 1 then
                Table[ButtonTarget.ID].isVisible = false  --make the object disappear or do something
                print("ButtonTarget.Function == ", ButtonTarget.Function)
                print("ButtonTarget.ID == ", ButtonTarget.ID)
                
            elseif ButtonTarget.Function == 2 then
                Table[ButtonTarget.ID].isVisible = false
                print("ButtonTarget.Function == ", ButtonTarget.Function)
                print("ButtonTarget.ID == ", ButtonTarget.ID)
                
            elseif ButtonTarget.Function == 3 then
                Table[ButtonTarget.ID].isVisible = false
                print("ButtonTarget.Function == ", ButtonTarget.Function)
                print("ButtonTarget.ID == ", ButtonTarget.ID)   
            end
            
        end
        return true
    end
    
    
    display.remove(ArrowGroup)
    ArrowGroup = nil
    ArrowGroup = display.newGroup()
    
    local Count = 0
    
    Table = {}
    
    for i = 1, 3 do  --spawn 3 different object sizes
        
        if i == 1 then
            
            for k = 1,  3 do  --spawn 3 of these objects at random locations
                
                Count = Count + 1
                
                Table[Count] = display.newRoundedRect( 0, 0, 20, 20, 5)
                Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
                Table[Count].x = math.random(1, 300)
                Table[Count].y = math.random(1, 460)
                Table[Count].strokeWidth = 3
                Table[Count]:setStrokeColor(79, 148, 205)
                Table[Count]:setFillColor(0, 255, 0)
                Table[Count].ID = Count  --This will ID the object so it can be tracked and do something, be removed, explode or move for instance
                Table[Count].Function = 1  – This will identify the objects function that will be read in the onEventListener function
                Table[Count]:addEventListener(“touch”, onEventListener)
                ArrowGroup:insert(Table[Count])
            end
            
        elseif i == 2 then
            for k = 1,  5 do  --spawn 5 of these objects
                
                Count = Count + 1
                
                Table[Count] = display.newRoundedRect( 0, 0, 30, 45, 5)
                Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
                Table[Count].x = math.random(1, 290)
                Table[Count].y = math.random(1, 450)
                Table[Count].strokeWidth = 3
                Table[Count]:setStrokeColor(79, 148, 205)
                Table[Count]:setFillColor(225, 0, 0)
                Table[Count].ID = Count
                Table[Count].Function = 2
                Table[Count]:addEventListener(“touch”, onEventListener)
                ArrowGroup:insert(Table[Count])
            end
            
        elseif i == 3 then
            for k = 1,  2 do  --spawn 2 of these items
                
                Count = Count + 1
                
                Table[Count] = display.newRoundedRect( 0, 0, 40, 40, 5)
                Table[Count]:setReferencePoint(display.TopLeftReferencePoint)
                Table[Count].x = math.random(1, 280)
                Table[Count].y = math.random(1, 440)
                Table[Count].strokeWidth = 3
                Table[Count]:setStrokeColor(79, 148, 205)
                Table[Count]:setFillColor(0, 0, 255)
                Table[Count].ID = Count
                Table[Count].Function = 2
                Table[Count]:addEventListener(“touch”, onEventListener)
                ArrowGroup:insert(Table[Count])
            end  
        end
    end   
    return ArrowGroup
end

spawnArrows()  --call the function

[/lua]

Hope this helps,

Nail

Cheers, 

I didn’t get any emails so not sure what happened there, but it is working now :slight_smile: