inserting new display object from module code into a group

I’m trying to add a new display object where the code is in a module.

But when I use this code:

local kernels = popcorn.new(100);
myGroup:insert( kernels )

I get the error:

bad argument #-2 to ‘insert’ (Proxy expected, got nil)

I know I must be missing something and I’ve searched the forums but can’t find what I need. Can someone provide any insight?

Thanks [import]uid: 94768 topic_id: 22665 reply_id: 322665[/import]

There isn’t enough info in your post to tell exactly what you are trying to accomplish, but this is a generic approach I would use to make a constructor function in a separate module:

in a file ‘popcorn.lua’ write something like this (pseudo code-not tested):

local mod = {};  
  
function mod.new(num)  
 --this is the function that creates a new popcorn 'class'  
 local popcornGroup = display.newGroup();  
  
 local txt = display.newText(num,0,0,native.systemFont,12);  
  
 popcornGroup:insert(txt);  
end  
  
return mod;  

in ‘Main.lua’, or any other file in your project:

[code]
local pop = require “popcorn”;

local popcorn1 = pop.new(100);
local popcorn2 = pop.new(450);

popcorn1.x , popcorn1.y = display.contentWidth*.5 , display.contentWidth*.5;
popcorn2.x , popcorn2.y = display.contentWidth*.5 , display.contentWidth*.5 + 50;
[/code] [import]uid: 33608 topic_id: 22665 reply_id: 90399[/import]

Thanks for the the reply amirfl7,

OK. Let’s see if this makes it any clearer.
I’ve got a function that spawns popcorn in a module: Puts them in a group and then returns the group.

Here’s my popcorn.lua:

function new(params)

local kernelGroup = display.newGroup()
if (params) then
for i = 1, params, 1 do
local kernel = display.newImageRect(“images/kernel.png”, 34, 34);
kernel:setReferencePoint(display.CenterReferencePoint);
kernel.x = mRand(50, _W-50); kernel.y = mRand(60, 90);
physics.addBody(kernel, {density=0.8, friction=0.3, radius=15});
–print (i);
kernelGroup:insert(kernel)
end
end
return popcorn
end

Once the group is returned I’m inserting it into a screenGroup since I’m using Storyboard so I won’t be able to use insert into a group in popcorn.lua but have to do it from main.lua or a scene.lua. [import]uid: 94768 topic_id: 22665 reply_id: 90402[/import]

Im sorry, i just reviewd the example i sent u and noticed that I forgot a line to return popcornGroup at the end of the ‘new’ function. In your code, your ‘new’ function should return kernelGroup. [import]uid: 33608 topic_id: 22665 reply_id: 90427[/import]

Thanks a lot Amir,

that fixed it. I knew I was missing something simple. I just needed another pair of eyes to look at it.

Joe [import]uid: 94768 topic_id: 22665 reply_id: 90431[/import]