Corona Game Controller – game loop management

All games need a game loop. It is the guts of your game and where all of the action happens, so it is important that it is controlled well and that it doesn’t get out of hand. I like to keep things super organised and prefer to only have one game loop happening in my game. So for this, I always use a singleton class. So whilst developing my game with Lua & the Corona SDK I endeavored to maintain this architecture within my game. The GameController class that I created is used to control all of the timings and enterFrame animations. It needs to be callable from anywhere within my game and I need to be able to add callback functions to the game loop and remove them for that case. Using a singleton is perfect as it means that I know that only one GameController is in use and if I need to add an enterFrame loop within one of my levels – in fact I usually use them in all of them – I just make a call to the GameController’s addCallback method to add a method from my level to the GameController’s loop.

  1. GameController.getInstance().addCallback(levelOneLoop, 'levelOneLoop')

The two parameters for the addCallback method are the actual method to be added as a callback and a name for the method as a String. This string is used to add the method to a table with a reference value, it also helps when removing the game from the loop, by calling the removeCallbackByName method, which removes the method from the main game loop.

  1. GameController.getInstance().removeCallbackByName('levelOneLoop')

The GameController also contains a method called beginGame, which is called to begin the game, as well as pause, stop & resume methods to control the loop at anytime.

Anyway, here is the class in its entirity:

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

  1. local _instance, callbacks = nil, {}
  2.  
  3. function GameController.getInstance()
  4.     if not _instance then
  5.         _instance = GameController
  6.     end
  7.  
  8.     local function gameLoop()
  9.         for i, callback in pairs(callbacks) do
  10.             if callback ~= nil then
  11.                 callbacks[i]()
  12.             end
  13.         end
  14.     end
  15.  
  16.     _instance.getCallbacks = function()
  17.         return callbacks
  18.     end
  19.  
  20.     _instance.removeCallbackByName = function(name)
  21.         callbacks[name] = nil
  22.     end
  23.  
  24.     _instance.addCallback = function(callback, name)
  25.         callbacks[name] = callback
  26.     end
  27.  
  28.     _instance.clearAllCallbacks = function()
  29.         callbacks = {};
  30.     end
  31.  
  32.     _instance.stop = function()
  33.         Runtime:removeEventListener("enterFrame", gameLoop)
  34.     end
  35.  
  36.     _instance.pause = function()
  37.         _instance.stop();
  38.     end
  39.  
  40.     _instance.resume = function()
  41.         _instance.beginGame();
  42.     end
  43.  
  44.     _instance.beginGame = function()
  45.         Runtime:addEventListener("enterFrame", gameLoop)
  46.     end
  47.  
  48.     _instance.main = function()
  49.         _instance.beginGame()
  50.     end
  51.  
  52.     return _instance
  53. end
  54.  
  55. function GameController:new()
  56.     assert(nil, 'GameController is a singleton and cannot be instantiated - use getInstance() instead')
  57. end

Happy coding.

This entry was posted in Games, Lua and tagged , . Bookmark the permalink.

 

 

 

[출처] http://ultravisual.co.uk/blog/2012/02/28/corona-game-controller-game-loop-management/

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
1 [no code 게임개발] PlayMaker 요약 file 졸리운_곰 2025.03.05 250
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED