Insert a 2D table group in a single group

groupA = {} groupB = display.newGroup() for i = 1, 5 do groupA[i] = {} for j = 1, 5 do groupA[i][j] = display.newGroup() local rect = display.newRect(0,0,100,100) groupA[i][j]:insert(rect) end end --Not sure about this groupB:insert(groupA) 

How can I Insert groupA to groupB?

thanks in advance

You can’t. groupA is a table. groupB is a display group. It does not make sense to insert a logical construct into a display object. You can attach groupA to groupB but that is the only useful purpose here: groupB.groupA = groupA

Okay, now that this is impossible. Gonna stick to my plan B, thanks though :slight_smile:

I’ve been trying to find your post that arrived in my inbox, but just realised that you must have deleted it.

I think you’re getting confused between table behaviour and group behaviour. I’ll try to explain…

You are possibly trying to put things into a display group using a name. That won’t work. While you can put something into a table or a display group by number or name, a display object is only rendered on screen as part of the display group if you use insert() or a number index, for example…

local myGroup = display.newGroup()

local circle = display.newCircle( 0, 0, 10 )

local rect = display.newRect( 0, 0, 10, 10 )

local img = display.newImage( “image.png” )

myGroup.aCircle = circle – does NOT work, but ‘.aCircle’ and [“aCircle”] will return the circle object, like a table

myGroup[“aCircle”] = circle – does NOT work, but ‘.aCircle’ and [“aCircle”] will return the circle object, like a table

myGroup[1] = rect – works, because you are putting the rect at a particular numerical index in the group

myGroup:insert( img ) – works, because you are putting the rect at a particular numerical index in the group

So, really, what you want to do is think about whether you want to keep a reference to your ‘cards’ or actually control their location in a ‘stack’.

Because the display group that your ‘cards’ are in will always have a reference to them (unless you remove them, of course) you don’t need to refer to them from a regular table.

About the delete, 

I began to see some light once I read what I had posted, and I didn’t want to bother any one with my errors.

Thank you for telling me you receive messages even if they have been deleted.

I will refrain from such things, unless I feel IT IS WORTHLESS to keep a post.

I am going over what you said above, and the “names” in Display Group not being allowed is making sense.

I just posted a new topic under forum discussion: “LUA TABLES”,

The SUBJECT of my post this morning is:

LUA TABLES OR DISPLAY GROUPS. A juggling act.

I have included all my “LOGIC” in that post.

It would rock if you could read that when you have time.

In the mean time, I am certain what you have just posted will shed some light.

By the end of the day, I will have the solution.

Thanks, Mate!

Chris:)

Horacebury,

I believe I am wanting to"control their location in a ‘stack’."

As you have mentioned above.

Initially, my logic was having a LUA table, called, cardsPlayedTable{ },

and have a CORONA display-group, called, cardsPlayedGroup( )

I would manipulate elements in cardsPlayedTable{ }, and have the cardsPlayedGroup( ) mirror that table.

THIS SEEMED REDUNDANT OF COURSE!

Avoiding ‘toFront’, I was going to remove a card from the cardsPlayedGroup( ) 

temporarily store it, then reinsert each card back into that very same cardsPlayedGroup, however,

in the order they are to be stacked.

This would work (in my mind) for shuffling the cards.

Putting them back into thedeckGroup( )

So, what little I do see, 

You say:

myGroup[1] = object – works, because you are putting the object at a particular “numerical index” in the group

myGroup:insert( img ) --works too

So, if I use the above, how do I know (after shuffling), what specific card is at myGroup[1] or myGroup[15].

I may not be able to see (at the moment) how I can simply be blind to the order of myGroup, and still have the control I need over

each card object.

My cards include more than typical properties:

suit

rank

color

Certain cards are “SPECIAL”, and I need to know when a special card has been sent to the hand, the played area, or even the discard area.

These cards have been coded with all personal properties in aCard.lua file which has Class.lua as a parent.

In my mind, that is the main reason I am trying to OOP my game, even if my game CAN be programmed, NON-OOP.

I hope I have given you enough to foresee any obstacles I have waiting for me.

And you have made clear certain crucial points.

In the mean time, it seems I can do what I initially set out to do.

