Animation clip doesnt work with object event

Hi All! I’m trying to apply OOP to Corona. Everything is fine until it came to Movie clip.

Rectangle class:
[lua]module (…,package.seeall)

function new ()
imageSeq = {}
local movieclip = require (“movieclip”)
for i=1,13 do
table.insert (imageSeq,“images/fly40x40_”…i…".png")
end

local obj = movieclip.newAnim (imageSeq)

function obj:enterFrame (event)
self.x = self.x + 1
end

function obj:start ()
Runtime:addEventListener (“enterFrame”,self)
self:play {startFrame=2,endFrame=13,loop=0}
end
return obj
end[/lua]

[lua]Rectangle = require (“rectangle”)
— START ----
rect = Rectangle.new ()
rect.x = 100; rect.y = 100
rect:rotate (90)
rect:start ()[/lua]

I suppose the object shifted to the right as well as display some animation. However it only move without any animation at all (instead it display one still image somewhere in the middle of the sequences)
[import]uid: 40631 topic_id: 7807 reply_id: 307807[/import]

for a start i would move the require(“movieclip”) line above the function new(). no need to keep requiring it every time you create a new object

as for your problem, it appears your enterFrame function is overriding the enterFrame function for the movieclip hence it moves x=x+1 but doesnt animate (since you killed the play function.

just working out a fix… [import]uid: 6645 topic_id: 7807 reply_id: 27733[/import]

try this

in movieclip.lua [lua]function g:enterFrame( event )
self:repeatFunction( event )

– add a new event
local event = { name=“mcEnterFrame”, target=self}
self:dispatchEvent(event)
end[/lua]

then in your main file

[lua]module (…,package.seeall)
local movieclip = require (“movieclip”)
function new ()
imageSeq = {}
for i=1,6 do
table.insert (imageSeq,“cube”…i…".png")
end

local obj = movieclip.newAnim (imageSeq)

function obj:mcEnterFrame(event)
self.x=self.x+1
end

function obj:start ()

self:play{startFrame=1,endFrame=6,loop=0}

– listen for new event
self:addEventListener(“mcEnterFrame”,self)
end

return obj

end[/lua]

it works but i’m still trying to work out how to avoid having to dispatch a new event

but i’ve checked it with 2 rectangle instances for example and it works fine [import]uid: 6645 topic_id: 7807 reply_id: 27738[/import]

It works like a charm now. Thank you for both the overriding tip and the multiple-instance test :slight_smile: [import]uid: 40631 topic_id: 7807 reply_id: 27758[/import]

ah, glad to hear that. had me stumped for a while. i tried storing the old enterframe, redefining it and then calling the old one in the new one but that just broke it. I am still wondering if there is a neater method and will look into it.

someone wrote this but i don’t know if it is relevant
http://developer.anscamobile.com/forum/2011/03/04/global-local-event-forwarding-table-listener

anyway i don’t think my solution for now has much affect on performance, so it should be fine, i’m just interested in other solutions to see what’s possible
[import]uid: 6645 topic_id: 7807 reply_id: 27774[/import]