How to Make Android 2D car game Tutorial 3

 

Class Attributes:AssetManager,Stage,Viewport,SpriteBatch, Table,TextureRegion,BitmapFont,Music,Sound,TextureAtlas,TextButton,Label

Class Description:Table: A group that sizes and positions children using table constraints. By default, Actor.getTouchable() is Touchable.childrenOnly. The preferred and minimum sizes are that of the children when laid out in columns and rows.

Stage: A 2D scene graph containing hierarchies of actors. Stage handles the viewport and distributes input events. setViewport(Viewport) controls the coordinates used within the stage and sets up the camera used to convert between stage coordinates and screen coordinates. A stage must receive input events so it can distribute them to actors. This is typically done by passing the stage to Gdx.input.setInputProcessor. An InputMultiplexer may be used to handle input events before or after the stage does. If an actor handles an event by returning true from the input method, then the stage’s input method will also return true, causing subsequent InputProcessors to not receive the event. The Stage and its constituents (like Actors and Listeners) are not thread-safe and should only be updated and queried from a single thread (presumably the main render thread). Methods should be reentrant, so you can update Actors and Stages from within callbacks and handlers.

TextureRegion: Defines a rectangular area of a texture. The coordinate system used has its origin in the upper left corner with the x-axis pointing to the right and the y axis pointing downwards.

Music: A Music instance represents a streamed audio file. The interface supports pausing, resuming and so on. When you are done with using the Music instance you have to dispose it via the dispose() method. Music instances are created via Audio.newMusic(FileHandle). Music instances are automatically paused and resumed when an Application is paused or resumed. See ApplicationListener.

Sound: A Sound is a short audio clip that can be played numerous times in parallel. It’s completely loaded into memory so only load small audio files. Call the dispose() method when you’re done using the Sound. Sound instances are created via a call to Audio.newSound(FileHandle). Calling the play() or play(float) method will return a long which is an id to that instance of the sound. You can use this id to modify the playback of that sound instance.

TextureAtlas: Loads images from texture atlases created by TexturePacker. A TextureAtlas must be disposed of to free up the resources consumed by the backing textures.

TextButton: A button with a child Label to display text.

Label: A text label, with optional word wrapping.The preferred size of the label is determined by the actual text bounds, unless word wrap is enabled.

package screens;
import assets.AssetsDescriptors;
import assets.RegionNames;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Logger;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.ninal.game.GdxGame;
import config.GameConfig;

public class MenuScreen extends ScreenAdapter {
private static final Logger log = new Logger(MenuScreen.class.getSimpleName(), Logger.DEBUG);
 private final GdxGame game;
 private final AssetManager manager;
 private Stage stage;
 private Viewport viewport;
 private SpriteBatch batch;
 private TextureRegion bgRegion;
 private BitmapFont font;
 private Music menuMusic;
 private Sound selectSound_3;
public MenuScreen(GdxGame game) {
 this.game = game;
 this.manager = game.getManager();
 }
       @Override
 public void show() {
 batch = game.getBatch();
 viewport = new FitViewport(GameConfig.HUD_WIDTH, GameConfig.HUD_HEIGHT);
 stage = new Stage(viewport, batch);
 Gdx.input.setInputProcessor(stage);
 font = manager.get(AssetsDescriptors.FONT);
 menuMusic = manager.get(AssetsDescriptors.FLY_SOUNDS);
 selectSound_3 = manager.get(AssetsDescriptors.NAVIGATE_SOUND_3);

 TextureAtlas btnAtlas = manager.get(AssetsDescriptors.BTN_ATLAS);
 Skin btnSkin = manager.get(AssetsDescriptors.BTN_SKIN);
 TextureAtlas gameAtlas = manager.get(AssetsDescriptors.GAME_ATLAS);
 Skin uiSkin = manager.get(AssetsDescriptors.UISKIN);
 bgRegion = gameAtlas.findRegion(RegionNames.MENU_BG);
 TextureRegion menubgRegion = btnAtlas.findRegion(RegionNames.PANEL);

 Table table = new Table();
 table.defaults().pad(20);

 TextButton playbtn = createTextButton("PLAY", uiSkin);
 playbtn.getLabelCell().pad(4, 50, 4, 50);
 playbtn.addListener(new ChangeListener() {
 @Override
 public void changed(ChangeEvent event, Actor actor) {
 play();
 }
 });

 TextButton optionbtn = createTextButton("OPTION", uiSkin);
 optionbtn.getLabelCell().pad(4, 35, 4, 35);
 optionbtn.addListener(new ChangeListener() {

 @Override
 public void changed(ChangeEvent event, Actor actor) {
 options();
 }
 });

 TextButton highScorebtn = createTextButton("HIGHSCORE", uiSkin);
 highScorebtn.getLabelCell().pad(4, 8, 4, 8);
 highScorebtn.addListener(new ChangeListener() {

 @Override
 public void changed(ChangeEvent event, Actor actor) {
 showHighScore();
 }
 });

 TextButton quitbtn = createTextButton("QUIT", uiSkin);
 quitbtn.getLabelCell().pad(4, 50, 3, 55);
 quitbtn.addListener(new ChangeListener() {

 @Override
 public void changed(ChangeEvent event, Actor actor) {
 quit();
 }
 });

 ClickListener mouseMove = new ClickListener() {
 @Override
 public boolean mouseMoved(InputEvent event, float x,float y) {
 selectSound_3.play();
 menuMusic.play();

 return super.mouseMoved(event, x, y);
 }
 };

 playbtn.addListener(mouseMove);
 optionbtn.addListener(mouseMove);
 highScorebtn.addListener(mouseMove);
 quitbtn.addListener(mouseMove);

 Label menu = new Label("MENU", uiSkin);
 Table menu_btn = new Table();
 menu_btn.defaults().pad(5);
 menu_btn.add(menu).row();
 menu_btn.add(playbtn).center().row();
 menu_btn.add(optionbtn).center().row();
 menu_btn.add(highScorebtn).center().row();
 menu_btn.add(quitbtn).center().row();
 menu_btn.setBackground(new TextureRegionDrawable(menubgRegion));

 table.add(menu_btn);
 table.setBackground(new TextureRegionDrawable(bgRegion));
 table.setFillParent(true);
 table.center();
 table.pack();
 stage.addActor(table);
 }

 @Override
 public void render(float delta) {
 Gdx.gl20.glClearColor(0, 0, 0, 0);
 Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
 stage.act();
 stage.draw();

 }

 @Override
 public void pause() {

 }

 @Override
 public void resume() {

 }

 @Override
 public void resize(int width, int height) {
 viewport.update(width, height, true);
 }

 @Override
 public void hide() {

 dispose();
 }

 @Override
 public void dispose() {
 stage.dispose();

 }

 private void play() {
 game.setScreen(new GameScreen(game));
 menuMusic.pause();
 }

 private void options() {
 game.setScreen(new OptionScreen(game));
 menuMusic.pause();

 }

 protected void showHighScore() {
 game.setScreen(new HighScoreScreen(game));
 menuMusic.pause();
 }

 protected void quit() {
 Gdx.app.exit();
 }

 private TextButton createTextButton(String text, Skin skin){
 TextButton textbtn = new TextButton(text, skin);
 return textbtn;
 
 }
}

Post a Comment

0 Comments