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.
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.
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.
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
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 )
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.
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.
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
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 )