Share Your Code

Simple menu system for Director Class

Posted by davidchiu, Posted on September 28, 2011

When prototyping a game using the Director Class (http://developer.anscamobile.com/code/director-class-10) it's useful to have a simple menu to test out different scenes. I quickly built this simple menu system to help quickly test scenes/levels.

menu.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module(..., package.seeall)
 
local localGroup = display.newGroup()
 
------------------------------------------------------------------------------------
-- This is the array that holds all the links and their associated scenes
------------------------------------------------------------------------------------
local links = {
-- {'button label', 'director scene'}
        {'Link1','Scene1'},
        {'Link2','Scene2'},
        {'Link3','Scene3'},
        {'Link4','Scene4'},
}
 
local buttons = {}
 
-----------------------------------
-- Clear all objects from memory
-----------------------------------
local function freeMem()
  for i = 1, #buttons do
    buttons[i]:removeSelf()
    buttons[i] = nil
  end
 
  buttons = nil
end
 
---------------------------------------------------------------
-- NEW
---------------------------------------------------------------
 
function new()
    local y = 30 -- initial Y position for text
 
    for i = 1, #links do
      buttons[i] = display.newText(links[i][1], 0, 0, 'ArialMT', 20)
      buttons[i]:setTextColor(255, 255, 255, 255)
      buttons[i]:setReferencePoint(display.CenterReferencePoint)
      buttons[i].x = display.contentWidth/2
      buttons[i].y = (i * 30) + y
 
      buttons[i]:addEventListener("tap", function()
        freeMem()
        director:changeScene(links[i][2])
        end)
      localGroup:insert(buttons[i])
    end
 
        return localGroup
 
end