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:
- understanding what for loops are and how they are used.
- understanding Tables vs Display Groups
- 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]