【问题标题】:Java string returning null after definition定义后返回 null 的 Java 字符串
【发布时间】:2016-04-22 01:38:09
【问题描述】:

正如标题所说,我正在使用 java 并且在运行时返回 null 的字符串出现问题,即使我之前设置了它。我正在使用 LibGDX,它在这一点上并没有真正复杂化,但这似乎很简单。否则我只是累了。

代码如下:

        package com.jett.game;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.utils.Array;

public class GameScreen implements Screen{

    public OrthographicCamera cam;
    public Player player;

    public SpriteBatch mainBatch;

    public Array<Sprite> fireSprites;
    public Sprite bartableSprite;
    public Texture choiceBanner;
    public Texture blackboardTex;

    public boolean paymentChoice;

    public FreeTypeFontGenerator fontGenerator;
    public FreeTypeFontParameter fontParameter;
    public BitmapFont font;

    public int shopMoney = 3000;
    public float dayTime;
    public float fireSpriteIndex;

    public Customer customer;

    public ArrayList<Favor> favors;
    // THERE SHOULD ONLY BE THREE FAVORS AT ONCE. IT WONT BREAK OTHERWISE, BUT IT WILL BECOME DIFFICULT

    public GameScreen(){
        favors = new ArrayList<Favor>();
        favors.add(new Favor(1,"Get 50 logs of wood."));
        favors.add(new Favor(2, "Find a hammer"));
        blackboardTex = new Texture(Gdx.files.internal("Blackboard.png"));
        fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("Coolville.ttf"));
        fontParameter = new FreeTypeFontParameter();
        fontParameter.size = 9;
        font = fontGenerator.generateFont(fontParameter);
        mainBatch = new SpriteBatch();
        player = new Player(-50,0);
        cam = new OrthographicCamera(16*10,9*10);
        player.batcher.setProjectionMatrix(cam.combined);
        choiceBanner = new Texture(Gdx.files.internal("ChoiceBanner.png"));
        bartableSprite = new Sprite(new Texture(Gdx.files.internal("Bartable.png")));
        fireSprites = new Array<Sprite>();
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire1.png"))));
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire2.png"))));
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire3.png"))));
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire4.png"))));
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire5.png"))));
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire6.png"))));
        fireSprites.add(new Sprite(new Texture(Gdx.files.internal("Fire7.png"))));
        mainBatch.setProjectionMatrix(cam.combined);
        customer = new DwarfScythe(80, 0);
        customer.batcher.setProjectionMatrix(cam.combined);
    }

    @Override
    public void render(float delta) {
        Gdx.app.log("blackboard favors: ", ""+favors.size());
        if(customer != null){
            if(customer.CURRENT_STATE == customer.TALKING){
                paymentChoice = true;
            }
            customer.delta = delta;
            if(customer.hasPayed){
                paymentChoice = false;
                customer = null;
            }
        }
        Gdx.graphics.getGL20().glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.graphics.getGL20().glClearColor(0.05f, 0.05f, 0.05f, 1);
        player.render();
        player.logic(delta);
        fireSpriteIndex += delta*5;
        mainBatch.begin();
        // Background
        mainBatch.draw(bartableSprite, -48, 0);
        mainBatch.draw(fireSprites.get((int)fireSpriteIndex), 0, 0);
        mainBatch.end();
        // People
        if(customer != null){
            customer.render();
            customer.logic();
        }
        // UI
        mainBatch.begin();
        mainBatch.draw(blackboardTex, -blackboardTex.getWidth()/2, -blackboardTex.getHeight()/2);
        Gdx.app.log("favor 1 name: ",favors.get(1).name); // Returns: favor 1 name: null
        //font.draw(mainBatch, favors.get(1).name, -blackboardTex.getWidth()/2+5, blackboardTex.getHeight()/2-5);
        font.draw(mainBatch, "Gold: " + shopMoney, -80, 44);
        if(paymentChoice){
            mainBatch.draw(choiceBanner, -16,-16);
            if(Gdx.input.isKeyJustPressed(Keys.G)){
                // Gold
                if(customer.isCheap){
                    shopMoney += customer.money/4;
                }   
                else{
                    shopMoney += customer.money;
                }
                customer.hasPayed = true;
            }
            if(Gdx.input.isKeyJustPressed(Keys.H)){
                // Help
            }
        }
        mainBatch.end();
        if(fireSpriteIndex >= 6.5f){
            fireSpriteIndex = 0;
        }
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

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

    }

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

    }

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

    }

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

    }

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

    }
}

【问题讨论】:

  • 什么字符串?请发布一个最小的例子。
  • 喜爱类包含字符串名称,即字符串。

标签: java string null libgdx


【解决方案1】:

问题已解决。看来我在开发 Favor 类的过程中犯了一个错误。很抱歉变得主观,但我注意到我只是通过添加这些类来过度复杂化它,而我本来可以简单地添加一个字符串的 ArrayList。我就是这样做的,问题就解决了。

【讨论】:

    猜你喜欢
    • 2015-02-12
    • 2014-07-28
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多