Libgdx制作游戏的正确方法

时间:2019-06-04 07:00:29

标签: java libgdx

最近,我开始制作“第一个”真正的RPG(其他“ RPG”是基于批处理或其他内容的文本)。我正在使用Libgdx,因为从我阅读的内容来看,掌握这些技巧很简单。

但是我感兴趣的是我做对了吗?

例如,我制作了TiledMap并绘制了摄影机等。一切“半有效”,但这是对的吗?在一个教程中,有一个人创建了线程等等,所以让我觉得我做错了

例如,这是我的GameScreen


public class GameScreen implements Screen, Runnable{
      private MenuScreen menuScreen;
        SpriteBatch batch, infobatch;
        BitmapFont font;

        private int tileWidth, tileHeight,
        mapWidthInTiles, mapHeightInTiles,
        mapWidthInPixels, mapHeightInPixels;

        private float delta;
        private InputHandler inputHandler;

        private TiledMap map;
        private AssetManager manager;
        private OrthogonalTiledMapRenderer renderer;
        private OrthographicCamera camera;

           private FitViewport playerViewport;
        private Player player;

        public GameScreen(MenuScreen mc)
        {
            menuScreen=mc;
        }



    @Override
    public void show() {

        batch = new SpriteBatch();
        infobatch = new SpriteBatch();
        font = new BitmapFont(Gdx.files.local("font/ornlan.fnt"));
        font.setColor(Color.BLACK);

        manager = new AssetManager();
        manager.setLoader(TiledMap.class, new TmxMapLoader());
        manager.load("world/test_1.tmx", TiledMap.class);
        manager.finishLoading();

        map = manager.get("world/test_1.tmx", TiledMap.class);

        MapProperties properties = map.getProperties();
        tileWidth         = properties.get("tilewidth", Integer.class);
        tileHeight        = properties.get("tileheight", Integer.class);
        mapWidthInTiles   = properties.get("width", Integer.class);
        mapHeightInTiles  = properties.get("height", Integer.class);
        mapWidthInPixels  = mapWidthInTiles  * tileWidth;
        mapHeightInPixels = mapHeightInTiles * tileHeight;

        camera = new OrthographicCamera(960.f, 720.f);
        playerViewport = new FitViewport(mapWidthInTiles, mapHeightInTiles, camera);

        camera.position.x = MathUtils.clamp(camera.position.x, camera.viewportWidth / 2, mapWidthInPixels - camera.viewportWidth /2);
        camera.position.y = MathUtils.clamp(camera.position.y, camera.viewportHeight / 2, mapHeightInPixels - camera.viewportHeight / 2);

        renderer = new OrthogonalTiledMapRenderer(map);

        player = new Player();
        inputHandler = new InputHandler();   


    }



    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.75f, 0.75f, 0.85f, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        camera.position.set(Player.x + Player.width / 2, Player.y + Player.height / 2, 0);
        camera.update();
        renderer.setView(camera);
        renderer.render();


        infobatch.begin();
        font.draw(infobatch, "Test World, Dev. purposes", 10,25);
        font.draw(infobatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 1180, 700);
        infobatch.end();


        batch.begin();
        batch.setProjectionMatrix(camera.combined);

           delta = Gdx.graphics.getDeltaTime();
           inputHandler.update();

            player.update(delta);
            player.render(batch);

            batch.end();
    }




    @Override
    public void resize(int width, int height) { 
    }

    @Override
    public void pause() {   
    }

    @Override
    public void resume() {  
    }

    @Override
    public void hide() {    
    }

    @Override
    public void dispose() { 
        batch.dispose();
        infobatch.dispose();
        font.dispose();
        renderer.dispose();
        manager.dispose();
    }




    @Override
    public void run() {
        // TODO Auto-generated method stub
    }

}

这是我的“ MainMenu”


import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public class MenuScreen extends Game {
    SpriteBatch batch;
    BitmapFont font;
    private Stage stage;
    private GameScreen gameScreen;
    private Texture btn_start;
    private TextureRegion btn_startRegion;
    private TextureRegionDrawable btn_startDrawable;
    private ImageButton btn_startIB;

    @Override
    public void create() {
        batch = new SpriteBatch();
        font = new BitmapFont(Gdx.files.local("font/ornlan.fnt"));
        font.setColor(Color.BLACK);

        btn_start = new Texture(Gdx.files.internal("ui/start_btn.png"));
        btn_startRegion = new TextureRegion(btn_start);
        btn_startDrawable = new TextureRegionDrawable(btn_startRegion);
        btn_startIB = new ImageButton(btn_startDrawable);
        btn_startIB.setPosition(10, 250);

        stage = new Stage(new ScreenViewport());
        stage.addActor(btn_startIB);
        Gdx.input.setInputProcessor(stage);

        btn_startIB.addCaptureListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {

                try {
                    System.out.println("Starting GameScreen...");
                    setGameScreen();
                } catch (Exception e) {
                    System.out.println("Error starting GameScreen E:" + e);
                }

            };
        });

    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0.67f, 0.75f, 0.98f, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
        batch.begin();
        font.getData().setScale(0.8f,0.8f);
        font.draw(batch, "Ornlan DevBuild (pa) v0.0.0.1", 1000, 25);
        batch.end();

        stage.act(Gdx.graphics.getDeltaTime()); // Perform ui logic
        stage.draw();

        super.render();
    }

    @Override
    public void dispose() {
        batch.dispose();
        font.dispose();
        super.dispose();
    }

    void setGameScreen() {
        gameScreen = new GameScreen(this);
        setScreen(gameScreen);
    }



}

0 个答案:

没有答案