Spawning Display Groups

Hello,

I have been trying to spawn a display group in my game using a spawn function. Actually i need to spawn one display group at a time, my code is not working, Please help

bar1 = display.newGroup( ) colort1 = display.newRect(bar1,0,hi\*4+200,wi,20) colort1.x = colort1.width/2 - colort1.width colort1:setFillColor(200/255,55/255,55/255) physics.addBody(colort1,"static",{}) colort1.isSensor = true colort2 = display.newRect(bar1,0,hi\*4+200,wi,20) colort2.x = colort1.x + colort1.width colort2:setFillColor(55/255,171/255,200/255) physics.addBody(colort2,"static",{}) colort2.isSensor = true local spawn = function(type) local barr = display.newGroup(type) barr.x = display.contentWidth/2 barr.y = 20 return barr end spawn(bar1)

Thank You :slight_smile:

Kallol

You don’t spawn display groups in a sense. I always thought of display groups as rubber bands the hold objects together.
documentation(https://docs.coronalabs.com/daily/api/library/display/newGroup.html)
Note: I am on mobile and so thing may look pretty code wise but it should get the job done
If you want to spawn a group like you have above you would do this:

local barr

local spawn = function(myType, colorTable)

local bar1 = display.newGroup()
local currentX= 0
for i= 1,#colorTable do

local colort1 = display.newRect(currentX,0,50,50)
currentX= currentX+50
colort1:setFillColor(colorTable[i][1],colorTable[i][2],colorTable[i][3])
physics.addBody(colort1,“static”,{})
colort1.isSensor = true
colort1.myType = myType

end

bar1.x, bar1.y = display.contentCenterX, display.contentCenterY

return bar1

end
local myColorTable1 ={}
myColorTable[1]= {55/255,171/255,200/255}
myColorTable[2]= {200/255,55/255,55/255}
barr= spawn(“bar1”, myColorTable1)
– you can now control the group
barr.alpha =.5–just an example

type is a reserved word (referencing a lua function), so that may be part of your trouble.

I didn’t think you can pass anything into display.group()

@scottrules44, you’re correct. display.newGroup() will ignore anything passed to it.

@Kallol it would be really helpful to know how its not working. A description of what is happening and what you expect would be super useful. Are you getting errors? Remember we can’t see what you’re seeing.

Rob

@scotruless44 Thank you I will try that

@Rob Nothing is being displayed on the screen.

In my game i have different bar types and in each bar type the two or more different colors will have different collision events, Therefore to spawn two or more colors one bar i will have to spawn them as a group…How do I do this?

[quote name=“playpunkgames” post=“329770” timestamp=“1465501651”]@scotruless44 Thank you I will try that @Rob Nothing is being displayed on the screen. In my game i have different bar types and in each bar type the two or more different colors will have different collision events, Therefore to spawn two or more colors one bar i will have to spawn them as a group…How do I do this?[/quote] I just updated my code above, I removed attributes that were not specified because I don’t what those values. I made a few error my self

You never put anything into those display.newGroups().

Rob

@scottruless44 Please have a look at my previous comment and see if you can help me in this situation… :slight_smile:

Actually in my game the player have to go through a bar …if the player passes through the like color then he scores and if the player passes through unlike color he would die…Now i have 8 types of bar (about bars i mentioned in the previous reply) …aI need to spawn those randomly over time and each spawned bar will move on the y axis and leave the screen making the player go through it…

I can do the other stuffs but having problem with the spawning

Just moded my code again to be more customizable.

Hopefully you can figure out how to add collision event.

Edit: just moded again, forgot myType attribute

You don’t spawn display groups in a sense. I always thought of display groups as rubber bands the hold objects together.
documentation(https://docs.coronalabs.com/daily/api/library/display/newGroup.html)
Note: I am on mobile and so thing may look pretty code wise but it should get the job done
If you want to spawn a group like you have above you would do this:

local barr

local spawn = function(myType, colorTable)

local bar1 = display.newGroup()
local currentX= 0
for i= 1,#colorTable do

local colort1 = display.newRect(currentX,0,50,50)
currentX= currentX+50
colort1:setFillColor(colorTable[i][1],colorTable[i][2],colorTable[i][3])
physics.addBody(colort1,“static”,{})
colort1.isSensor = true
colort1.myType = myType

end

bar1.x, bar1.y = display.contentCenterX, display.contentCenterY

return bar1

end
local myColorTable1 ={}
myColorTable[1]= {55/255,171/255,200/255}
myColorTable[2]= {200/255,55/255,55/255}
barr= spawn(“bar1”, myColorTable1)
– you can now control the group
barr.alpha =.5–just an example

type is a reserved word (referencing a lua function), so that may be part of your trouble.

I didn’t think you can pass anything into display.group()

@scottrules44, you’re correct. display.newGroup() will ignore anything passed to it.

@Kallol it would be really helpful to know how its not working. A description of what is happening and what you expect would be super useful. Are you getting errors? Remember we can’t see what you’re seeing.

Rob

@scotruless44 Thank you I will try that

@Rob Nothing is being displayed on the screen.

In my game i have different bar types and in each bar type the two or more different colors will have different collision events, Therefore to spawn two or more colors one bar i will have to spawn them as a group…How do I do this?

[quote name=“playpunkgames” post=“329770” timestamp=“1465501651”]@scotruless44 Thank you I will try that @Rob Nothing is being displayed on the screen. In my game i have different bar types and in each bar type the two or more different colors will have different collision events, Therefore to spawn two or more colors one bar i will have to spawn them as a group…How do I do this?[/quote] I just updated my code above, I removed attributes that were not specified because I don’t what those values. I made a few error my self

You never put anything into those display.newGroups().

Rob

@scottruless44 Please have a look at my previous comment and see if you can help me in this situation… :slight_smile:

Actually in my game the player have to go through a bar …if the player passes through the like color then he scores and if the player passes through unlike color he would die…Now i have 8 types of bar (about bars i mentioned in the previous reply) …aI need to spawn those randomly over time and each spawned bar will move on the y axis and leave the screen making the player go through it…

I can do the other stuffs but having problem with the spawning

Just moded my code again to be more customizable.

Hopefully you can figure out how to add collision event.

Edit: just moded again, forgot myType attribute