Hmm… I have been doing a lot of this in corona(dynamic “naming” of object… really just putting them in a table and referancing them by thier table index.)
I thought I would throw together a working sample… it is a little ugly (ok maybe more than a little) but I thought it might inspire someone else…
The following is a “Color Picker”. The number of colors displayed is based on the screen resoltion.(varible) and they are changed by referancing the rectangle(color) by it’s table index. (in this case it just cycles thru all off the objects but it is possible to change just one if you keep track of the indexes… like in anouther Lua table where the index is something usefull and the value is it’s position in the display group.)
[code]
Ww,Hh = display.viewableContentWidth, display.viewableContentHeight
BackgroundColor = display.newRect( 0,0,Ww,Hh);BackgroundColor:setFillColor(0,0,255)
function cButton(lx,ly)
local c=1
while customButton[c] do
local tT=customButton[c]
if tT[“active”]== “true” and lx > tT[“x1”] and lx < tT[“x2”] and ly > tT[“y1”] and ly < tT[“y2”] then
if tT[“command”] then
return tT[“command”]
else
return “color”,tT[“r”],tT[“g”],tT[“b”]
end
end
c=c+1
end
return nil
end
local testHandle = function (event)
– print("—began-----"…event.x…" “…event.y)
local lCMD,r,g,b = cButton(event.x,event.y)
if event.phase == “began” and lCMD then
print(”—began-----"…event.phase…"--------"…lCMD)
CMD = lCMD
elseif event.phase == “moved” and CMD then
print("—moved-----"…event.phase…"--------"…CMD)
if lCMD ~= CMD then
print("—cancel----"…event.phase…"--------"…CMD)
CMD = nil
end
elseif event.phase == “ended” and CMD then
print("—ended-----"…event.phase…"--------"…CMD)
if lCMD == “color” then
PickerGroup.alpha=0
PickerButtonGroup.alpha=1
local i = 1
–disable picker buttons
while i customButton[i][“active”]=“false”
i = i+1
end
customButton[PickerKey][“active”]=“true”
BackgroundColor:setFillColor(r,g,b)
elseif lCMD == “pick” then
displayPicker()
elseif lCMD == “more” then
displayPicker()
elseif lCMD == “cancel” then
PickerGroup.alpha=0
PickerButtonGroup.alpha=1
local i = 1
--disable picker buttons
while i customButton[i][“active”]=“false”
i = i+1
end
customButton[PickerKey][“active”]=“true”
end
CMD = nil
end
end
function createPicker()
local cgrid =math.floor(Ww/7)
local maxWIDE = 7
local maxHIGH = math.floor((Hh - 100)/cgrid )
local colorKey = 1
customButton = {}
local gW = 3–math.ceil(cgrid/2)–10 --width of lines
PickerGroup = display.newGroup()
local mH,mV = 1,1
while mH <= maxWIDE do
local bH=(mH-1)*cgrid
while mV <= maxHIGH do
local bV=(mV-1)cgrid
local rR,gG,bB = math.random (1,255),math.random (1,255),math.random (1,255)
cColor = display.newRect( PickerGroup,bH,bV,cgrid,cgrid);cColor:setFillColor(rR,gG,bB)
customButton[colorKey] = {}
customButton[colorKey][“x1”] = bH
customButton[colorKey][“x2”] = bH+cgrid
customButton[colorKey][“y1”] = bV
customButton[colorKey][“y2”] = (bV+cgrid)
customButton[colorKey][“r”]= rR
customButton[colorKey][“g”]= gG
customButton[colorKey][“b”]= bB
customButton[colorKey][“active”]=“false”
colorKey = colorKey + 1
mV = mV+1
end–vert while
mH = mH+1
mV = 1
end–horz while
– adding command buttons
–cancel
PickerGroup.alpha=0
–Picker button
PickerButtonGroup = display.newGroup()
local Choose = display.newRect(PickerButtonGroup, Ww/4,Hh/4,Ww/2,Hh/2);Choose:setFillColor(255,255,255)
customButton[colorKey] = {}
customButton[colorKey][“x1”] = Ww/4
customButton[colorKey][“x2”] = Ww-(Ww/4)
customButton[colorKey][“y1”] = (Hh/4)
customButton[colorKey][“y2”] = Hh-(Hh/4)
customButton[colorKey][“command”]= "pick"
customButton[colorKey][“active”]="true"
PickerKey = colorKey
colorKey = colorKey + 1
local title = display.newText(PickerButtonGroup,“Pick Color”,Ww/3,Hh/3, nil, 20 );title:setTextColor(0,0,0)
local Cancel = display.newRect( PickerGroup,0,Hh-100,Ww/2-2,100);Cancel:setFillColor(255,255,255)
customButton[colorKey] = {}
customButton[colorKey][“x1”] = 0
customButton[colorKey][“x2”] = Ww/2
customButton[colorKey][“y1”] = Hh-100
customButton[colorKey][“y2”] = Hh
customButton[colorKey][“command”]= "cancel"
customButton[colorKey][“active”]="false"
colorKey = colorKey + 1
local More = display.newRect( PickerGroup,Ww/2+2,Hh-100,Ww/2-2,100);More:setFillColor(255,255,255)
customButton[colorKey] = {}
customButton[colorKey][“x1”] = Ww/2
customButton[colorKey][“x2”] = Ww
customButton[colorKey][“y1”] = Hh-100
customButton[colorKey][“y2”] = Hh
customButton[colorKey][“command”]= "more"
customButton[colorKey][“active”]="false"
colorKey = colorKey + 1
local title = display.newText(PickerGroup, “Cancel”,Ww/8,Hh-50, nil, 20 );title:setTextColor(0,0,0)
local title = display.newText(PickerGroup, “More”,Ww/86,Hh-50, nil, 20 );title:setTextColor(0,0,0)
end–func
function displayPicker()
PickerGroup.alpha=1
PickerButtonGroup.alpha=0
local i = 1
--enable picker buttons
while i local rR,gG,bB = math.random (1,255),math.random (1,255),math.random (1,255)
PickerGroup[i]:setFillColor(rR,gG,bB)
customButton[i][“active”]=“true”
customButton[i][“r”]=rR
customButton[i][“g”]=gG
customButton[i][“b”]=bB
i = i+1
end
i = i+1
customButton[i][“active”]=“true”
i = i+1
customButton[i][“active”]=“true”
PickerButtonGroup.alpha=0
customButton[PickerKey][“active”]="false"
end
createPicker()
Runtime:addEventListener(“touch”,testHandle)
[/code] [import]uid: 40100 topic_id: 15029 reply_id: 55947[/import]