加载uiskin时libgdx android app崩溃了没有日志

时间:2016-12-05 22:19:28

标签: libgdx

由于某种原因,在我的应用程序中添加按钮后,它已停止在Android上运行。它在桌面上按预期工作,但是当尝试在Android上运行时,应用程序构建成功,启动,然后在加载场景之前立即崩溃。我试图通过aLogcat查看日志,但是我在这些日志中看不到任何信息表明发生了问题。

我得出的结论是,这个问题与添加到舞台上的uiskin /按钮有关,因为我可以添加没有任何演员的舞台,应用程序仍将在android中运行。 uiskin.json文件中列出的文件都应该位于/ android / assets目录中。我做错了什么吗?我错过了什么?

uiskin.json

{
    "com.badlogic.gdx.graphics.g2d.BitmapFont":{
        "default_font": { 
            "file": "book_antiqua.fnt" 
        } 
    },
    "com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle":{
        "default": {
            "down": "default-round-down", 
            "up": "default-round", 
            "font": "default_font" 
        }
    },
    "com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle":{
        "default":  {
            "titleFont": "default_font"
        }
    }
}

主要课程

package com.freedom.thirty;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.Material;
//import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
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.ClickListener;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import com.freedom.thirty.MyCameraInputController;


public class FreedomThirty implements ApplicationListener {

    public ScreenViewport vp;
    public ScreenViewport stageVp;

    public OrthographicCamera cam;
    public MyCameraInputController camController;
    public ModelBatch modelBatch;
    public AssetManager assets;
    public Array<ModelInstance> allModelInstances = new Array<ModelInstance>();
    public Environment environment;
    public boolean loading;


    // Model instances that make up the map
    public ModelInstance bridge;
    public ModelInstance container_001;
    public ModelInstance crate_001;
    public ModelInstance crate_002;
    public ModelInstance crate_003;
    public ModelInstance crate_004;
    public ModelInstance crate_005;
    public ModelInstance grass_area;
    public ModelInstance gravel_area;
    public ModelInstance rock_wall;
    public ModelInstance water;
    public ModelInstance player;
    public ModelInstance skybox;

    public Skin skin;
    public Stage stage;
    public Table table;


    //public FPSLogger fpsLogger = new FPSLogger();


