This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.as
372 lines (312 loc) · 9.18 KB
/
Menu.as
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package
{
import gui.*;
import net.flashpunk.*;
import net.flashpunk.utils.*;
import net.flashpunk.graphics.*;
/**
* Full screen title menu. Seperate Flashpunk world from the game.
*/
public class Menu extends World
{
//button size and positioning
private const BUTTON_SIZE:int = 30; //size of button text normally
private const BUTTON_SIZE_SELECTED:int = 45; //size of button text when selected
private const BUTTON_SPACING:int = 7 + BUTTON_SIZE; //space between buttons
//location of menu lists
private const LIST_X:int = 40;
private const LIST_Y:int = 100;
//menu lists that hold displayed buttons
private var _mainList:Array = new Array();
private var _levelsList:Array = new Array();
private var _optionsList:Array = new Array();
private var _fade:Fader = new Fader(); //whole screen _fade effect
private var _levels:Array = new Array("old school", "level 2", "level 3"); //names of _levels
public function Menu()
{
}
override public function begin():void
{
//{ CONSTANT GRAPHICS
//background
var bg:Entity = new Entity(0, 0, new Image(Resources.MENU_BG));
add(bg);
//seperator
var seperator:Entity = new Entity(0, 80, new Image(Resources.MAIN_MENU_SEPERATOR));
add(seperator);
//}
//{ MENU LISTS
//{{ MAIN
var button:TextButton;
//title
button = new TextButton(10, 7, "breakout", 70, Resources.FONT);
button.adjustHitbox();
add(button);
_mainList.push(button);
//continue
button = new TextButton(LIST_X, LIST_Y, "play", 30, Resources.FONT);
button.onClick(continueBtn_onClick);
_mainList.push(button);
//_levels
//button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING, "_levels", 30, Resources.FONT);
//button.onClick(levelsBtn_onClick);
//_mainList.push(button);
//options
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 1, "options", 30, Resources.FONT);
button.onClick(optionsBtn_onClick);
_mainList.push(button);
//credits
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 2, "credits", 30, Resources.FONT);
button.onClick(creditsBtn_onClick);
_mainList.push(button);
//add graphics for mousing over and add to world
for (var i:int = 1; i < _mainList.length; i++)
{
_mainList[i].onMouseOver(buttonMouseOver);
_mainList[i].onMouseStray(buttonMouseStray);
_mainList[i].adjustHitbox();
add(_mainList[i]);
}
//}}
//{{ LEVELS
//title
_levelsList[0] = new TextButton(10, 7, "_levels", 70, Resources.FONT);
_levelsList[0].adjustHitbox();
add(_levelsList[0]);
//1
_levelsList[1] = new TextButton(LIST_X, LIST_Y, _levels[0], 30, Resources.FONT);
_levelsList[1].onClick(levelList_onClick);
//2
_levelsList[2] = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING, _levels[1], 30, Resources.FONT);
_levelsList[2].onClick(levelList_onClick);
//3
_levelsList[3] = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 2, _levels[2], 30, Resources.FONT);
_levelsList[3].onClick(levelList_onClick);
//back
_levelsList[4] = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 3, "back", 30, Resources.FONT);
_levelsList[4].onClick(levelList_onClick);
//add graphics for mousing over and add to world
for (var i:int = 1; i < _levelsList.length; i++)
{
_levelsList[i].onMouseOver(buttonMouseOver);
_levelsList[i].onMouseStray(buttonMouseStray);
_levelsList[i].adjustHitbox();
add(_levelsList[i]);
}
//}}
//{{ OPTIONS
//title
button = new TextButton(10, 7, "options", 70, Resources.FONT);
button.adjustHitbox();
add(button);
_optionsList.push(button);
//sound
if (Main.soundOn)
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 0, "sound: on", 30, Resources.FONT);
else
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 0, "sound: off", 30, Resources.FONT);
button.onClick(soundBtn_onClick);
_optionsList.push(button);
//music
if (Main.musicOn)
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 1, "music: on", 30, Resources.FONT);
else
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 1, "music: off", 30, Resources.FONT);
button.onClick(musicBtn_onClick);
_optionsList.push(button);
//back
button = new TextButton(LIST_X, LIST_Y + BUTTON_SPACING * 2, "back", 30, Resources.FONT);
button.onClick(backBtn_onClick);
_optionsList.push(button);
//add graphics for mousing over and add to world
for (var i:int = 1; i < _optionsList.length; i++)
{
_optionsList[i].onMouseOver(buttonMouseOver);
_optionsList[i].onMouseStray(buttonMouseStray);
_optionsList[i].adjustHitbox();
add(_optionsList[i]);
}
//}}
//}
showList("main"); //display main list and hide others
//_fade into screen at start
_fade.fadeIn(2);
add(_fade);
}
override public function update():void
{
super.update();
}
/**
* Starts a new game.
*/
private function newGame():void
{
Registry.game = new Game();
FP.world = Registry.game;
}
/**
* Changes the active menu as specified.
* @param list Name of menu to make active.
*/
private function showList(list:String):void
{
//enable/disable main menu buttons
for each (var button:Button in _mainList)
{
if (list == "main")
{
button.visible = true;
button.interactable = true;
}
else
{
button.visible = false;
button.forceStray();
button.interactable = false;
}
}
//enable/disable level menu buttons
for each (var button:Button in _levelsList)
{
if (list == "_levels")
{
button.visible = true;
button.interactable = true;
}
else
{
button.visible = false;
button.forceStray();
button.interactable = false;
}
}
//enable/disable credit menu buttons
for each (var button:Button in _optionsList)
{
if (list == "options")
{
button.visible = true;
button.interactable = true;
}
else
{
button.visible = false;
button.forceStray();
button.interactable = false;
}
}
}
//{ EVENT HANDLERS
/**
* Loads last level played.
* @param button Button that triggered the event.
*/
private function continueBtn_onClick(button:TextButton):void
{
_fade.fadeOut(.5, newGame);
}
/**
* Displays the _levels menu.
* @param button Button that triggered the event.
*/
private function levelsBtn_onClick(button:TextButton):void
{
showList("_levels");
}
/**
* Displays the options menu.
* @param button Button that triggered the event.
*/
private function optionsBtn_onClick(button:TextButton):void
{
showList("options");
}
/**
* Displays the credits screen.
* @param button Button that triggered the event.
*/
private function creditsBtn_onClick(button:TextButton):void
{
}
/**
* Starts a new game of the selected level.
* @param button Button that triggered the event.
*/
private function levelList_onClick(button:TextButton):void
{
switch (button.text)
{
case _levels[0]:
//newGame();
break;
case _levels[1]:
//newGame();
break;
case _levels[2]:
//newGame();
break;
case "back":
showList("main");
break;
}
}
/**
* Returns to a higher level in the menu.
* @param button Button that triggered the event.
*/
private function backBtn_onClick(button:TextButton):void
{
showList("main");
}
/**
* Toggles game sound.
* @param button Button that triggered the event.
*/
private function soundBtn_onClick(button:TextButton):void
{
if (Main.soundOn)
button.text = "sound: off";
else
button.text = "sound: on";
Main.soundOn = !Main.soundOn;
}
/**
* Toggles game music.
* @param button Button that triggered the event.
*/
private function musicBtn_onClick(button:TextButton):void
{
if (Main.musicOn)
button.text = "music: off";
else
button.text = "music: on";
Main.musicOn = !Main.musicOn;
}
/**
* Increases text size of button.
* Fires when any button is moused over.
* @param button Button that triggered the event.
*/
private function buttonMouseOver(button:TextButton):void
{
var oldCenterY:int = button.centerY;
button.size = BUTTON_SIZE_SELECTED;
button.adjustHitbox();
button.y += oldCenterY - button.centerY;
}
/**
* Decreases text size of button.
* Fires when the mouse leaves the hitbox of any button.
* @param button Button that triggered the event.
*/
private function buttonMouseStray(button:TextButton):void
{
var oldCenterY:int = button.centerY;
button.size = BUTTON_SIZE;
button.adjustHitbox();
button.y += oldCenterY - button.centerY;
}
//}
}
}