Director Class and Grouping objects Problem!

Hi all! I’ve got a problem about Director class and grouping some objects on an external class. Here is my scripts of short example application.

main.lua : http://cl.ly/3i323A0S1J0m1a0G3G0Z
gameScene.lua : http://cl.ly/3g471J1H26363D2H2O3G
fakeScene.lua : http://cl.ly/2w1q083J431J3B2Z1U3F
theClass.lua : http://cl.ly/080L0B232W0t422r3w3L

Do not be lazy to glance, scripts are very short :slight_smile:

At the bottom of “theClass.lua” script, there is a line like that : “theGroup:insert(back)”

When I comment this line, there is no problem about application, but I need to group some objects in this theClass module. How can I fix this problem?

Thanks a lot! [import]uid: 111666 topic_id: 22780 reply_id: 322780[/import]

Because you are using director, try this. For the localGroup where you put “back” into, try this code.

[lua]module(…,package.seeall)

new = function ( params )
local localGroup

local theGroup = display.newGroup ( )
function theFunc(localGroupValue)
localGroup = localGroupValue

local back = display.newImage(“background.png”)
back:setReferencePoint(display.TopLeftReferencePoint)
back.x = 200
back.y = 200

theGroup:insert(back)
end

– initVars function for director
local initVars = function()
localGroup:insert(back)
end
–initializes variables
initVars()

end [import]uid: 122076 topic_id: 22780 reply_id: 90986[/import]

i see that you are trying to build something in your class module then take that function and call it from your scene module, the problem you are having is with inserting the objects in your class module if i am not mistaken.
you should be able to make a group at the start of you function then return the group at the end, then when you call the function you can also insert it into a group in your scene module. Also i did some modifications since the module class module is not called by director and is just an external module.

[lua]–theClass module
–Create a table to house any external module functions
–you want to be accessed from the outside
local classTable = { }

local theGroup = display.newGroup ( )

local function theFunc ()
local theGroup = display.newGroup ( )

local back = display.newImage(“background.png”)
back:setReferencePoint(display.TopLeftReferencePoint)
back.x = 200
back.y = 200
theGroup:insert(back)
return theGroup
end
classTable.theFunc = theFunc

–Return the module function table at the end of the module
return classTable[/lua]

then you just call the function from the module you want and insert it into the scene

[lua]–fakeScene.lua
–module call for director
module(…,package.seeall)

require(“cleangroup”)
local theClass = require(“theClass”)
local director = require(“director”)

–house all of your scene into the new function
function new()

local localGroup = display.newGroup ( )

local back = display.newImage( “background.png” )
back:setReferencePoint(display.TopLeftReferencePoint)
back.x = 100
back.y = 100
localGroup:insert (back)

local function clicked(event)
if(event.phase == “ended”) then
director:changeScene(“gameScene”)
end
end

back:addEventListener(“touch”, clicked)

local theFunc = theClass.theFunc
–since you returned the group from the class module you can --insert into the local group just like any other display object
localGroup: insert (theFunc)
theFunc()
end[/lua]

didnt test it but it should work, just let me know, i changed the way you had the module set up by using a table so you dont have to worry about the module (…, package.seeall) for your module, still have to use it for director. also You need to include your whole scene in the function new() area for director to run it when it changes scene [import]uid: 126161 topic_id: 22780 reply_id: 91012[/import]

Thanks so much for your replies and I tried both of them but neither works for me :confused:

I tested the scripts with a lot of combinations and i realized, when I try to insert an object to a group ( not localGroup for director ) in theClass.lua ( not in a scene module ) , the app crashes.

My aim is just grouping some objects in theClass module ( not in a scene module ) and control all objects with one group. I dont want to return them from theClass module. I just want to create and group some objets in theClass module, thats it.

Do you have any ideas?? [import]uid: 111666 topic_id: 22780 reply_id: 91026[/import]

Alright, well what im gonna do is take your files, test it out, and then try o figure it out for you :slight_smile: [import]uid: 122076 topic_id: 22780 reply_id: 91027[/import]

Thanks a lot, im waiting brandonbuffa2010 :slight_smile: [import]uid: 111666 topic_id: 22780 reply_id: 91028[/import]

Ok well the first thing I noticed when I loaded everything, Theres two images for background.png . Is this one of the bugs you were talking about too? [import]uid: 122076 topic_id: 22780 reply_id: 91029[/import]

In addition, I found a note on the Corona SDK API page. Here it is :

"Note: Inserting display objects into a group, removes the object from it current group (objects cannot be in multiple groups). "

I’m inserting an object to 2 different groups. (theGroup and localGroup)
Could it be the reason? [import]uid: 111666 topic_id: 22780 reply_id: 91030[/import]

brandonbuffa2010, its not a bug. I’m creating 2 same object at the fakeScene.

Just check with removing the “localGroup:insert( back )” line at the “theClass” module.
It will work fine.

By clicking the images, scenes are changing. [import]uid: 111666 topic_id: 22780 reply_id: 91031[/import]

Oh I see and yes, since you assigned back to different groups, that could also be the reason, try that [import]uid: 122076 topic_id: 22780 reply_id: 91032[/import]

But when I edit my theClass module like that :
[lua] back = display.newImage(“background.png”)
back:setReferencePoint(display.TopLeftReferencePoint)
back.x = 200 + (counter * 20)
back.y = 200

theGroup:insert(back)
localGroup:insert(theGroup)[/lua]

also i got the same error. :confused:

I’m not inserting one object to multiple groups now, i think it is not the reason. [import]uid: 111666 topic_id: 22780 reply_id: 91033[/import]

Im not sure. But one question, do you know if your allowed to put a group into another group such as localGroup? [import]uid: 122076 topic_id: 22780 reply_id: 91034[/import]

Yes , there shouldnt be any problem.

Any ideas? [import]uid: 111666 topic_id: 22780 reply_id: 91084[/import]

right now you are trying to pass the local group from your fakeScene.lua into theClass.lua module by putting it in the function params, that is your problem, if u want to add theClass group into your local group then you need to return the group in the function like i showed you, also if it did not work, then mess around with it, i can guarantee it will work, i dont have time to grab all 4 files and change them so i can run them. I do exactly what i showed all the time. [import]uid: 126161 topic_id: 22780 reply_id: 91089[/import]

3DreamsGaming thanks so much you are right!

I realized the problem before I read your post. But the error is this.

I’m defining the group (theGroup) at the fakeScene, then I’m passing it as theFunc parameter.
I’m using my theGroup then I’m returning theGroup value to the fakeScene.

It works fine know!

Thanks for everyone :slight_smile: Especially, 3DreamsGaming! [import]uid: 111666 topic_id: 22780 reply_id: 91092[/import]

Glad i could help! [import]uid: 126161 topic_id: 22780 reply_id: 91095[/import]