Java字符串在定义后返回null

时间:2016-04-22 01:38:10

标签: java string null libgdx

正如标题所说,我使用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

    }
}

1 个答案:

答案 0 :(得分:0)

问题已得到解决。看来我在开发Favor类时出错了。很抱歉变得主观,但我注意到,当我只是添加一个字符串ArrayList时,我只是通过添加这些类过于复杂。这就是我所做的,问题就解决了。