set alpha of whole table

Most likely the snippet you are referring to is from the transition.to documentation from here:
http://docs.coronalabs.com/api/library/transition/to.html

As cyberparkstudios mentioned above you should also look at the displaygroup documentation here:
http://docs.coronalabs.com/api/library/display/newGroup.html

Here is what I think you are after using transtion.to and a display group

[lua]
local function buildTile()
local object = display.newRect(0,0,64,64)
return object
end

– when working with images a display group functions like a table
– with properties to support images
local spawnGroup = display.newGroup()

local function createTile(x,y)
– The for loop here was really not doing anything because it started at 1 and ended at 1

– the new rectangle is returned by buidTile, so lets put it in a new variable spawnRect
local spawnRect = buildTile(x,y)

– now its properties can be set
spawnRect.x = x
spawnRect.y = y
– the rectangles alpha does not need to be set here

– add the new rectangle to the display group
spawnGroup:insert(spawnRect)

– set the display group’s alpha to 0 so it wont be seen.
spawnGroup.alpha = 0

– use transition.to to slowly change the display group’s alpha to 1, making it fade in
transition.to( spawnGroup, { time=1500, alpha=1.0} )

end

local function makeTilesTouch(event)
if event.phase == “ended” then
y = event.y
x = event.x
createTile(x,y)
end
end
Runtime:addEventListener(“touch”, makeTilesTouch)
[/lua] [import]uid: 173551 topic_id: 36554 reply_id: 144951[/import]

Thanks to everyone for their assistance! I ended up figuring out something on early Sunday morning:

[lua]

clearSettings = false

local function buildGrid()
local object = display.newRect(0,0,64,64)
return object
end

local spawnTable = {}
local tileGroup

local function createTile(x,y)
local function createGroup()
if clearSettings == false then
tileGroup = display.newGroup()
tileGroup.alpha = .2
end
end
createGroup()
clearSettings = true
for i = 1, 1 do
spawnTable[i] = buildGrid(x,y, alpChan)
spawnTable[i].x = x
spawnTable[i].y = y
spawnTable[i].alpha = 1
tileGroup:insert(spawnTable[i])
end
end

local function makeTilesTouch(event)
if event.phase == “ended” then
alpChan = .1
y = event.y
x = event.x
createTile(x,y)
end
end
Runtime:addEventListener(“touch”, makeTilesTouch)

local clearButton = display.newRect(display.contentWidth/4, display.contentHeight/1.2, 256,64)
local function makeAlpha(event)
if event.phase == “ended” then
clearSettings = false
tileGroup.alpha = 1
tileGroup = nil
return true
end
end

clearButton:addEventListener(“touch”, makeAlpha)

[/lua]

It has been working thus far for my purposes. Does anyone have any idea why this would be bad code, if indeed bad code it is? [import]uid: 135394 topic_id: 36554 reply_id: 144973[/import]

For someone starting out, it is not bad, but honestly it does need improvement. I say that as someone who also needs to improve. But that being said the best learning comes from correcting your mistakes, because you get a better understanding of how the pieces fit together. Just don’t give up on it because there is always more to learn.

I would expect that taking a proper beginning programing class, or a class in programing logic will help you (a school is best, but there are online sources as well). Also look at other peoples example code, and take it apart to understand it (I know you already do this).

Things that would help you with this project are:

  1. understanding what for loops are and how they are used.
  2. understanding Tables vs Display Groups
  3. although a somewhat advanced topic, learning about Performance and Optimization will help you build a good foundation from the beginning, and save you some headaches.
    look here for #3:
    http://developer.coronalabs.com/content/performance-and-optimization

I have rewritten your code to be a little more efficient, and to help you understand some better ways to do things. I wouldn’t necessarily say it is the best way, but I wanted to keep your code as intact as possible so you could see the differences. I endeavored to comment the changes to clarify what I did and why. I also added a new button (the Red one) to show you how to properly remove the display objects (as I understand it, I am also still learning)

Good luck!

[lua]-- I flipped the true/false values for clearSettings
– this makes more sense based on the variable name
clearSettings = true

