我用于播放器的纹理区域不会绘制到我的屏幕
这里是播放器类,其中定义了播放器,纹理区域附加到框2d主体
public Player (World world, PlayScreen screen){
super(screen.getAtlas().findRegion("player"));
this.world= world;
currentState = State.STANDING;
previousState = State.STANDING;
stateTimer=0;
runningRight=true;
Array<TextureRegion> frames = new Array<TextureRegion>();
for(int i=5; i<9; i++){
frames.add(new TextureRegion(getTexture(),i*50,0,50,50));
}
playerRun= new Animation(0.1f , frames);
defineplayer();
playerStand = new TextureRegion(getTexture(),0,0,50,50);
setBounds(0,0,50,50);
setRegion(playerStand);
}
public void update(float dt){
setPosition(b2dbody.getPosition().x-getWidth()/2,b2dbody.getPosition ().y-getHeight()/2);
setRegion(getFrame(dt));
}
public TextureRegion getFrame(float dt){
currentState = getState();
TextureRegion region;
switch(currentState){
case RUNNING:
region=(TextureRegion) playerRun.getKeyFrame(stateTimer, true);
break;
case STANDING:
default:
region = playerStand;
break;
}
if((b2dbody.getLinearVelocity().x<0 || !runningRight) && !region.isFlipX() ){
region.flip(true, false);
runningRight=false;
}
if((b2dbody.getLinearVelocity().x>0 || runningRight) && region.isFlipX()){
region.flip(true, false);
runningRight=true;
}
stateTimer = currentState == previousState ? stateTimer +dt : 0;
previousState = currentState;
return region;
}
public State getState(){
if(b2dbody.getLinearVelocity().x!=0){
return State.RUNNING;
}
else{
return State.STANDING;
}
}
public void defineplayer(){
BodyDef bdef = new BodyDef();
bdef.position.set(32,32);
bdef.type = BodyDef.BodyType.DynamicBody;
b2dbody = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(5);
fdef.shape=shape;
b2dbody.createFixture(fdef);
}
}
这是我的播放器正在显示和移动的播放屏幕
public class PlayScreen implements Screen {
private final FinalGame game;
Texture texture;
private OrthographicCamera gamecam;
private Viewport gameport;
private Hud hud;
private TmxMapLoader loader;
private TiledMap map;
private OrthogonalTiledMapRenderer render;
private Player player;
private TextureAtlas atlas;
private Music music;
int Vx;
int Vy;
//Box 2D
private World world;
private Box2DDebugRenderer b2dr;
public PlayScreen(FinalGame game , String name){
atlas = new TextureAtlas("Player_And_Enemies.pack");
hud=new Hud(game.sb, name);
this.game=game;
gamecam=new OrthographicCamera();
gameport=new StretchViewport(FinalGame.width ,FinalGame.height,gamecam);
loader = new TmxMapLoader();
map=loader.load("level1.tmx");
render=new OrthogonalTiledMapRenderer(map);
gamecam.position.set(gameport.getWorldWidth()/2,gameport.getWorldHeight()/2,0);
world = new World(new Vector2(0,0),true);
b2dr = new Box2DDebugRenderer();
player=new Player(world,this);
music = FinalGame.manager.get("Horror field.mp3", Music.class);
music.setLooping(true);
music.setVolume(0.1f);
music.play();
}
@Override
public void show() {
// TODO Auto-generated method stub
}
public TextureAtlas getAtlas(){
return atlas;
}
public void handleinput(float dt){
Vy=0;
Vx=0;
if(Gdx.input.isKeyPressed(Input.Keys.W)){
Vy=40;
}
if(Gdx.input.isKeyPressed(Input.Keys.S)){
Vy=-40;
}
if(Gdx.input.isKeyPressed(Input.Keys.D)){
Vx=40;
}
if(Gdx.input.isKeyPressed(Input.Keys.A)){
Vx=-40;
}
player.b2dbody.setLinearVelocity(new Vector2(Vx,Vy));
}
public void update(float dt){
handleinput(dt);
world.step(1/60f,6,2);
player.update(dt);
gamecam.update();
render.setView(gamecam);
}
@Override
public void render(float delta) {
//renders the play screen map
update(delta);
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
render.render();
game.sb.setProjectionMatrix(gamecam.combined);
game.sb.begin();
player.draw(game.sb);
game.sb.end();
game.sb.setProjectionMatrix(hud.stage.getCamera().combined);
//draws all the hud information to the screen
Hud.stage.draw();
}
@Override
public void resize(int width, int height) {
gameport.update(width, height);
}
@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
}
}