Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Scene manager and local global variables — Gideros Forum

Scene manager and local global variables

NinjadoodleNinjadoodle Member
edited October 2015 in General questions
Hi guys,

Just trying to figure out how to communicate between the different 'scene manager functions'.
Scene1 = gideros.class(Sprite)
 
function Scene1:init()
	self:addEventListener("enterBegin", self.onTransitionInBegin, self)
	self:addEventListener("enterEnd", self.onTransitionInEnd, self)
	self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
	self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end
 
function Scene1:onTransitionInBegin()
	print("scene1 - enter begin")
 
	createContainers(self)
 
	local lightSwitch = LightSwitch.new(160, 160, layerMG)
end
 
function Scene1:onTransitionInEnd()
	print("scene1 - enter end")
 
	local function lightSwitchTouch(button, event)
		if lightSwitch:hitTestPoint(event.x, event.y) then
			sceneManager:changeScene('scene2', 2, SceneManager.flipWithShade, easing.inOutQuadratic)
			lightSwitch:removeEventListener(Event.MOUSE_UP, lightSwitchTouch, lightSwitch)
		end
	end
 
	lightSwitch:addEventListener(Event.MOUSE_UP, lightSwitchTouch, lightSwitch)
end
 
function Scene1:onTransitionOutBegin()
	print("scene1 - exit begin")
end
 
function Scene1:onTransitionOutEnd()
	print("scene1 - exit end")
end
If I create a 'local' sprite 'onTransitionInBegin' ... I can't access it from inside 'onTransitionInEnd'

Do I have to make the sprite global?

Thank you heaps for any help!

Comments

  • hgy29hgy29 Maintainer
    If you want your lightSwitch Sprite to be accessible throughout your Scene1 instance, then you should make it a member of the instance:
    self.lightSwitch = LightSwitch.new(...)
  • NinjadoodleNinjadoodle Member
    edited October 2015
    Thank you for the explanation. I'm assuming that 'self.' is similar to 'this.' in javascript?
  • hgy29hgy29 Maintainer
    edited October 2015 Accepted Answer
    Yes, it is the same concept. One of the most confusing things in lua is that it is not object oriented at all at first sight. There are no such things as classes or instances, only tables. No methods, only functions.
    But you can make it act as if it was object-oriented, pretty much like in javascript.
    To me, the most important thing to know when dealing with classes/objects is lua is the following equivalence (notice the use of dot or colon depending on the construct) :
    function SomeClass:someMethod(a)
    is the same as
    SomeClass.someMethod=function(self,a)
    And similarility the two follings line are the same:
     SomeClass.someMethod(SomeClass,a)
     SomeClass:someMethod(a)

    Likes: Holonist

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks again for the explanation!
Sign In or Register to comment.