local function buildGrid()
local object = display.newRect(0,0,64,64)
return object
end

– changed spawnTable into a display group so that you can put new tileGroups into it.
– each tileGroup becomes a new object distinct from any other, but needs to be stored
– into a new group to continue to exist after a call to create group
local spawnGroup = display.newGroup()
local tileGroup

local function createTile(x,y)

local function createGroup()
if clearSettings == true then
tileGroup = display.newGroup()
tileGroup.alpha = .2
spawnGroup:insert(tileGroup)
end
end
createGroup()
clearSettings = false

– This for loop is really not serving any purpose.
– i only ever reaches the value 1, so it keeps putting your new rectangle into spawnTable[1]
– for i = 1, 1 do
– spawnTable[i] = buildGrid(x,y, alpChan)
– spawnTable[i].x = x
– spawnTable[i].y = y
– spawnTable[i].alpha = 1
– tileGroup:insert(spawnTable[i])
– end

– This is equivalent to the above for loop:
– Your buildGrid function does not accept paramaters,
– so calling it with spawnTable[i] = buildGrid(x,y, alpChan) will not work.
– your code worked before because you set the x,y and alpha after calling buildGrid
local spawnRect = buildGrid()
spawnRect.x = x
spawnRect.y = y
spawnRect.alpha = 1
tileGroup:insert(spawnRect)

end

local function makeTilesTouch(event)
if event.phase == “ended” then
– This is not used by your code
–local alpChan = .1
local y = event.y
local x = event.x
createTile(x,y)
end
end
local clearButton = display.newRect(display.contentWidth/4, display.contentHeight/1.2, 256,64)
local removeButton = display.newRect(display.contentWidth/4, display.contentHeight/1.2 - 80, 256,64)
removeButton:setFillColor( 255, 0, 0)

local function makeAlpha(event)
if event.phase == “ended” then
clearSettings = true
– Set the last group added’s alpha to 1
spawnGroup[spawnGroup.numChildren].alpha = 1
–tileGroup.alpha = 1
–tileGroup = nil
return true
end
end

local function removeAll(event)
if event.phase == “ended” then
– this for loop runs backwards
– for example 10,9,8,7,6…
– when removing objects from a display group,
– you have to go backwards because the objects move forward
– after one is removed.

for i = spawnGroup.numChildren,1,-1 do
display.remove(spawnGroup[i])
spawnGroup[i] = nil
clearSettings = true
end
end
return true
end

Runtime:addEventListener(“touch”, makeTilesTouch)
clearButton:addEventListener(“touch”, makeAlpha)
removeButton:addEventListener(“touch”, removeAll)[/lua] [import]uid: 173551 topic_id: 36554 reply_id: 145034[/import]

@Pace, thanks for the help. My noob is showing…

I actually started this exercise to learn a bit more about tables, how they work with lua and what exactly I can do with them. In attempting to learn them, it would appear I got turned around and started to think to much about what the demo should do, rather than what it could do with tables.

Tables and loops are, by far, the things I have the most trouble with. I’ve worked on learning them better, but I think (similar to storyboard) my best bet for education and retention is to work on a project using both. That way I can really understand what is happening under the hood, so to speak.

Again, thanks for the help, and if you have any other tips/tricks/magic from which you think I will benefit, send’em on over! [import]uid: 135394 topic_id: 36554 reply_id: 145089[/import]

btw, to anyone trying to learn and understand tables, I’d suggest taking a look at Patsky’s Match 3 code in the Code Share library. It’s very elegant and incorporates a few other methods. [import]uid: 135394 topic_id: 36554 reply_id: 145107[/import]

I’d pass alpha as an argument to “buildTile” in line 10 and set it right after you create the tile (currently you’re passing x and y but not doing anything with them).

Brent [import]uid: 200026 topic_id: 36554 reply_id: 144865[/import]

Brent, thanks for the response! I actually use the x and y variables for the locations in my touch listener.

What I’m actually trying to do is create a rectangle, put it into a table, set the table’s alpha property, and then sometime later, increase the alpha property of that entire table to 1. Is that possible? I could have sworn I had a code snippet that did this but all my searching and googling is turning up a gooseegg. Let me know if I can provide any additional info. Thanks!
[import]uid: 135394 topic_id: 36554 reply_id: 144872[/import]

As you create each tile, insert into a displayGroup, then position the tile, then you can alpha the displayGroup.
[import]uid: 148857 topic_id: 36554 reply_id: 144887[/import]

Most likely the snippet you are referring to is from the transition.to documentation from here:
http://docs.coronalabs.com/api/library/transition/to.html

As cyberparkstudios mentioned above you should also look at the displaygroup documentation here:
http://docs.coronalabs.com/api/library/display/newGroup.html

Here is what I think you are after using transtion.to and a display group

[lua]
local function buildTile()
local object = display.newRect(0,0,64,64)
return object
end

– when working with images a display group functions like a table
– with properties to support images
local spawnGroup = display.newGroup()

local function createTile(x,y)
– The for loop here was really not doing anything because it started at 1 and ended at 1

– the new rectangle is returned by buidTile, so lets put it in a new variable spawnRect
local spawnRect = buildTile(x,y)

– now its properties can be set
spawnRect.x = x
spawnRect.y = y
– the rectangles alpha does not need to be set here

– add the new rectangle to the display group
spawnGroup:insert(spawnRect)

– set the display group’s alpha to 0 so it wont be seen.
spawnGroup.alpha = 0

– use transition.to to slowly change the display group’s alpha to 1, making it fade in
transition.to( spawnGroup, { time=1500, alpha=1.0} )

end

local function makeTilesTouch(event)
if event.phase == “ended” then
y = event.y
x = event.x
createTile(x,y)
end
end
Runtime:addEventListener(“touch”, makeTilesTouch)
[/lua] [import]uid: 173551 topic_id: 36554 reply_id: 144951[/import]

Thanks to everyone for their assistance! I ended up figuring out something on early Sunday morning:

[lua]

clearSettings = false

local function buildGrid()
local object = display.newRect(0,0,64,64)
return object
end

local spawnTable = {}
local tileGroup

local function createTile(x,y)
local function createGroup()
if clearSettings == false then
tileGroup = display.newGroup()
tileGroup.alpha = .2
end
end
createGroup()
clearSettings = true
for i = 1, 1 do
spawnTable[i] = buildGrid(x,y, alpChan)
spawnTable[i].x = x
spawnTable[i].y = y
spawnTable[i].alpha = 1
tileGroup:insert(spawnTable[i])
end
end

local function makeTilesTouch(event)
if event.phase == “ended” then
alpChan = .1
y = event.y
x = event.x
createTile(x,y)
end
end
Runtime:addEventListener(“touch”, makeTilesTouch)

local clearButton = display.newRect(display.contentWidth/4, display.contentHeight/1.2, 256,64)
local function makeAlpha(event)
if event.phase == “ended” then
clearSettings = false
tileGroup.alpha = 1
tileGroup = nil
return true
end
end

clearButton:addEventListener(“touch”, makeAlpha)

[/lua]

It has been working thus far for my purposes. Does anyone have any idea why this would be bad code, if indeed bad code it is? [import]uid: 135394 topic_id: 36554 reply_id: 144973[/import]

For someone starting out, it is not bad, but honestly it does need improvement. I say that as someone who also needs to improve. But that being said the best learning comes from correcting your mistakes, because you get a better understanding of how the pieces fit together. Just don’t give up on it because there is always more to learn.

I would expect that taking a proper beginning programing class, or a class in programing logic will help you (a school is best, but there are online sources as well). Also look at other peoples example code, and take it apart to understand it (I know you already do this).

Things that would help you with this project are:

  1. understanding what for loops are and how they are used.
  2. understanding Tables vs Display Groups
  3. although a somewhat advanced topic, learning about Performance and Optimization will help you build a good foundation from the beginning, and save you some headaches.
    look here for #3:
    http://developer.coronalabs.com/content/performance-and-optimization

