Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
The better way to type this up? — Gideros Forum

The better way to type this up?

NinjadoodleNinjadoodle Member
edited October 2015 in General questions
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

  • piepie Member
    I think that there is not much difference here, however I vote for B if you don't need to lightCheck() outside the scope of switchTouch.
    As soon as lightCheck is done it may be garbagecollected, and it is useless to load it if hitTestPoint is false.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited October 2015
    Hi @pie

    Thank for the feedback! I'just trying to optimise things a little bit and trying to understand how to do so :)
Sign In or Register to comment.