How to Make Android 2D car game Tutorial 8

Hello what's up every-one and again i'm back for this tutorial number
8 of our android 2d Car game series, In this Tutorial I Come up with
the idea of creating an Abstract type of class that I will going to use as
Base class for most of our screen but not all, Since by the time I wrote
this code I forgot to use it for all screen to my game, If you are just
new Maybe you can't understand or will not learn at all from what i've
posted but don't worry i provided the code below just keep exploring
then surely you will learn atleast, after experimenting it with your
favorite IDE. So since this class will not be instantiated and will only
hold attributes and some method's that other classes might use as-well,
it is good to declare it with the protected Modifier, Protected type of
mode will let you access this field and method from other classes which
are on the same package.in this way we can prevent having redundant
codes from all our classes that using the same principle. in e..g Player
class can have components like Position,Size and so on, if for instance
we create another class but will have also the same principle, its a
good idea to put it in Abstract class and access those properties
whenever we need in some of our classes. I hope this lettle article
make sense to you once you create your own game.


package screens;

import com.badlogic.gdx.Application;

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.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
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 abstract class ScreenBase extends ScreenAdapter {
private static final Logger log = new Logger(
ScreenBase.class.getSimpleName(), Logger.DEBUG);
protected final GdxGame game;
protected final AssetManager manager;
protected Stage stage;
protected SpriteBatch batch;
protected Viewport viewport;

public ScreenBase(GdxGame game) {
    this.game = game;
    this.manager = game.getManager();

}

@Override
public void show() {
    log.debug("show()");
    Gdx.app.setLogLevel(Application.LOG_DEBUG);
    batch = game.getBatch();
    viewport = new FitViewport(GameConfig.HUD_WIDTH, GameConfig.HUD_HEIGHT);
    stage = new Stage(viewport, batch);
    Gdx.input.setInputProcessor(stage);
    stage.addActor(createUI());
}

protected abstract Actor createUI();

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

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

@Override
public void hide() {
    dispose();
    log.debug("hide()");

}

@Override
public void dispose() {
    stage.dispose();
    log.debug("dispose()");

}}


Post a Comment

0 Comments