I have rewritten your code to be a little more efficient, and to help you understand some better ways to do things. I wouldn’t necessarily say it is the best way, but I wanted to keep your code as intact as possible so you could see the differences. I endeavored to comment the changes to clarify what I did and why. I also added a new button (the Red one) to show you how to properly remove the display objects (as I understand it, I am also still learning)

Good luck!

[lua]-- I flipped the true/false values for clearSettings
– this makes more sense based on the variable name
clearSettings = true

local function buildGrid()
local object = display.newRect(0,0,64,64)
return object
end

– changed spawnTable into a display group so that you can put new tileGroups into it.
– each tileGroup becomes a new object distinct from any other, but needs to be stored
– into a new group to continue to exist after a call to create group
local spawnGroup = display.newGroup()
local tileGroup

local function createTile(x,y)

local function createGroup()
if clearSettings == true then
tileGroup = display.newGroup()
tileGroup.alpha = .2
spawnGroup:insert(tileGroup)
end
end
createGroup()
clearSettings = false

– This for loop is really not serving any purpose.
– i only ever reaches the value 1, so it keeps putting your new rectangle into spawnTable[1]
– for i = 1, 1 do
– spawnTable[i] = buildGrid(x,y, alpChan)
– spawnTable[i].x = x
– spawnTable[i].y = y
– spawnTable[i].alpha = 1
– tileGroup:insert(spawnTable[i])
– end

– This is equivalent to the above for loop:
– Your buildGrid function does not accept paramaters,
– so calling it with spawnTable[i] = buildGrid(x,y, alpChan) will not work.
– your code worked before because you set the x,y and alpha after calling buildGrid
local spawnRect = buildGrid()
spawnRect.x = x
spawnRect.y = y
spawnRect.alpha = 1
tileGroup:insert(spawnRect)

end

local function makeTilesTouch(event)
if event.phase == “ended” then
– This is not used by your code
–local alpChan = .1
local y = event.y
local x = event.x
createTile(x,y)
end
end
local clearButton = display.newRect(display.contentWidth/4, display.contentHeight/1.2, 256,64)
local removeButton = display.newRect(display.contentWidth/4, display.contentHeight/1.2 - 80, 256,64)
removeButton:setFillColor( 255, 0, 0)

local function makeAlpha(event)
if event.phase == “ended” then
clearSettings = true
– Set the last group added’s alpha to 1
spawnGroup[spawnGroup.numChildren].alpha = 1
–tileGroup.alpha = 1
–tileGroup = nil
return true
end
end

local function removeAll(event)
if event.phase == “ended” then
– this for loop runs backwards
– for example 10,9,8,7,6…
– when removing objects from a display group,
– you have to go backwards because the objects move forward
– after one is removed.

for i = spawnGroup.numChildren,1,-1 do
display.remove(spawnGroup[i])
spawnGroup[i] = nil
clearSettings = true
end
end
return true
end

Runtime:addEventListener(“touch”, makeTilesTouch)
clearButton:addEventListener(“touch”, makeAlpha)
removeButton:addEventListener(“touch”, removeAll)[/lua] [import]uid: 173551 topic_id: 36554 reply_id: 145034[/import]

@Pace, thanks for the help. My noob is showing…

I actually started this exercise to learn a bit more about tables, how they work with lua and what exactly I can do with them. In attempting to learn them, it would appear I got turned around and started to think to much about what the demo should do, rather than what it could do with tables.

Tables and loops are, by far, the things I have the most trouble with. I’ve worked on learning them better, but I think (similar to storyboard) my best bet for education and retention is to work on a project using both. That way I can really understand what is happening under the hood, so to speak.

Again, thanks for the help, and if you have any other tips/tricks/magic from which you think I will benefit, send’em on over! [import]uid: 135394 topic_id: 36554 reply_id: 145089[/import]

btw, to anyone trying to learn and understand tables, I’d suggest taking a look at Patsky’s Match 3 code in the Code Share library. It’s very elegant and incorporates a few other methods. [import]uid: 135394 topic_id: 36554 reply_id: 145107[/import]