{"id":91,"date":"2015-07-24T17:51:00","date_gmt":"2015-07-24T17:51:00","guid":{"rendered":"https:\/\/therenaissanceraven.com\/?p=91"},"modified":"2015-07-24T17:51:00","modified_gmt":"2015-07-24T17:51:00","slug":"libgdx-creating-a-main-menu","status":"publish","type":"post","link":"https:\/\/therenaissanceraven.com\/?p=91","title":{"rendered":"*libGDX* Creating a Main Menu"},"content":{"rendered":"<p>Woohoo! We are actually going to create than darn main menu now. Since we are going to be clicking past the main menu we are going to have to create a screen to hold our formal game. So to do this we will create a new class in the package and call it GameScreen, then build the base model the same as shown below (Note: Where it says AdlermacGame, replace that with the name of your name Game Class):<\/p>\n<div style=\"background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #008800; font-weight: bold;\">package<\/span> com<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">mygdx<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">adlermac<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">com.badlogic.gdx.Gdx<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">com.badlogic.gdx.Screen<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">com.badlogic.gdx.graphics.GL20<\/span><span style=\"color: #333333;\">;<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold;\">com.badlogic.gdx.graphics.OrthographicCamera<\/span><span style=\"color: #333333;\">;<\/span><br \/><br \/><span style=\"color: #888888;\">\/**<\/span><br \/><span style=\"color: #888888;\"> * Created by God on 7\/24\/2015.<\/span><br \/><span style=\"color: #888888;\"> *\/<\/span><br \/><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">GameScreen<\/span> <span style=\"color: #008800; font-weight: bold;\">implements<\/span> Screen <span style=\"color: #333333;\">{<\/span><br \/>    <span style=\"color: #888888;\">\/\/Creates a reference to hold our \"passed\" game. Change \"AdlermacGame\" to you game class name.<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">private<\/span> AdlermacGame game<span style=\"color: #333333;\">;<\/span><br \/><br \/>    <span style=\"color: #888888;\">\/\/Defining the camera to be used<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">private<\/span> OrthographicCamera camera<span style=\"color: #333333;\">;<\/span><br \/><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">GameScreen<\/span><span style=\"color: #333333;\">(<\/span>AdlermacGame game<span style=\"color: #333333;\">){<\/span><br \/>        <span style=\"color: #888888;\">\/\/assigns the game we passed to the placeholder<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">game<\/span><span style=\"color: #333333;\">=<\/span>game<span style=\"color: #333333;\">;<\/span><br \/>        <span style=\"color: #888888;\">\/\/creates the camera<\/span><br \/>        camera <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> OrthographicCamera<span style=\"color: #333333;\">();<\/span><br \/>        <span style=\"color: #888888;\">\/\/Sets the screen to be 800 width by 480 height<\/span><br \/>        camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setToOrtho<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">800<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">480<\/span><span style=\"color: #333333;\">);<\/span><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #888888;\">\/\/this is called every time the game is rendered<\/span><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">render<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">float<\/span> deltaTime<span style=\"color: #333333;\">){<\/span><br \/>        <span style=\"color: #888888;\">\/\/Sets the color to be applied after clearing the screen (R,G,B,A)<\/span><br \/>        Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">gl<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">glClearColor<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">255<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">);<\/span><br \/>        <span style=\"color: #888888;\">\/\/Clears the screen<\/span><br \/>        Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">gl<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">glClear<\/span><span style=\"color: #333333;\">(<\/span>GL20<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">GL_COLOR_BUFFER_BIT<\/span><span style=\"color: #333333;\">);<\/span><br \/><br \/>        <span style=\"color: #888888;\">\/\/updates camera view<\/span><br \/>        camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">update<\/span><span style=\"color: #333333;\">();<\/span><br \/><br \/>        <span style=\"color: #888888;\">\/\/makes the camera fit onto the available screen<\/span><br \/>        game<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">gameBatch<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setProjectionMatrix<\/span><span style=\"color: #333333;\">(<\/span>camera<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">combined<\/span><span style=\"color: #333333;\">);<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">resize<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> width<span style=\"color: #333333;\">,<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> height<span style=\"color: #333333;\">){<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">show<\/span><span style=\"color: #333333;\">(){<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">hide<\/span><span style=\"color: #333333;\">(){<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">pause<\/span><span style=\"color: #333333;\">(){<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">resume<\/span><span style=\"color: #333333;\">(){<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><br \/>    <span style=\"color: #555555; font-weight: bold;\">@Override<\/span><br \/>    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">dispose<\/span><span style=\"color: #333333;\">(){<\/span><br \/><br \/>    <span style=\"color: #333333;\">}<\/span><br \/><span style=\"color: #333333;\">}<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now that we have created the next screen, we will move to placing a button on the screen that can be clicked. To do this, I simply made a very simply button that reads &#8220;play&#8221; and is 200px wide by 100px tall:<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/2.bp.blogspot.com\/-bz-Sqh1jetE\/VbKBLXxn2II\/AAAAAAAAAYA\/enbxuyU_TOA\/s1600\/playbutton.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/2.bp.blogspot.com\/-bz-Sqh1jetE\/VbKBLXxn2II\/AAAAAAAAAYA\/enbxuyU_TOA\/s1600\/playbutton.png\" \/><\/a><\/div>\n<p>This button was then placed in the Assets folder. Then its pretty simply to draw it onto our screen, you simply will need to add this line of code within the render method of the MainMenuClass:<br \/>&nbsp; <br \/><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\">1<br \/>2<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #888888;\">\/\/draws the play button in the middle of the screen<\/span><br \/>        game<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">gameBatch<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">draw<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> Texture<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"playbutton.png\"<\/span><span style=\"color: #333333;\">),<\/span><span style=\"color: #0000dd; font-weight: bold;\">300<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">190<\/span><span style=\"color: #333333;\">);<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Once it is added, we need to see when the person clicks on it, we do that by using the previous (is.touched) method before for creating all of our objects. I am going to leave the object creation portion of the code in due to its complete awesomeness! ha..ha.. anyways, the new section of code is the following:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"> 1<br \/> 2<br \/> 3<br \/> 4<br \/> 5<br \/> 6<br \/> 7<br \/> 8<br \/> 9<br \/>10<br \/>11<br \/>12<\/pre>\n<\/td>\n<td>\n<pre style=\"line-height: 125%; margin: 0;\"><span style=\"color: #888888;\">\/\/Code snippet that runs when being touched<\/span><br \/>        <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">input<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isTouched<\/span><span style=\"color: #333333;\">()){<\/span><br \/>            <span style=\"color: #888888;\">\/\/Creates a new Object (which is our moving thingies) with a random x and random y speed<\/span><br \/>            MyObject myObject <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> MyObject<span style=\"color: #333333;\">(<\/span>MathUtils<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">random<\/span><span style=\"color: #333333;\">(-<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">),<\/span>MathUtils<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">random<\/span><span style=\"color: #333333;\">(-<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">));<\/span><br \/>            <span style=\"color: #888888;\">\/\/Adds our new created object to our object array<\/span><br \/>            myObjectArray<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">add<\/span><span style=\"color: #333333;\">(<\/span>myObject<span style=\"color: #333333;\">);<\/span><br \/><br \/>            <span style=\"color: #888888;\">\/\/Checks if the click was in the bounds of the Play Button<\/span><br \/>            <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">input<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getX<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">300<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">input<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getX<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">500<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">input<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getY<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">190<\/span> <span style=\"color: #333333;\">&amp;&amp;<\/span> Gdx<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">input<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getY<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">&lt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">290<\/span><span style=\"color: #333333;\">){<\/span><br \/>                game<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">setScreen<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #008800; font-weight: bold;\">new<\/span> GameScreen<span style=\"color: #333333;\">(<\/span>game<span style=\"color: #333333;\">));<\/span><br \/>            <span style=\"color: #333333;\">}<\/span><br \/>        <span style=\"color: #333333;\">}<\/span><br \/><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>And with that addition, we will be able to switch click the play button and bring you to the next screen, albeit, blank. But, perfect for our next phase of work!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Woohoo! We are actually going to create than darn main menu now. Since we are going to be clicking past the main menu we are going to have to create a screen to hold our formal game. So to do this we will create a new class in the package and call it GameScreen, then&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":0,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"wp:attachment":[{"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/therenaissanceraven.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}