LibGDX TextureRegion NullPointerException

时间:2017-09-13 11:13:29

标签: java java-8 libgdx

我已经开始使用Lib GDX AssetManager而不是" normal"导入纹理的方法。但是,在试验它时,我很快遇到了NullPointerException错误。这是代码:

    assets = Generals.getAssetManager();
    assets.load("Characters/c.pack", TextureAtlas.class);
    assets.finishLoading();
    TextureAtlas atlas = assets.get("Characters/c.pack", TextureAtlas.class);
    atlas.findRegion("PriestOverworld3");
    Array<TextureRegion> frames = new Array<TextureRegion>();
    Texture testtex = new Texture("Characters/c.png");
    TextureRegion test = new TextureRegion(testtex);
    for (int i = 0; i < 3; i++) {
        System.out.println(i);
        frames.add(new TextureRegion(getTexture(), i * 16, 0, 16, 16));
    }
    playerStasis = new Animation<>(0.2f, frames);

我在

行获得了NullPointerException
frames.add(new TextureRegion(getTexture(), i * 16, 0, 16, 16));

错误:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:82)
    at com.badlogic.gdx.graphics.g2d.TextureRegion.<init>(TextureRegion.java:53)
    at Project.sprites.Player.<init>(Player.java:55)

TextureRegion:

// TextureRegion.java:53
setRegion(x, y, width, height);
...
// TextureRegion.java:82
float invTexWidth = 1f / texture.getWidth();

1 个答案:

答案 0 :(得分:2)

IT看起来像是在找到atlasRegion而不是将其设置为变量。

尝试使用此

var app = {
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);        
    },
    onDeviceReady: function () {
    app.receivedEvent('deviceready');
    // Here, we redirect to the web site.
    var targetUrl = "http://example.org/app";
    var bkpLink = document.getElementById("bkpLink");
    bkpLink.setAttribute("href", targetUrl);
    bkpLink.text = targetUrl;
    window.location.replace(targetUrl);
    //I want to call a alert when button click      
    document.getElementsByClassName('btn')[0].addEventListener('click', (function(i) {
    return function() {
        alert("okkkkkkkkkk");
    };
    })(i), false);  

    },
    // Note: This code is taken from the Cordova CLI template.
    receivedEvent: function (id) {
    }

};
app.initialize();