Can we extend the Corona API?

For example, I want to add new functions to every image I create.
At the moment I’m creating a new function to create images along the lines of …
[lua]function newImage(…)
img = display.newImageRect(…)
img.fadeOut = function(t) transition.to(t,alpha=0)
end

end[/lua]
that was a made up example… what I want to know, is can I directly extend display.newImageRect’s object template to do this? [import]uid: 34945 topic_id: 11059 reply_id: 311059[/import]

although i haven’t tried this… following should work.

[lua]cacheFunc = display.newImageRect

function display.newImageRect(…)
local img = cacheFunc(…)
img.fadeOut = function(t) transition.to(t,alpha=0)
retun img
end[/lua]

It’s almost similar to what you are already doing… just matter of using function name provided by api instead of your own. [import]uid: 48521 topic_id: 11059 reply_id: 40238[/import]

Maybe you should take a look at Jon Beebes site - he has really usefull stuff on that and recently discussed this topic:

http://jonbeebe.net/hi-jack-optimizations [import]uid: 13097 topic_id: 11059 reply_id: 40239[/import]

@innominata,

Lua is a very powerful language, surprisingly it looks like a meek scripting language. What you suggest can be easily done, all you need to do is create a custom factory event that creates the new object, so for example insteado of using

[lua]local myImage = display.newImage("" … )[/lua]

you can use

[lua]local function myNewImage(filename)
local img = display.newImage(filename,0,0)

local function someFunction()

end

img.fadeOut = someFunction
return img
end

local myCustomImage = myNewImage(“myimage.png”)
myCustomImage.fadeOut()[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 11059 reply_id: 40256[/import]

You are the winner chinmay.patil!
[lua]cacheFunc = display.newImageRect

function display.newImageRect(…)
local img = cacheFunc(…)
function img:fadeOut(t)
transition.to(self,{time=t,alpha=0})
end
return img
end[/lua]
works perfectly. [import]uid: 34945 topic_id: 11059 reply_id: 40335[/import]

@jayantv

Er… unless I’m missing something, thats pretty much a more verbose transcription of my above example? [import]uid: 34945 topic_id: 11059 reply_id: 40336[/import]

blackList:addname(“innominata”)

:wink: [import]uid: 3826 topic_id: 11059 reply_id: 40337[/import]

I still have no idea what you are talking about
[import]uid: 34945 topic_id: 11059 reply_id: 40338[/import]

I haven’t found an OOP way to extend Corona objects that I like much, works as expected, or can be further inherited.

Sadly the class() inheritance implementation hasn’t worked.

MyGroup = class( display.newGroup, function( self )  
end)  
  
function MyGroup:myCustomFunction()  
end  

And this attempt below will create an object that inherits Corona Group functions, such as :insert, and adds my custom functions, but it’s Corona properties such as .width, and .numChildren do not update to reflect its state as you might expect.

MyGroup = display.newGroup()  
MyGroup.\_\_index = MyGroup  
  
function display.createMyGroup()  
  
 local self = display.newGroup()  
 setmetatable( self, MyGroup )  
 return self  
end  
  
function MyGroup:myCustomFunction()  
end  

So I’ve been using the below, which works as expected, with the only hassle being linking MyGroup functions to the corona group manually

[code]
MyGroup = {}
MyGroup.__index = MyGroup

function display.newMyGroup()

local self = display.newGroup()
self.myCustomFunction = MyGroup.myCustomFunction
return self
end

function MyGroup:myCustomFunction()
end
[/code] [import]uid: 4596 topic_id: 11059 reply_id: 40427[/import]

Pants!

I’d been using the manual linking myself and hoped to move onto a cleaner system, but was struggling to get it working.

There is actually a clean way that works a bit better than manually linking, you can use pairs(baseclass) to map over all the fields you want to your custom display group.

So my hope had been to have a prototype based system where man.lua, handled all manly things. Civilian.lua then supplemented Man with civilised things and the display group did the keeping all the sprites in one place thing.

Here’s what I’ve got:

  
Civilian=Man:clone()  
  
function Civilian:new()  
 local inst = display.newGroup()  
 -- map all fields in Civilian and Man onto display group  
 for k,v in pairs(Civilian) do  
 inst[k]=v  
 end  
 return inst  
end  
  

I’ll probably move the creating of a new display group and mapping the fields over into it’s own function, in case I want to subclass civilian later.

The clone method I’m using is as follows:

function clone( base\_object, clone\_object )  
 if type( base\_object ) ~= "table" then  
 return clone\_object or base\_object   
 end  
 clone\_object = clone\_object or {}  
 clone\_object.\_\_index = base\_object  
 return setmetatable(clone\_object, clone\_object)  
end  

From http://lua-users.org/wiki/InheritanceTutorial

I had been hoping to be able to pass display a Group instance in as the clone_object param, but none of the functions were preserved for some reason. Partly because I think the Corona display objects are implemented in a slightly weird way. Try using pairs to print of a list of the group’s functions and fields. It doesn’t produce the results I expected!

[import]uid: 11757 topic_id: 11059 reply_id: 41454[/import]