deck[1] = card1 – works, because I am putting card1 at a particular numerical index in the group

deck:insert( card2 ) – works, because I am putting card2 at a particular numerical index in the group

shuffleDeck( )

deck.card2 could now be at location deck[1]

Is this correct?

And how could I know if it was the KING card, or the Queen card?

Am I close? I feel like it!

Chris:)

Horacebury, you said:

"…think about whether you want to keep a reference to your ‘cards’ 

or actually control their location in a ‘stack’."

Hey, you bloody Englishman! I am a cold-blooded American!

I Want both kinds of music, COUNTRY and WESTERN!

Can I do both!

Chris:)

I believe I am wanting to"control their location in a ‘stack’."

This, being a visual operation, needs to be done in a display group. To control the position you will want to use insert() because that avoids the group forgetting the object’s references. Eg:

-- create stack local stack = display.newGroup() -- add cards local a = display.newGroup() stack:insert( a ) local b = display.newGroup() stack:insert( b ) local c = display.newGroup() stack:insert( c ) -- get bottom card local bottom = stack[1] -- move bottom card to the middle table.insert( stack, 2, bottom )

"and have the cardsPlayedGroup( ) mirror that table"

Correct.

"So, if I use the above, how do I know (after shuffling), what specific card is at myGroup[1] or myGroup[15]."

If all you want to do is shuffle the cards in the display group (remember that you don’t need a separate table, because the display group does the same function for you) then I think your logic is a bit too complex. What I would do is this:

-- create stack local stack = display.newGroup() stack.x, stack.y = display.contentCenterX, display.contentCenterY -- add a load of cards for i=1, 52 do local card = display.newGroup() stack:insert( card ) local back = display.newRect( card, 0, 0, 200, 600 ) local text = display.newText{ parent=card, text=i, x=0, y=0, fontSize=48 } text.fill = {0,0,0} end -- shuffle cards for i=1, math.random( 50, 100 ) do local indexToShuffle = math.random( 1, stack.numChildren ) stack[indexToShuffle]:toFront() -- :toBack() works just fine end -- at this point the cards are shown on screen in shuffled order, so we reveal them for i=stack.numChildren, 1, -1 do transition.to( stack[i], { delay=i\*500, time=350, y=600 } ) end

"Certain cards are “SPECIAL”, and I need to know when a special card has been sent to the hand, the played area, or even the discard area."

So, have a display group for the ‘hand’, too. Also, the cards (like real cards) should all have the same structure - they should all be display groups - but you have values attached to give them meaning. If you need to find one, you use a function to search the stack:

-- create a card local function createCard( parent, name, class, x, y ) local group = display.newGroup() group.name, group.class = name, class group.x, group.y = x, y -- add to parent stack (or hand) parent:insert( group ) -- load things like it's image here, etc. return group end -- create stack local function createStack( parent, name, x, y ) local group = display.newGroup() group.name = name group.x, group.y = x, y -- add to parent group parent:insert( group ) -- provide search functions... function group:findCardBy( name ) for i=1, group.numChildren do if (group[i].name == name) then return group[i] end end return nil end function group:findCardBy( class ) for i=1, group.numChildren do if (group[i].class == class) then return group[i] end end return nil end return group end

Hope this helps.

Very Cool, Horacebury, reading your post now! 

I was not sure how I was going to start my day, but thanks to you I have some reading to do!

You must be a rocker of sorts; of the British invasion flavor; good-day it is, mate!

Chris:)

No worries :slight_smile:

Just discovered that there’s a limit on the number of quotes you can put in a thread post though.

M

Yea, well I did worse!

I found out, there is a limit to the “smiley faces” you can add!

:slight_smile:

Chris:) 

Horacebury,

I have implemented your code and all works wonderfully with my existing code!

The changes I had to make were surprisingly painless and quick!

I have a question: "How can I dispatch an event to each card through the SECOND for-loop below?

– shuffle cards

for i=1, math.random( 50, 100 ) do local indexToShuffle = math.random( 1, stack.numChildren )--number between 1 and 52 stack[indexToShuffle]:toFront() -- :toBack() works just fine end

– at this point the cards are shown on screen in shuffled order, so we reveal them

