Class Attributes: AssetManager,Stage,Viewport,SpriteBatch, OrthographicCamera, Skin, Actor
With in this tutorial we will finally create our HighScore Screen, before
will begin to code we need to think first what classes should we need
inorder to create our HighScore screen, and in this case I end up
extending the "ScreenAdapter" class so the first step i did is to declare
attributes field as private, I declare "private final GdxGame" class this
is the first libgdx class that will occur in our project after we generate.
next attribute will be the "SpriteBatch" Why we need SpriteBatch? Every
game need one instance of this class and this type of class is
responsible for handling a drawing in our canvas, namely
shapes,textures, sprites and so on that we can relate for graphic paint.
I mention earlier about ScreenAdapter class, This type of class
implement "Screen" Interface and since Screen Represents one of many
application Screens, such as main menu, A settings, menu, the game
screen and so on, You can think of screen as Activity if you are in
Android application, Take note that dispose() method is not called
automatically, that is why we need to called this method when the
screen should release all resources. There are few methods that this
interface has like hide(),pause(),render(float dt),resize(),resume
(),show(). let's take a look the method details in each one of this,
Method info
show --> Called when this screen becomes the current screen for a
Game
render-->Called when the screen should render itself
Parameters:
delta --> The time in seconds since the last render
resize-->Called when the application window width and height changes
and this method adopt the new configuration
hide-->Called when this screen is no longer the current Screen for a
Game
dispose--> Called when this screen should release all resources.
so here we are naming all the methods that require to create our
HighScore Screen and giving description so we can know when we
should use it ok?
The next Attribute will be the skin, and now again we meet a new class.
so what this class do to our HighScore Screen, We need this class if we
want to pack all of our assets into one file,This Skin Need two types of
file reference namely "atlas", and "json" this 2 look for match reference
name and return the value as you requested like font,label,texture and
other UI widget…to ease the pain we use this, so we don't to worry
about putting bunch of resources in our asset… directory. To give you
the exact difinition of the class see the libgdx statement below
Skin-->"A skin stores resources for UI widgets to use (texture regions,
ninepatches, fonts, colors, etc). Resources are named and can be
looked up by name and type. Resources can be described in JSON. Skin
provides useful conversions, such as allowing access to regions in the
atlas as ninepatches, sprites, drawables, etc. The get* methods return
an instance of the object in the skin. The new* methods return a copy
of an instance in the skin." I can't go further to discuss one by one, if
you want learn how to create HighScore Screen just watch my video
above or read the code below. Thanks!
package screens; import settings.GameManager; import utilities.CameraHelper; import assets.AssetsDescriptors; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Button; 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.utils.ChangeListener; import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.Viewport; import com.ninal.game.GdxGame; import config.GameConfig; public class HighScoreScreen extends ScreenAdapter { private final GdxGame game; private SpriteBatch batch; private Skin uiSkin; private Stage stage; private final AssetManager manager; private Viewport viewport; private CameraHelper cameraHelper; private OrthographicCamera camera; public HighScoreScreen(GdxGame game) { this.game = game; this.manager = game.getManager(); } @Override public void show() { batch = game.getBatch(); camera = new OrthographicCamera(); viewport = new FitViewport(GameConfig.HUD_WIDTH, GameConfig.HUD_HEIGHT,camera); stage = new Stage(viewport,batch); Gdx.input.setInputProcessor(stage); stage.addActor(createHighScoreUI()); cameraHelper = new CameraHelper(); cameraHelper.setstartPosition(GameConfig.HUD_WIDTH/2, GameConfig.HUD_HEIGHT/2); } private Actor createHighScoreUI() { uiSkin = manager.get(AssetsDescriptors.UISKIN); Table table = new Table(); table.defaults().pad(10); Table btntable = new Table(uiSkin); btntable.defaults().pad(8); Label highScore = new Label("HIGH-SCORE", uiSkin); Label back = new Label("BACK",uiSkin); String Score = GameManager.GAME_MANAGER.getHighScoreString(); Button backbtn = new Button(back,uiSkin); backbtn.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { back(); }}); backbtn.pad(0, 50, 0,50); btntable.add(highScore).row(); btntable.add(Score).row(); btntable.add(backbtn); // btntable.center(); table.add(btntable); // table.center(); table.setFillParent(true); // table.setVisible(true); // table.setDebug(true); // table.debug(); table.pack(); return table; } @Override public void render(float delta) { Gdx.gl20.glClearColor(0,0,0,0); Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); cameraHelper.handleInputKeys(delta); cameraHelper.applyTo(camera); stage.act(); stage.draw(); } @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 back() { game.setScreen(new MenuScreen(game)); } }
0 Comments