在libgdx框架中看不到背景图像?

时间:2018-07-15 17:25:53

标签: java libgdx

这是游戏的背景图像Background image。但是由于某种原因,该图像失去了焦点。这是游戏Game的屏幕截图。我正在将纹理图像加载到MyMainGame中创建方法中的类。

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.manager.Content;
import com.mygdx.manager.GameInputProcessor;

import com.mygdx.manager.GameStateManager;
import com.mygdx.manager.MyInput;


import javafx.scene.layout.Background;

import static com.mygdx.manager.Box2DVariables.PPM;

public class MyMainGame implements ApplicationListener {
    SpriteBatch batch;
    Texture img;
    public static final int Width=320;
    public static final int Height=240;
    public static final int SCALE=2;

    public static final float STEP= 1/60f;  //60 frames per second

    private float accumulator;
    private SpriteBatch sb;
    protected OrthographicCamera cam;
    protected OrthographicCamera hud;
    private Texture tex;
    private Background[] backgrounds;

    private GameStateManager gsm;

    public SpriteBatch getSb() {
        return sb;
    }

    public OrthographicCamera getCam() {
        return cam;
    }

    public OrthographicCamera getHud() {
        return hud;
    }
     public  static Content con;



    @Override
    public void create () {
        Gdx.input.setInputProcessor(new GameInputProcessor());
        con=new Content();
        con.loadTexture("images//sprite.jpg","sprite");
        con.loadTexture("images//bck.png","background");

        gsm=new GameStateManager(this);

        sb=new SpriteBatch();
        cam=new OrthographicCamera(160,120);


        cam.setToOrtho(false,Width/2,Height/2);
        hud=new OrthographicCamera();
        hud.setToOrtho(false,Width/2,Height/2);










    }

    @Override
    public void render () {
        cam.update();
        hud.update();

        accumulator +=Gdx.graphics.getDeltaTime();
        while(accumulator>=STEP){
            accumulator-=STEP;
            gsm.update(STEP);
            gsm.render();
            MyInput.update();
        }



    }
    public void resize(int width,int height){
        sb.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
     }

    public void dispose(){
            con.disposeTexture("sprite");
            con.disposeTexture("background");
    }
    public void pause(){

    }

    public void resume(){
        gsm.setState(GameStateManager.PLAY);
    }
}

在Play类中,我正在Constructor中检索纹理,并在类的render方法中使用SpriteBatch对其进行绘制。

public class Play extends GameState{

  //  private BitmapFont font=new BitmapFont();
    private World  world;
    private Body playerBody;
    private int viewportWidth= 10 * 32 ;
    int viewportHeight= 8 * 32 ;


    private Box2DDebugRenderer b2d;
    private OrthographicCamera B2DCAM;
    private TiledMap tileMap;
    private OrthogonalTiledMapRenderer orthomap;
    private MyContactListener cl;
    private float tileSize;
    private Background[] backgrounds;
    private Texture back;



    //file name
    private final String LEVEL_1 ="maps/tilemap1.tmx";

    public Play(GameStateManager gsm) {
        super(gsm);


        //setup box2d
        world=new World(new Vector2(0,-9.8f),true);
        cl=new MyContactListener();
        world.setContactListener(cl);
        // sb=new SpriteBatch();
        //cam=new OrthographicCamera();

        b2d=new Box2DDebugRenderer();

        //create Player
        createPlayer();
        //create Tiles
        createTiles();
        orthomap = new OrthogonalTiledMapRenderer(tileMap,1/32f);
         sb=new SpriteBatch();


        //setup Box2D Cam
        B2DCAM=new OrthographicCamera();
        B2DCAM.setToOrtho(false, MyMainGame.Width/PPM,MyMainGame.Height/PPM);
        hud=new OrthographicCamera();
        cam=new OrthographicCamera();
        cam.setToOrtho(false,10,7);


         back = MyMainGame.con.getTexture("background");











      /* //TODO - remove me
        File file = new File(LEVEL_1);
        if(file.exists())
            System.out.println("file exists");*/


    }


    @Override
    public void init() {

    }

    @Override
    public void update(float dt) {
        handleInput();
            world.step(dt,6,2);
          Texture sprite = MyMainGame.con.getTexture("sprite");
        back = MyMainGame.con.getTexture("background");


    }

    @Override
    public void draw() {

    }

    @Override
    public void render() {

        //clear screen

        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

        cam.position.set(playerBody.getPosition().x * PPM +MyMainGame.Width / 4,MyMainGame.Height / 2,0);


          System.out.println("Camera position width"+playerBody.getPosition().x * PPM +MyMainGame.Width / 4+"Camera position height:"+MyMainGame.Height / 2);
        cam.setToOrtho(false,10,7);

       cam.update();
      hud.setToOrtho(false,10,7);
       hud.update();
         cam.translate(0,0);

        if (sb == null) {
System.out.println("Sb is null");
        }
            sb.setProjectionMatrix(cam.combined);
            sb.begin();
            sb.draw(back,160,120);
            sb.end();





        orthomap.setView(cam);
        orthomap.render();
        b2d.render(world,B2DCAM.combined);




    }

    @Override
    public void handleInput() {
     if(MyInput.isPressed((MyInput.SPACE))){
         System.out.println("space pressed");
         if(cl.isPlayerOnGround())
             System.out.println(cl.isPlayerOnGround());
             playerBody.applyForceToCenter(0,200,true);
         }
     }


    @Override
    public void dispose() {
        world.dispose();
        b2d.dispose();
        tileMap.dispose();
        orthomap.dispose();
    }

    private void createPlayer(){
        BodyDef bodydef=new BodyDef();
        FixtureDef fixdef=new FixtureDef();
        PolygonShape shape=new PolygonShape();


        //create player
        bodydef.position.set(160/PPM,200/PPM);
        bodydef.type= BodyDef.BodyType.DynamicBody;
        playerBody=world.createBody(bodydef);

        shape.setAsBox(5/PPM,5/PPM);
        fixdef.shape=shape;
        fixdef.filter.categoryBits= BIT_PLAYER;
        fixdef.filter.maskBits=Box2DVariables.BIT_BLUE;
        playerBody.createFixture(fixdef).setUserData("PLAYER");


        //create foot sensor
        shape.setAsBox(2/PPM,2/PPM,new Vector2(0,-5/PPM),0);
        fixdef.shape=shape;
        fixdef.filter.categoryBits= BIT_PLAYER;
        fixdef.filter.maskBits=Box2DVariables.BIT_BLUE;
        fixdef.isSensor=true;
        playerBody.createFixture(fixdef).setUserData("FOOT");
    }
    private void createTiles(){
//load tile map
        tileMap = new TmxMapLoader().load(LEVEL_1);
        //orthomap = new OrthogonalTiledMapRenderer(tileMap,1);


        tileSize=(int)tileMap.getProperties().get("tilewidth", Integer.class);
        System.out.println("Tile Size " +tileSize);

        TiledMapTileLayer layer;

        layer=(TiledMapTileLayer)tileMap.getLayers().get("Blue");
            createLayer(layer,Box2DVariables.BIT_BLUE);
        layer=(TiledMapTileLayer)tileMap.getLayers().get("Green");
        createLayer(layer,Box2DVariables.BIT_GREEN);
        layer=(TiledMapTileLayer)tileMap.getLayers().get("Red");
        createLayer(layer,Box2DVariables.BIT_RED);
        System.out.println("Layer Height " +layer.getHeight());
        System.out.println("Layer Width " +layer.getWidth());
    }
    private void createLayer(TiledMapTileLayer layer,short bits){
        BodyDef bodydef=new BodyDef();
        FixtureDef fixdef=new FixtureDef();

//go through cells in layer
        for(int row=0;row<layer.getHeight();row++){
            for(int col=0;col<layer.getWidth();col++){
                //get cells
                TiledMapTileLayer.Cell cell=layer.getCell(col,row);

                //check if cell exists
                if(cell==null) continue;
                if(cell.getTile()==null) continue;

                //create body and fixture from cell
                bodydef.type= BodyDef.BodyType.StaticBody;
                bodydef.position.set((col+0.5f)*tileSize/PPM,(row+0.5f)*tileSize/PPM);
                ChainShape cs=new ChainShape();
                Vector2[] v=new Vector2[3];
                //bottom left
                v[0]=new Vector2(-tileSize/2/PPM,-tileSize/2/PPM);
                //top left
                v[1]=new Vector2(-tileSize/2/PPM,tileSize/2/PPM);
                //top right corner
                v[2]=new Vector2(tileSize/2/PPM,tileSize/2/PPM);


                cs.createChain(v);
                fixdef.friction=0;
                fixdef.shape =cs;
                fixdef.filter.categoryBits=Box2DVariables.BIT_BLUE;
                fixdef.filter.maskBits=BIT_PLAYER;
                fixdef.isSensor=false;
                world.createBody(bodydef).createFixture(fixdef);

            }
        }
    }
}

我应该如何设置背景图像的焦点?

0 个答案:

没有答案