for i=stack.numChildren, 1, -1 do--from 52 down to 1

 Right here, how can I dispatch an event to each card through this loop? I have custom functions for each card in the aCard.lua file; each card in-turn has a parent, required() from: class.lua. All cards share common functions, and I have custom constructors that give certain cards special functions:

“EGGsample” :slight_smile:

aCard:doSomething() --COMMON TO ALL CARDS

Card1:doSomethingSpecial() --ONLY SPECIAL CARDS

MY SHOT IN THE DARK!

stack[i]:dispatchEvent( myEvent )--whyDoesThisNotWork?

 transition.to( stack[i], { delay=i\*500, time=350, y=600 } ) end

I even attached:

object:addEventListener( "myEvent", self ) 

…in the same location I attached:

object:addEventListener( "touch", self ) 

The latter works, the former does not.

Using the class.lua and attempting OOP, I am beginning to slightly blur the communication with the “self” in the aCard.lua module, when I am “OUT OF SCOPE”.

I know I am closer. Your assistance today took me ‘personally’ light years ahead in my understanding, hence, the new questions.

Chris:)

You can’t. groupA is a table. groupB is a display group. It does not make sense to insert a logical construct into a display object. You can attach groupA to groupB but that is the only useful purpose here: groupB.groupA = groupA

Okay, now that this is impossible. Gonna stick to my plan B, thanks though :slight_smile:

I’ve been trying to find your post that arrived in my inbox, but just realised that you must have deleted it.

I think you’re getting confused between table behaviour and group behaviour. I’ll try to explain…

You are possibly trying to put things into a display group using a name. That won’t work. While you can put something into a table or a display group by number or name, a display object is only rendered on screen as part of the display group if you use insert() or a number index, for example…

local myGroup = display.newGroup()

local circle = display.newCircle( 0, 0, 10 )

local rect = display.newRect( 0, 0, 10, 10 )

local img = display.newImage( “image.png” )

myGroup.aCircle = circle – does NOT work, but ‘.aCircle’ and [“aCircle”] will return the circle object, like a table

myGroup[“aCircle”] = circle – does NOT work, but ‘.aCircle’ and [“aCircle”] will return the circle object, like a table

myGroup[1] = rect – works, because you are putting the rect at a particular numerical index in the group

myGroup:insert( img ) – works, because you are putting the rect at a particular numerical index in the group

So, really, what you want to do is think about whether you want to keep a reference to your ‘cards’ or actually control their location in a ‘stack’.

Because the display group that your ‘cards’ are in will always have a reference to them (unless you remove them, of course) you don’t need to refer to them from a regular table.

About the delete, 

I began to see some light once I read what I had posted, and I didn’t want to bother any one with my errors.

Thank you for telling me you receive messages even if they have been deleted.

I will refrain from such things, unless I feel IT IS WORTHLESS to keep a post.

I am going over what you said above, and the “names” in Display Group not being allowed is making sense.

I just posted a new topic under forum discussion: “LUA TABLES”,

The SUBJECT of my post this morning is:

LUA TABLES OR DISPLAY GROUPS. A juggling act.

I have included all my “LOGIC” in that post.

It would rock if you could read that when you have time.

In the mean time, I am certain what you have just posted will shed some light.

By the end of the day, I will have the solution.

Thanks, Mate!

Chris:)

Horacebury,

I believe I am wanting to"control their location in a ‘stack’."

As you have mentioned above.

Initially, my logic was having a LUA table, called, cardsPlayedTable{ },

and have a CORONA display-group, called, cardsPlayedGroup( )

I would manipulate elements in cardsPlayedTable{ }, and have the cardsPlayedGroup( ) mirror that table.

THIS SEEMED REDUNDANT OF COURSE!

Avoiding ‘toFront’, I was going to remove a card from the cardsPlayedGroup( ) 

temporarily store it, then reinsert each card back into that very same cardsPlayedGroup, however,

in the order they are to be stacked.

This would work (in my mind) for shuffling the cards.

Putting them back into thedeckGroup( )

So, what little I do see, 

You say:

myGroup[1] = object – works, because you are putting the object at a particular “numerical index” in the group

myGroup:insert( img ) --works too

