This is my port of the core parts of Robert Penner's as3-signals to Lua for Corona:
Label: corona android iphone ipad building error inmobi problem display screen View image tableView failed object physics touch button game lua config event listener |
Like as3-signals? Try signal.lua
Nice!
Here's another implementation using the SimpleLuaClasses class.lua method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | require "class" --local mySignal = Signal() --mySignal:add( myListener ) --mySignal:dispatch( 'something' ) Signal = class(function( self ) self.callbacks = {} end) function Signal:dispatch( ... ) if #self.callbacks > 0 then for i=1, #self.callbacks do if self.callbacks[i] then self.callbacks[i]( unpack( arg )) end end end end function Signal:add( callback ) table.insert( self.callbacks, callback ) end function Signal:remove( callback ) local i = #self.callbacks while i > 0 do if self.callbacks[i] == callback then table.remove( self.callbacks, i ) end i = i-1 end end function Signal:removeAt( i ) if i <= #self.callbacks then table.remove( self.callbacks, i ) end end function Signal:removeAll() self.callbacks = {} end function Signal:dispose() self.callbacks = nil end |
And an implementation for native signals.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | require "class" --local mySignal = SignalNative( someDisplayObject, 'touch' ) --mySignal:add( myListener ) SignalNative = class(function( self, target, type, ... ) self.target = target self.type = type self.arg = arg self.stopPropagation = false self.focus = true self[ self.type ] = SignalNative.Handler self.callbacks = {} self.active = false end) function SignalNative.Handler( self, event ) if self.callbacks then if #self.callbacks > 0 then self:dispatch( event, unpack( self.arg ) ) end if self.type == 'touch' then if self.focus and self.target ~= Runtime then if 'began' == event.phase then display.getCurrentStage():setFocus( self.target ) elseif 'ended' == event.phase then display.getCurrentStage():setFocus( nil ) end end end end return self.stopPropagation end function SignalNative:dispatch( ... ) local callback if #self.callbacks > 0 then for i=1, #self.callbacks do callback = self.callbacks[i] if callback then callback( unpack( arg ) ) end end end end function SignalNative:add( callback ) table.insert( self.callbacks, callback ) self:_addEventListener() end function SignalNative:remove( callback ) local i = #self.callbacks while i > 0 do if self.callbacks[i] == callback then table.remove( self.callbacks, i ) end i = i-1 end if #self.callbacks == 0 then self:_removeEventListener() end end function SignalNative:removeAt( i ) if i <= #self.callbacks then table.remove( self.callbacks, i ) end if #self.callbacks == 0 then self:_removeEventListener() end end function SignalNative:removeAll() self:_removeEventListener() self.callbacks = {} end function SignalNative:dispose() self[ self.type ] = nil self:removeAll() self.callbacks = nil self.target = nil self.type = nil self.arg = nil end function SignalNative:_removeEventListener() if self.active then self.active = false self.target:removeEventListener( self.type, self ) end end function SignalNative:_addEventListener() if self.active == false then self.active = true self.target:addEventListener( self.type, self ) end end |
What does this do exactly? I have not AS3 experience so some descriptive info will help a lot for pure LUA users like me.
A different approach to events.
It's an alternative to events. Basically, instead of subscribing to an event by passing a string to addEventListener, you subscribe to a signal, which is a first-class member of the object. You can also pass multiple arguments, instead of a single event object.
Instead of this:
1 2 3 4 5 | local function eventHandler(event) print(event.arg1, event.arg2) end object:addEventListener("myEvent", eventHandler) |
views:2025 update:2011/10/10 15:46:04
热门文章
GameCenter LeaderBoards and Achievements Enabled Through OpenFeint
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
corona forums © 2003-2011