|
-
Flash Incompetent
[CS3] dynamic game menu advice?
Does anyone have any advice on how they handle dynamic menus in a game, for example in my game people will enter different building which will all have different menus and sub menus and some of these items when selected will require different actions to be taken. For example entering one building a menu might come up with buy, sell, identify, talk, and leave another menu might have rest, save, talk, and leave. Basically its really easy to get the first menu, I have all my buildings marked with a different code, shop types are 1, inn's are two, etc... but my problem comes with the menu and navigating them.
I have it set up with 5 separate text boxes each one will be an option but I'm not sure how to set it up so once someone selects buy, items display and can be scrolled through and then when one of those is selected it knows to buy and go back to the first menu. I was currently doing it by classifying each option as a mode for example if they enter shoptype 1 it will run
Code:
displayOption(1, "buy",101,building1);
optionMode[1] = 101;
displayOption(1, "sell",102,building1);
optionMode[2] = 102;
displayOption(1, "identify",103,building1));
optionMode[3] = 103;
displayOption(1, "talk",104,building1));
optionMode[4] = 104;
displayOption(1, "leave",105,building1));
optionMode[5] = 105;
and then when you select an item it checks what mode, for example 101 means bring up the item list for building1 and all the items will have mode 110 which means buy item and go back to main menu once purchase is complete. Is there a much smarter way to do this because I get the feeling I'm missing a much easier way of programming this...
Thanks in advance, and lol, please don't laugh at me if my current way of doing it is that dumb...
-ChaseNYC
-
M.D.
I would approach it in a event listener type way.
A menu item knows nothing about what it needs to do, all it does is tell something its been clicked. That something listens for the evnt and handles the action.
seperate each shop into a class.
Inn extends Shop
AmmoShop extends Shop
MagicShop extends Shop
Shop class has functionality for which all shops need, the ability to create menu's. The Shop class however should not have any functionlity about the specifics of these menus, this is handles by the specific Shop. So in the "Inn" class, you might have something like:
Code:
package {
public class Inn extends Shop {
private var menu1:Menu;
private var menu2:Menu;
private var menu3:Menu;
private var menu4:Menu;
public function Inn():void {
}
public function initShop() {
menu1 = createMenu("inn", onOpenMenu);
menu2 = createMenu("sleep", onSleepPress);
menu3 = createMenu("buy beer", onBeerPress);
menu4 = createMenu("exit", onCloseMenu);
menu1.appendMenu(menu2);
menu1.appendMenu(menu3);
menu1.appendMenu(menu4);
}
private function onBeerPress(evt:MenuEvent){
trace("add a beer")
}
private function onSleepPress(evt:MenuEvent){
trace("get some sleep")
}
}
}
Code:
package {
public class Shop {
public function Shop():void {
}
protected function createMenu(title:String, func:Function):Menu {
var menu:Menu = new Menu(title);
menu.addEventListener(MenuEvent.PRESS, func);
return menu;
}
protected function onOpenMenu(evt:MenuEvent):void {
evt.menu.open();
}
protected function onCloseMenu(evt:MenuEvent):void {
evt.menu.close();
}
}
}
of course that wouldn't work out of the box, you need to create your Menu class and a MenuEvent class and find a way to add menu's to an objects display list, but hopefully you get the idea. Use events and functions. Seperate into classes / objects and handle the data specifically in those places. No need for massive if / switch statements, id's...
Last edited by mr_malee; 07-07-2008 at 09:59 PM.
-
Flash Incompetent
Thanks for the quick response, this is going to take some time for me to digest, but I was thinking of doing something similar but I think it will take some time for me to get it right. I do have one quick question to ask, even though I might have more later. If I'm not using mouse clicks, (basically I'm trying to do it like a console game so I'm not using the mouse at all) to still use event listeners based off of whatever item is highlighted when the user hits the designated select key, or do I just have to resort to switch statement or if then statements?
-
M.D.
thats the beauty of events. You can make your own event and dispatch it whenever you want.
I would probably change it to something like:
MenuEvent.SELECT or MenuEvent.SELECTED. Remember all that means is the type of event, its just a string variable, its just a neat way of declaring event types.
here's a custom event class I made which I use when I want to listen to events on the time line. Just creating the class doesn't do anything, Its only purpose is to add additional event types to the event class and help me to keep type safe. I could totally ditch the class and simply pass a string when I want to dispatch/add an event, but I choose not to, because its messy and clumsy.
Code:
package com.malee.events {
import flash.events.Event;
public class MovieClipEvent extends Event {
//
// -- EVENT TYPES --
//
public static const TIMELINE_END:String = "timelineEnd";
public static const TIMELINE_START:String = "timelineStart";
public static const TIMELINE_EVENT:String = "timelineEvent";
//
// -- CONSTRUCTOR --
//
public function MovieClipEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false):void {
super(type, bubbles, cancelable);
}
//
// -- PUBLIC --
//
override public function clone():Event {
return new MovieClipEvent(type, bubbles, cancelable);
}
}
}
any object can dispatch the event.
Code:
myObject.dispatchEvent( new MovieClipEvent(MovieClipEvent.TIMELINE_EVENT) )
then something listens
Code:
myObject2.addEventListener(MovieClipEvent.TIMELINE_EVENT, onTimelineEvent);
function onTimelineEvent(evt:TimelineEvent){
}
I could have just done this though:
Code:
myObject.dispatchEvent( new Event("timelineEvent") )
myObject2.addEventListener("timelineEvent", onTimelineEvent);
function onTimelineEvent(evt:Event){
}
so, if you create a custom MenuEvent class you can either just create it for the purpose of holding different event types, or you could add objects to it, like the menu which dispatched the event. And therfore dispatch this event whenever / however you want.
hopefully that made sense
Last edited by mr_malee; 07-07-2008 at 11:41 PM.
-
Flash Incompetent
ahhh, it almost makes sense.... but hahah I don't completely get it, I think I need some time to absorb it. I appreciate the help tremendously,
ChaseNYC
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|