2013-03-21 77 views
0

我正在寫一個libgdx遊戲,但似乎我無法顯示我的飛濺屏幕,它只顯示黑屏。有誰知道這是怎麼來的?libgdx我的飛濺屏幕沒有顯示

我沒有得到任何運行時錯誤,那隻能說明一個blackscreen,即使我設置glClearColor到其他顏色那麼黑

package mygame; 



import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class SplashScreen implements Screen { 

Texture splashTexture; 
Sprite splashSprite; 
SpriteBatch batch; 
MyGame game; 

public SplashScreen(MyGame game) { 
    this.game = game; 
} 

@Override 
public void render(float delta) { 
    Gdx.gl.glClearColor(0, 0, 0, 1); 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

    batch.begin(); 
    splashSprite.draw(batch); 
    batch.end(); 
} 

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

} 

@Override 
public void show() { 
    splashTexture = new Texture("mygame/splash.png"); 
    splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 

    splashSprite = new Sprite(splashTexture); 
    //splashSprite.setColor(1, 1, 1, 0); 
    splashSprite.setX(Gdx.graphics.getWidth()/2 - (splashSprite.getWidth()/2)); 
    splashSprite.setY(Gdx.graphics.getHeight()/2 - (splashSprite.getHeight()/2)); 

    batch = new SpriteBatch(); 
} 

@Override 
public void hide() { 

} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

@Override 
public void dispose() { 

} 

} 

回答

1

我想你的代碼(見下文),而且運作良好夠了,所以問題可能出在MyGame類。例如,你是否覆蓋Game.render()然後不打電話super.render()

package hacks; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.backends.lwjgl.LwjglApplication; 
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; 
import com.badlogic.gdx.graphics.GL10; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.Texture.TextureFilter; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class MyGame extends com.badlogic.gdx.Game { 

    @Override 
    public void create() { 
     setScreen(new SplashScreen(this)); 
    } 

    public static void main(String[] args) { 
     LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); 

     config.title = MyGame.class.getName(); 
     config.width = 800; 
     config.height = 480; 
     config.fullscreen = false; 
     config.useGL20 = true; 
     config.useCPUSynch = true; 
     config.forceExit = true; 
     config.vSyncEnabled = true; 

     new LwjglApplication(new MyGame(), config); 
    } 
} 

class SplashScreen implements Screen { 

    Texture splashTexture; 
    Sprite splashSprite; 
    SpriteBatch batch; 
    MyGame game; 

    public SplashScreen(MyGame game) { 
     this.game = game; 
    } 

    @Override 
    public void render(float delta) { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

     batch.begin(); 
     splashSprite.draw(batch); 
     batch.end(); 
    } 

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

    @Override 
    public void show() { 
     splashTexture = new Texture("assets/data/libgdx.png"); 
     splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 

     splashSprite = new Sprite(splashTexture); 
     // splashSprite.setColor(1, 1, 1, 0); 
     splashSprite.setX(Gdx.graphics.getWidth()/2 - (splashSprite.getWidth()/2)); 
     splashSprite.setY(Gdx.graphics.getHeight()/2 - (splashSprite.getHeight()/2)); 

     batch = new SpriteBatch(); 
    } 

    @Override 
    public void hide() { 

    } 

    @Override 
    public void pause() { 

    } 

    @Override 
    public void resume() { 

    } 

    @Override 
    public void dispose() { 
    } 
} 
0

這是我的遊戲類看起來像:

package mygame; 

import com.badlogic.gdx.Game; 

public class MyGame extends Game { 

@Override 
public void create() { 

    setScreen(new SplashScreen(this)); 
} 

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

} 

@Override 
public void render() { 
    // 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 dispose() { 
    // TODO Auto-generated method stub 

} 

} 
+1

我發現錯誤,確實是渲染方法 – 2013-03-22 10:19:12

0

這是你的遊戲類應該什麼樣子......你需要調用超級功能。

package com.sample; 

import com.badlogic.gdx.Game; 

public class Sample extends Game{ 

    @Override 
    public void dispose() { 
     // TODO Auto-generated method stub 
     super.dispose(); 
    } 

    @Override 
    public void pause() { 
     // TODO Auto-generated method stub 
     super.pause(); 
    } 

    @Override 
    public void resume() { 
     // TODO Auto-generated method stub 
     super.resume(); 
    } 

    @Override 
    public void render() { 
     // TODO Auto-generated method stub 
     super.render(); 
    } 

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

    @Override 
    public void create() { 
     // TODO Auto-generated method stub 
     setScreen(new AnotherScreen(this)); 
    } 

}