I’m fairly new to tables, having trouble locating the shortcut for batch assignment of a custom property.
Table contains abbreviations for each US state …
local states = { ak, al, ar ... wy }
--Looking for a way to batch the following table-wide declaration/assignment
states.[every element].is\_State = true;
--Each state also has custom 'label' property declared outside { } like this
ak.label = akL;
…
wy.label = wyL;
Sample usage in a touch-check function …
local labelMatch; --holds var for current answer (i.e., akL)
for i = localGroup[i]numChildren, 1, -1 do
if localGroup[i].is\_State then --if touch is within a state
if localGroup[i].label == labelMatch then --if label matches
transition.to(localGroup[i], {time=1000, alpha=1}); -- fade in state
end)
end
end
Typing 50 ‘is_State’ declarations works, but seems insanely inefficient, given that all elements share the same property and value; and, there are other cases where identical set-wide property values apply.
Is it possible to batch assign? Does calling these, as above, require any special syntax?
If you know how to do this or where to find the doc, I’d greatly appreciate any help you can give. I’m having trouble translating table examples that I’ve found to this case. Thanks very much.
Minor hitch. The ‘for’ works in the sample fade-in function, but not in resetScene( ) in same lua file:
function resetScene(e)
if(e.phase == "ended") then
for i = localGroup.numChildren, 1, -1 do
if(localGroup[i].isReset) then -- isReset works, assigned individually
localGroup[i].alpha = 0;
end
if (localGroup[i].isState) then -- isState doesn't work, for loop assigned
localGroup[i].alpha = 0;
end
end
end
end
Works when isState assigned individually outside braces …
ak.isState = true;
… so, only difference: isState assignment method. No error in terminal. The states’ alphas just remain 1 … as if isState property doesn’t exist.
Does for-loop assignment affect property visibility? i.e., does isState live inside table braces when ‘for’ assigned vs. outside the braces when individually assigned externally. Does localGroup[i].isState search syntax need to change?
Minor hitch. The ‘for’ works in the sample fade-in function, but not in resetScene( ) in same lua file:
function resetScene(e)
if(e.phase == "ended") then
for i = localGroup.numChildren, 1, -1 do
if(localGroup[i].isReset) then -- isReset works, assigned individually
localGroup[i].alpha = 0;
end
if (localGroup[i].isState) then -- isState doesn't work, for loop assigned
localGroup[i].alpha = 0;
end
end
end
end
Works when isState assigned individually outside braces …
ak.isState = true;
… so, only difference: isState assignment method. No error in terminal. The states’ alphas just remain 1 … as if isState property doesn’t exist.
Does for-loop assignment affect property visibility? i.e., does isState live inside table braces when ‘for’ assigned vs. outside the braces when individually assigned externally. Does localGroup[i].isState search syntax need to change?
A variety of questions since I don’t see much code relevant to how the table works…
You need to use strings for abbreviations; I can only assume you did this but it wasn’t in the sample.
local states = { "ak", "al", "wy" } -- correct
local states = { ak, al, wy } -- incorrect, each entry will = nil
How is your table built?
[code]-- One level table
local states = { “ak”, “al”, “wy” }
– You can’t set states[i].isState in this table because states[i] is a string, not a table
– Two level table
local states = {}
states[1] = { abbrev=“ak”, isState=true } – here, each state[i] is a table so you can put stuff in it.
– Display object table
local states = {}
states[1] = display.newText(“Arizona”)
– Display objects are also tables so you can add data to them.
states[1].isState = true
[/code]
Looking at your last post, I’m guessing that you’ve made display objects for each state, and then used :insert() to add them into the displayGroup called “localGroup”?
local localGroup = display.newGroup()
localGroup:insert(ak) -- assuming ak is a display object
localGroup[1].alpha = 0 -- this should mean ak.alpha = 0 as well
You can actually crunch that code inside the loop since they do the same thing.
if localGroup[i].isReset or localGroup[i].isState then
localGroup[i].alpha = 0
end [import]uid: 41884 topic_id: 33547 reply_id: 137396[/import]
A variety of questions since I don’t see much code relevant to how the table works…
You need to use strings for abbreviations; I can only assume you did this but it wasn’t in the sample.
local states = { "ak", "al", "wy" } -- correct
local states = { ak, al, wy } -- incorrect, each entry will = nil
How is your table built?
[code]-- One level table
local states = { “ak”, “al”, “wy” }
– You can’t set states[i].isState in this table because states[i] is a string, not a table
– Two level table
local states = {}
states[1] = { abbrev=“ak”, isState=true } – here, each state[i] is a table so you can put stuff in it.
– Display object table
local states = {}
states[1] = display.newText(“Arizona”)
– Display objects are also tables so you can add data to them.
states[1].isState = true
[/code]
Looking at your last post, I’m guessing that you’ve made display objects for each state, and then used :insert() to add them into the displayGroup called “localGroup”?
local localGroup = display.newGroup()
localGroup:insert(ak) -- assuming ak is a display object
localGroup[1].alpha = 0 -- this should mean ak.alpha = 0 as well
You can actually crunch that code inside the loop since they do the same thing.
if localGroup[i].isReset or localGroup[i].isState then
localGroup[i].alpha = 0
end [import]uid: 41884 topic_id: 33547 reply_id: 137396[/import]