Menu system for ISS

ISS started with an arrow driven menu designed by Abel Toy for indie(Magazine);, but when i decided to release it I changed it all for mouse clicks. It was just better, to control it with mouse. I did it with no entities just text and here’s the code.

package worlds.objs
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.graphics.Graphiclist;
	import net.flashpunk.utils.Input;
	/**
	 * ...
	 * @author Konstantinos Egarhos
	 */
	public class Menu_Obj extends Entity
	{
		protected var title:Text;
		protected var selection:Array;
		protected var back:Text;
		protected var returnBack:Boolean; // When text back is highlighted.
		protected var selected:Menu_Obj; // Returns the menu to which to change the screen.
		public var updates:Boolean; // When false the player can't press enter to choose the same option probably because the screen is doing something.
		protected var menu:Graphiclist;
		private var choice:Number = 1;
		protected var focus:Boolean; // To lose focus if no choice has been selected.
		public var fadeIn:Boolean;
		public var fadeOut:Boolean;

		// Gets-Sets
		public function get returnBackG():Boolean
		{
			return returnBack;
		}
		public function get choiceG():Number
		{
			return choice;
		}
		protected function set choiceS(setValue:int):void
		{
			if (setValue == 0)
			{
				choice = 0;
			}else
			{
				choice += setValue;
				if (choice < 0)
				{
					choice = 0;
				}else if (choice > selection.length -1)
				{
					choice = selection.length -1;
				}
			}
		}
		protected function set choiceSM(setValue:int):void
		{
			choice = setValue;
		}
		public function get selectedG():Menu_Obj
		{
			return selected;
		}
		public function set selectedS(setValue:Menu_Obj):void
		{
			selected = setValue;
		}

		public function Menu_Obj()
		{
			selected = null;
			updates = true;
			returnBack = false;
		}

		override public function update():void
		{
			if (selected == null && updates)
			{
				checkInput();
			}
		}

		/**
		 * Checks the input.
		 */
		public function checkInput():void
		{
			/*if (Input.pressed("down"))
			{
				Text(selection[choiceG]).alpha = 0.5;
				choiceS = 1;
				Text(selection[choiceG]).alpha = 1;
			}
			else if (Input.pressed("up"))
			{
				Text(selection[choiceG]).alpha = 0.5;
				choiceS = -1;
				Text(selection[choiceG]).alpha = 1;
			}*/

			focus = false;

			for (var i:uint = 0; i < selection.length; i++)
			{
				if (Input.mouseX >= Text(selection[i]).x && Input.mouseX <=  Text(selection[i]).width + Text(selection[i]).x)
				{
					if (Input.mouseY >= Text(selection[i]).y && Input.mouseY <=  Text(selection[i]).height + Text(selection[i]).y)
					{
						focus = true;

						if (choiceG != i)
						{
							for (var j:uint = 0; j < selection.length; j++)
							{
								if (j != i)
								{
									Text(selection[j]).alpha = 0.5;
								}else
								{
									Text(selection[j]).alpha = 1;
									choiceSM = j;
								}
							}
						}
					}
				}
			}
			if (back != null)
			{
				if (Input.mouseX >= Text(back).x && Input.mouseX <= Text(back).x + Text(back).width)
				{
					if (Input.mouseY >= Text(back).y && Input.mouseY <= Text(back).y + Text(back).height)
					{
						focus = true;
						if (Text(back).alpha != 1)
						{
							Text(back).alpha = 1;
							returnBack = true;
						}
					}else
					{
						if (Text(back).alpha != 0.5)
						{
							Text(back).alpha = 0.5;
							returnBack = false;
						}
					}
				}
			}

			if (!focus)
			{
				for (var d:uint = 0; j < selection.length; j++)
				{
					if (Text(selection[j]).alpha != 0.5)
					{
						Text(selection[j]).alpha = 0.5;
					}
				}
				if (choiceG != 100)
				{
					choiceSM = 100;
				}
			}
		}
	}
}

and here is how to use it. This is the difficulty selection screen.

package worlds.objs
{
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Graphiclist;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Input;
	import worlds.Level;
	/**
	 * ...
	 * @author Konstantinos Egarhos
	 */
	public class DifSel_Obj extends Menu_Obj
	{

		public function DifSel_Obj()
		{
			selection = new Array();
			selected = null;
			title = new Text(String("Select Difficulty"));
			title.font = 'FONT_TITLE';
			title.align = "center";
			title.size = 20;
			title.x = FP.halfWidth - title.width / 2;
			title.y = 20;
			title.color = 0x32cd32; // Dark Green
			menu = new Graphiclist(title);
			updates = true;

			selection.push(new Text(String("Normal")));
			selection.push(new Text(String("Brutal")));

			for (var i:uint = 0; i < selection.length; i++)
			{
				Text(selection[i]).font = 'FONT_CHOICE';
				Text(selection[i]).size = 16;
				Text(selection[i]).align = "center";
				Text(selection[i]).x = FP.halfWidth - Text(selection[i]).width / 2;
				Text(selection[i]).y = FP.height / (selection.length + 1) * (i + 1);
				Text(selection[i]).color = 0xFFFFFF; // White
				Text(selection[i]).alpha = 0.5;
				menu.add(selection[i]);
			}

			back = new Text(String("back"));
			Text(back).font = 'FONT_CHOICE';
			Text(back).size = 10;
			Text(back).x = 5;
			Text(back).y = FP.height - (Text(back).height);
			Text(back).color = 0x00CC00;
			Text(back).alpha = 0.5;
			menu.add(back);

			Text(selection[0]).alpha = 0.5;

			fadeIn = fadeOut = false;

			choiceS = 0;
			graphic = menu;
		}

		override public function update():void
		{
			super.update();
		}

		override public function checkInput():void
		{
			super.checkInput();

			if (Input.pressed("enter") || Input.mousePressed)
			{
				switch (choiceG)
				{
					case 0:
						fadeOut = true;
						FP.alarm(1, normalGame);
						Log.CustomMetric("Normal", "difficulty");
						updates = false;
						break;
					case 1:
						fadeOut = true;
						FP.alarm(1, brutalGame);
						Log.CustomMetric("Brutal", "difficulty");
						GlobalVariables.BRUTALSCORE = 0;
						updates = false;
						break;
					default:
						break;
				}
			}
		}

		/**
		 * Starts level 1 from normal difficulty.
		 */
		public function normalGame():void
		{
			FP.world = new Level(1,false);
		}

		/**
		 * Starts level 1 from brutal difficulty.
		 */
		public function brutalGame():void
		{
			FP.world = new Level(1, true);
		}
	}

}

Explanation: The menu system consists of three text objects. Title – the title of the menu, selection – an array of text objects those that can be selected by player, back – the back button. Variable selected is another menu_Obj class which takes the next menu to change this one. The way it is changed is out of the scope of this class but what i did was create it to the right outside from view and then move both to the left. Next recycle that one.

Sometimes i didn’t want to have these functions e.g. the Leaderboards didn’t have choices. That’s when I overrided the updates and replaced it with an empty update function. Generally I wanted to have and an icon for sound mute, but i didn’t make it, though i advise you to make something like it.

Posted on 9 November, 2011, in Actionscript 3, FlashPunk, ISS, Programming and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: