Passing a custom object in display.newImage([parent], )

Hi,

In the object i’m creating (which is in fact an extended displayGroup) I use the following code to intercept :insert calls.

parentGroup.\_cachedInsert = parentGroup.insert function parentGroup:insert(\_obj) contentGroup:insert(\_obj) end

This works great when I call myObject:insert(image) directly. But I would also like the convenience of passing my object into the [param] arg of the display.new… functions.

So this works now:

myObject:insert(image)

How would I get this to work?

display.newImage(myObject, "images.....")

If your intent is to insert the display object you’re creating into a parent group within the constructor, display.newImage already supports this as the first param (optional) which can be a group to insert the object into.

See here for more info:

If your intent is to insert the display object you’re creating into a parent group within the constructor, display.newImage already supports this as the first param (optional) which can be a group to insert the object into.

See here for more info: https://docs.coronalabs.com/api/library/display/newImage.html

If this isn’t what you are looking to do… Would you mind elaborating a bit more?

Hi Danny, thank you for your reply, but that was not exactly what I meant to do. Here is a better explanation with some more code:

I constructed a module which looks a bit like this (simplified) :

local myScrollViewSpawner = {} function myScrollView:new(\_options) local myScrollViewObject = display.newContainer(\_options.width, \_options.height) local contentGroup = display.newGroup() function myScrollViewObject:insert(\_obj) print("custom insert method") --Something is inserted in myScrollViewObject, but i'll insert it into our contentGroup instead contentGroup:insert(\_obj) end return myScrollViewObject end return myScrollViewSpawner

Sample usage is in the main.lua below:

local myScrollViewSpawner = require("myScrollViewSpawner") local myScrollView = myScrollViewSpawner:new({width = 100, height = 100}) -- INSERT METHOD 1: This works (prints: "custom insert method") local rectangle1 = display.newRect(0, 0, 10, 10) myScrollView:insert(rectangle1) -- INSERT METHOD 2: This doesn't work yet local rectangle2 = display.newRect(myScrollView, 0, 0, 10, 10)

What I would like to do is to get INSERT METHOD 2 to work, thus also use my custom insert method. But I don’t know how the [parent] argument is handled within displayObjects. I hope this clears things up

You’re not going to be able to do that.

While this:

local group = display.newGroup() local tmp = display.newCircle( 10, 10, 10 ) group:insert( tmp )

 produces the same end result as:

local group = display.newGroup() local tmp = display.newCircle( group, 10, 10, 10 )

The second example does not call the insert() function on group.  Corona inserts it directly as part of the circle building code.

However… I’ll give you a solution in my next post.

If you really want to force a display.new*() builder to do something special do this:

display.\_cachedNewCircle = display.newCircle display.newCicle = function( ... ) local tmp = display.\_cachedNewCircle( unpack( arg ) ) if( type( arg[1] ) == "table" ) then -- Force call group's insert function if a group was passed as first arg arg[1]:insert( tmp ) end return tmp end

Then write your ‘group insert override’ code:

local group = display.newGroup() group.\_cachedInsert = group.insert -- Not a perfect override as I don't handle extra args, just for this example group.insert = function( self, obj ) print("In special insert") self:\_cachedInsert( obj ) end

Now try this:

local tmp = display.newCircle( group, 10, 10, 10 )

An you’ll see this in the console:

In special insert.

Ah yes I was looking for this :slight_smile: Thank you

To add on to Ed’s example, I’d also ensure that arg[1].numChildren property isn’t nil, so that way you know it’s a display group object

If your intent is to insert the display object you’re creating into a parent group within the constructor, display.newImage already supports this as the first param (optional) which can be a group to insert the object into.

See here for more info:

If your intent is to insert the display object you’re creating into a parent group within the constructor, display.newImage already supports this as the first param (optional) which can be a group to insert the object into.

See here for more info: https://docs.coronalabs.com/api/library/display/newImage.html

If this isn’t what you are looking to do… Would you mind elaborating a bit more?

Hi Danny, thank you for your reply, but that was not exactly what I meant to do. Here is a better explanation with some more code:

I constructed a module which looks a bit like this (simplified) :

local myScrollViewSpawner = {} function myScrollView:new(\_options) local myScrollViewObject = display.newContainer(\_options.width, \_options.height) local contentGroup = display.newGroup() function myScrollViewObject:insert(\_obj) print("custom insert method") --Something is inserted in myScrollViewObject, but i'll insert it into our contentGroup instead contentGroup:insert(\_obj) end return myScrollViewObject end return myScrollViewSpawner

Sample usage is in the main.lua below:

local myScrollViewSpawner = require("myScrollViewSpawner") local myScrollView = myScrollViewSpawner:new({width = 100, height = 100}) -- INSERT METHOD 1: This works (prints: "custom insert method") local rectangle1 = display.newRect(0, 0, 10, 10) myScrollView:insert(rectangle1) -- INSERT METHOD 2: This doesn't work yet local rectangle2 = display.newRect(myScrollView, 0, 0, 10, 10)

What I would like to do is to get INSERT METHOD 2 to work, thus also use my custom insert method. But I don’t know how the [parent] argument is handled within displayObjects. I hope this clears things up

You’re not going to be able to do that.

While this:

local group = display.newGroup() local tmp = display.newCircle( 10, 10, 10 ) group:insert( tmp )

 produces the same end result as:

local group = display.newGroup() local tmp = display.newCircle( group, 10, 10, 10 )

The second example does not call the insert() function on group.  Corona inserts it directly as part of the circle building code.

However… I’ll give you a solution in my next post.

If you really want to force a display.new*() builder to do something special do this:

display.\_cachedNewCircle = display.newCircle display.newCicle = function( ... ) local tmp = display.\_cachedNewCircle( unpack( arg ) ) if( type( arg[1] ) == "table" ) then -- Force call group's insert function if a group was passed as first arg arg[1]:insert( tmp ) end return tmp end

Then write your ‘group insert override’ code:

local group = display.newGroup() group.\_cachedInsert = group.insert -- Not a perfect override as I don't handle extra args, just for this example group.insert = function( self, obj ) print("In special insert") self:\_cachedInsert( obj ) end

Now try this:

local tmp = display.newCircle( group, 10, 10, 10 )

An you’ll see this in the console:

In special insert.

Ah yes I was looking for this :slight_smile: Thank you

To add on to Ed’s example, I’d also ensure that arg[1].numChildren property isn’t nil, so that way you know it’s a display group object