So, if I use the above, how do I know (after shuffling), what specific card is at myGroup[1] or myGroup[15].

I may not be able to see (at the moment) how I can simply be blind to the order of myGroup, and still have the control I need over

each card object.

My cards include more than typical properties:

suit

rank

color

Certain cards are “SPECIAL”, and I need to know when a special card has been sent to the hand, the played area, or even the discard area.

These cards have been coded with all personal properties in aCard.lua file which has Class.lua as a parent.

In my mind, that is the main reason I am trying to OOP my game, even if my game CAN be programmed, NON-OOP.

I hope I have given you enough to foresee any obstacles I have waiting for me.

And you have made clear certain crucial points.

In the mean time, it seems I can do what I initially set out to do.

deck[1] = card1 – works, because I am putting card1 at a particular numerical index in the group

deck:insert( card2 ) – works, because I am putting card2 at a particular numerical index in the group

shuffleDeck( )

deck.card2 could now be at location deck[1]

Is this correct?

And how could I know if it was the KING card, or the Queen card?

Am I close? I feel like it!

Chris:)

Horacebury, you said:

"…think about whether you want to keep a reference to your ‘cards’ 

or actually control their location in a ‘stack’."

Hey, you bloody Englishman! I am a cold-blooded American!

I Want both kinds of music, COUNTRY and WESTERN!

Can I do both!

Chris:)

I believe I am wanting to"control their location in a ‘stack’."

This, being a visual operation, needs to be done in a display group. To control the position you will want to use insert() because that avoids the group forgetting the object’s references. Eg:

-- create stack local stack = display.newGroup() -- add cards local a = display.newGroup() stack:insert( a ) local b = display.newGroup() stack:insert( b ) local c = display.newGroup() stack:insert( c ) -- get bottom card local bottom = stack[1] -- move bottom card to the middle table.insert( stack, 2, bottom )

"and have the cardsPlayedGroup( ) mirror that table"

Correct.

"So, if I use the above, how do I know (after shuffling), what specific card is at myGroup[1] or myGroup[15]."

If all you want to do is shuffle the cards in the display group (remember that you don’t need a separate table, because the display group does the same function for you) then I think your logic is a bit too complex. What I would do is this:

-- create stack local stack = display.newGroup() stack.x, stack.y = display.contentCenterX, display.contentCenterY -- add a load of cards for i=1, 52 do local card = display.newGroup() stack:insert( card ) local back = display.newRect( card, 0, 0, 200, 600 ) local text = display.newText{ parent=card, text=i, x=0, y=0, fontSize=48 } text.fill = {0,0,0} end -- shuffle cards for i=1, math.random( 50, 100 ) do local indexToShuffle = math.random( 1, stack.numChildren ) stack[indexToShuffle]:toFront() -- :toBack() works just fine end -- at this point the cards are shown on screen in shuffled order, so we reveal them for i=stack.numChildren, 1, -1 do transition.to( stack[i], { delay=i\*500, time=350, y=600 } ) end

"Certain cards are “SPECIAL”, and I need to know when a special card has been sent to the hand, the played area, or even the discard area."

So, have a display group for the ‘hand’, too. Also, the cards (like real cards) should all have the same structure - they should all be display groups - but you have values attached to give them meaning. If you need to find one, you use a function to search the stack:

-- create a card local function createCard( parent, name, class, x, y ) local group = display.newGroup() group.name, group.class = name, class group.x, group.y = x, y -- add to parent stack (or hand) parent:insert( group ) -- load things like it's image here, etc. return group end -- create stack local function createStack( parent, name, x, y ) local group = display.newGroup() group.name = name group.x, group.y = x, y -- add to parent group parent:insert( group ) -- provide search functions... function group:findCardBy( name ) for i=1, group.numChildren do if (group[i].name == name) then return group[i] end end return nil end function group:findCardBy( class ) for i=1, group.numChildren do if (group[i].class == class) then return group[i] end end return nil end return group end

Hope this helps.

Very Cool, Horacebury, reading your post now! 

I was not sure how I was going to start my day, but thanks to you I have some reading to do!

You must be a rocker of sorts; of the British invasion flavor; good-day it is, mate!

Chris:)