    @Override
    public void create() {

        modelBatch = new ModelBatch();
        assets = new AssetManager();
        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1f));
        //environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

        cam = new OrthographicCamera();
        cam.position.set(-4f, 4f, 4f);
        cam.lookAt(8f, 0f, -8f);
        cam.near = 1f;
        cam.far = 2000f;
        cam.update();

        // Create input controller for default scene camera. This is what allows the user to orbit around the scene
        camController = new MyCameraInputController(cam);
        // Set the position where the camera will look. This will need to come from json config file in the future
        camController.target.set(8f, 0f, -8f);

        vp = new ScreenViewport(cam);
        vp.setUnitsPerPixel(0.017f);
        vp.apply();


        stageVp = new ScreenViewport();
        stageVp.apply();
        stage = new Stage(stageVp);


        assets.load("uiskin.json", Skin.class);
        assets.load("map_003.g3db", Model.class);
        loading = true;

    }

    private void doneLoading(){ // Assets are now loaded into memory and can be accessed without error

        // Load UI skin
        skin = assets.get("uiskin.json", Skin.class);

        // Define the buttons that will be in the ui table
        final TextButton zoomOut = new TextButton("Zoom Out",skin,"default");
        final TextButton zoomIn = new TextButton("Zoom In",skin,"default");

        // Create UI table actor
        table = new Table();
        table.setWidth(stage.getWidth());
        table.align(Align.center | Align.top);

        // In the future determine what the current viewport height/width is, and set these
        //  to different sizes based on where the viewport falls between. Just like bootstrap's xs,sm,md,lg classes
        table.setSize(250f, 250f);
        table.setPosition(800f,400f);

        zoomOut.setWidth(200);
        zoomOut.setHeight(50);

        zoomIn.setWidth(200);
        zoomIn.setHeight(50);

        // Event listeners for ui buttons
        zoomOut.addListener(new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y){
                //Gdx.app.log("Zoom Out", "Press successful");
                vp.setUnitsPerPixel(vp.getUnitsPerPixel() + 0.001f);

                vp.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
                stageVp.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

                event.stop();
            }

        });
        zoomIn.addListener(new ClickListener(){

            @Override
            public void clicked(InputEvent event, float x, float y){
                //Gdx.app.log("Zoom In", "Press successful");
                vp.setUnitsPerPixel(vp.getUnitsPerPixel() - 0.001f);

                vp.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
                stageVp.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

                event.stop();
            }

        });


        // Add buttons to table
        table.row().padBottom(30).padTop(30);
        table.add(zoomOut);
        table.add(zoomIn);

        // Add table to stage
        stage.addActor(table);



        // Create an input multiplexer and add our stage and camController to it, set input processor to our new multiplexer
        InputMultiplexer im = new InputMultiplexer(stage, camController);
        Gdx.input.setInputProcessor(im);


        // Load the 3d map, and character as character happens to be in the map file as of right now
        // In the future each map and it's elements will be contained within it's own class, extended from
        //  map class
        Model model = assets.get("map_003.g3db", Model.class);        

        bridge = new ModelInstance(model, "bridge");
        allModelInstances.add(bridge);

        container_001 = new ModelInstance(model, "container_001");
        allModelInstances.add(container_001);

        crate_001 = new ModelInstance(model, "crate_001");
        allModelInstances.add(crate_001);
        crate_002 = new ModelInstance(model, "crate_002");
        allModelInstances.add(crate_002);
        crate_003 = new ModelInstance(model, "crate_003");
        allModelInstances.add(crate_003);
        crate_004 = new ModelInstance(model, "crate_004");
        allModelInstances.add(crate_004);
        crate_005 = new ModelInstance(model, "crate_005");
        allModelInstances.add(crate_005);

        grass_area = new ModelInstance(model, "grass_area");
        allModelInstances.add(grass_area);

        gravel_area = new ModelInstance(model, "gravel_area");
        allModelInstances.add(gravel_area);

        rock_wall = new ModelInstance(model, "rock_wall");
        allModelInstances.add(rock_wall);

        water = new ModelInstance(model, "water");
        allModelInstances.add(water);

        player = new ModelInstance(model, "character");
        allModelInstances.add(player);


        player.transform.setTranslation(8f,0f,-6f); // Test character movement on the map

        loading = false;        

    }

    @Override
    public void render() {

        if(loading && assets.update()){
            doneLoading();
        }

        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);


        camController.update();

        //Gdx.app.log("Camera Position: ", cam.position.toString());

        modelBatch.begin(cam);
        modelBatch.render(allModelInstances, environment);
        modelBatch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        //fpsLogger.log();

    }

    @Override
    public void dispose() {

        modelBatch.dispose();
        allModelInstances.clear();
        assets.dispose();

    }

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

        vp.update(width, height);
        stageVp.update(width, height);

    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

}

2 个答案:

答案 0 :(得分:1)

最终纠正此问题的是从libgdx repository on github下载默认的uiskin文件。我从头开始构建它们时显然错过了什么?我在回购中使用的文件是:

  • uiskin.json
  • uiskin.atlas
  • 为Default.png
  • uiskin.png

答案 1 :(得分:0)

我认为加载uiskin文件需要很长时间。尝试代替skin = new Skin(Gdx.files.internal("uiskin.json"));之类的东西:

AssetManager manager = new AssetManager(); manager.load("uiskin.json", Skin.class); manager.finishLoading(); skin = manager.get("uiskin.json", Skin.class);

在完成游戏之前,不要忘记丢弃AssetManager。