Hi guys
I'm just interested to see whether someone can tell me which is the better / more efficient way to write this up - A or B.
Could you please let me know why?
Thank you heaps in advance!
A.
StagePower01 = gideros.class(Sprite)
function StagePower01:init()
setupStage(self)
setupLightbulb(self, pack1, pack2, pack3, pack4)
local lightSwitch = LightSwitch.new(pack1, -28, 0, items)
local powerSwitch = PowerSwitch.new(pack2, 28, 0, items)
local switchTouch
local function lightCheck()
if lightSwitch.on and powerSwitch.on then
lightSwitch:removeEventListener(Event.MOUSE_DOWN, switchTouch, lightSwitch)
powerSwitch:removeEventListener(Event.MOUSE_DOWN, switchTouch, powerSwitch)
lightUp(self, "stagePower02")
end
end
function switchTouch(target, event)
if target:hitTestPoint(event.x, event.y) then
sfxChanel = click:play()
if target.on then
target.on = false
target.mc:gotoAndStop(1)
else
target.on = true
target.mc:gotoAndStop(2)
end
lightCheck()
end
end
lightSwitch:addEventListener(Event.MOUSE_DOWN, switchTouch, lightSwitch)
powerSwitch:addEventListener(Event.MOUSE_DOWN, switchTouch, powerSwitch)
end |
B.
StagePower01 = gideros.class(Sprite)
function StagePower01:init()
setupStage(self)
setupLightbulb(self, pack1, pack2, pack3, pack4)
local lightSwitch = LightSwitch.new(pack1, -28, 0, items)
local powerSwitch = PowerSwitch.new(pack2, 28, 0, items)
function switchTouch(target, event)
if target:hitTestPoint(event.x, event.y) then
sfxChanel = click:play()
if target.on then
target.on = false
target.mc:gotoAndStop(1)
else
target.on = true
target.mc:gotoAndStop(2)
end
local function lightCheck()
if lightSwitch.on and powerSwitch.on then
lightSwitch:removeEventListener(Event.MOUSE_DOWN, switchTouch, lightSwitch)
powerSwitch:removeEventListener(Event.MOUSE_DOWN, switchTouch, powerSwitch)
lightUp(self, "stagePower02")
end
end
lightCheck()
end
end
lightSwitch:addEventListener(Event.MOUSE_DOWN, switchTouch, lightSwitch)
powerSwitch:addEventListener(Event.MOUSE_DOWN, switchTouch, powerSwitch)
end |
Comments
As soon as lightCheck is done it may be garbagecollected, and it is useless to load it if hitTestPoint is false.
Likes: Ninjadoodle
Thank for the feedback! I'just trying to optimise things a little bit and trying to understand how to do so