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):
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
|
package com.mygdx.adlermac;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera;
/** * Created by God on 7/24/2015. */ public class GameScreen implements Screen { //Creates a reference to hold our "passed" game. Change "AdlermacGame" to you game class name. private AdlermacGame game;
//Defining the camera to be used private OrthographicCamera camera;
public GameScreen(AdlermacGame game){ //assigns the game we passed to the placeholder this.game=game; //creates the camera camera = new OrthographicCamera(); //Sets the screen to be 800 width by 480 height camera.setToOrtho(false,800,480); }
//this is called every time the game is rendered @Override public void render(float deltaTime){ //Sets the color to be applied after clearing the screen (R,G,B,A) Gdx.gl.glClearColor(0,0,255,1); //Clears the screen Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//updates camera view camera.update();
//makes the camera fit onto the available screen game.gameBatch.setProjectionMatrix(camera.combined);
}
@Override public void resize(int width, int height){
}
@Override public void show(){
}
@Override public void hide(){
}
@Override public void pause(){
}
@Override public void resume(){
}
@Override public void dispose(){
} }
|
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 “play” and is 200px wide by 100px tall:
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:
1 2
|
//draws the play button in the middle of the screen game.gameBatch.draw(new Texture("playbutton.png"),300,190);
|
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:
1 2 3 4 5 6 7 8 9 10 11 12
|
//Code snippet that runs when being touched if(Gdx.input.isTouched()){ //Creates a new Object (which is our moving thingies) with a random x and random y speed MyObject myObject = new MyObject(MathUtils.random(-5,5),MathUtils.random(-5,5)); //Adds our new created object to our object array myObjectArray.add(myObject);
//Checks if the click was in the bounds of the Play Button if(Gdx.input.getX() > 300 && Gdx.input.getX() < 500 && Gdx.input.getY() > 190 && Gdx.input.getY() < 290){ game.setScreen(new GameScreen(game)); } }
|
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!