Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
A try at pausing (: — Gideros Forum

A try at pausing (:

H4ch1H4ch1 Member
edited February 2012 in Code snippets
Hey there,
was working on adding pause into my template. Usually, without modularizing anything, I would just have a local variable, and in each important event have a check to that variable to see if pause is active (hence don't do anything) or not.
But since Gideros support OO approaches so well, I couldn't use a local variable anymore. Using a global one would mean having a 30% slower reading of it, which is not that big of a deal, but adding a check in each object...well, wanted something a little better (:
I haven't checked all the API, so I don't know if there's a magical feature that removes and add all the listeners added, or that just freeze everything and resume it, so if it's there, please tell me :D Will add it immediately!
Meanwhile, I wrote this:
GCpauseBut = gideros.class(Bitmap);
 
function GCpauseBut:init()
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self);
end
 
function GCpauseBut:addAllListeners(sprite)
	local numChild = sprite:getNumChildren();
	if numChild ~= 0 then
		for i = 1, numChild do
			local child = sprite:getChildAt(i)
			local subNumChild = child:getNumChildren();
			if subNumChild ~= 0 then
				if child.addListeners then
					child:addListeners();
				end
				self:addAllListeners(child);
			else
				if child.addListeners then
					child:addListeners();
				end
			end
		end
	else
		if sprite.addListeners then
			sprite:addListeners();
		end
	end
end
 
function GCpauseBut:removeAllListeners(sprite)
	local numChild = sprite:getNumChildren();
	if numChild ~= 0 then
		for i = 1, numChild do
			local child = sprite:getChildAt(i)
			local subNumChild = child:getNumChildren();
			if subNumChild ~= 0 then
				if child.removeListeners then
					child:removeListeners();
				end
				self:removeAllListeners(child);
			else
				if child.removeListeners then
					child:removeListeners();
				end
			end
		end
	else
		if sprite.removeListeners then
			sprite:removeListeners();
		end
	end
end
 
function GCpauseBut:onMouseDown(event)
	if self:hitTestPoint(event.x, event.y) then
		if self.paused then
			self.paused = false;
			self:addAllListeners(stage);
		else
			self.paused = true;
			self:removeAllListeners(stage);
		end
		event:stopPropagation();
	end
end
It cycles through each object in the stage, searching for removeListeners or addListeners functions.

You can create one with
pausebut = GCpauseBut.new(texture);

And each one of the object you want to stop, must have a removeListeners() function and a addListeners() one, containing all the thing you want to remove or add (listeners, but even other things).

Likes: anneMurielle

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • atilimatilim Maintainer
    edited February 2012
    I think pausing is always hard to implement and I like your way :)

    I usually prefer this way:
    1. I use a global variable like g_paused

    2. I put
    if g_paused then return end
    to the beginning of all Event.ENTER_FRAME events

    3. I use Timer.pauseAllTimers and Timer.resumeAllTimers to temporarily pause and resume all timers

    4. My pause menu sprite calls event:stopPropagation() for mouse/touch events so that none of the sprites below it receives mouse/touch events.

    5. I use GTween.pauseAll to temporarily pause and resume all tweens
  • H4ch1H4ch1 Member
    edited February 2012
    Yup, but as I stated above, that way kinda drags performance. Reading from a global variable instead of a local takes 30% more time, and it's a check in each one of the enterFrame events. I'm kind of a performance/optimization freak d: .
    But I used to use that for semplicity with the other sdk, where class approach wasn't as well implemented as in Gideros. Just with a local instead of a global (;
  • atilimatilim Maintainer
    edited February 2012
    I got your point :) When the optimization is in question, it's always hard to decide these issues.
  • atilimatilim Maintainer
    btw, as time passes, I like your way more and more. :)

  • Thanks, it has its positive sides once all the pieces come together (:
    For example, with my AnimationManager class, I put a self:pause() call and a self:play() call in addListeners and removeListeners of my main player object to make it pause its animation and resume it. Helps keeping the code clean (;
  • hey,do you have a simple timer countdown with pause and resume? I have a countdown but the code is hard to understand and hard to put a pause and resume...please reply,thanks...I just need it for my thesis educational quiz game :)
Sign In or Register to comment.