2017-11-21 24 views
0

그래서 나는 플레이어 내 게임에 내 코드를 리팩토링의 과정에있어 플레이어 클래스에서 그릴을 시도 할 때 이동 플레이어 클래스 시작되지 않습니다 난플레이어

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Input; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class Player 
{ 
    protected String name; 
    protected float health, maxHealth; 
    protected Texture texture; 
    protected int xPos, yPos; 

    public Player(String name, float maxHealth, Texture texture) { 
     this(name, maxHealth, texture, 0, 0); 
    } 

    public Player(String name, float maxHealth, Texture texture, int xPos, int yPos) { 
     this.name = name; 
     this.health = this.maxHealth = maxHealth; 
     this.texture = texture; 
     this.xPos = xPos; 
     this.yPos = yPos; 
    } 

    public Texture getTexture() { 
     return this.texture; 
    } 

    public int getX() { 
     return xPos; 
    } 
    public int getY() { 
     return yPos; 
    } 

    public String getName() { 
     return name; 
    } 

    public void draw(SpriteBatch spriteBatch) { 
     spriteBatch.begin(); 
     spriteBatch.draw(texture, xPos, yPos, 0.5f, 0.5f, 0, 0, 
       texture.getWidth(), texture.getHeight(), false, false); 
     spriteBatch.end(); 
    } 

    public void update(float delta) { 
     processMovement(delta); 
    } 

    public void processMovement(float delta) { 
     if(Gdx.input.isKeyPressed(Input.Keys.A)) { 
      xPos -= 50 * delta; 
     } 
     if(Gdx.input.isKeyPressed(Input.Keys.D)) { 
      xPos += 50 * delta; 
     } 
    } 
} 

을 정사영 카메라를 사용하여, 다음에 할 일을 할 때 아직 지형을 추가하지 않았지만, 플레이어가 항상 중앙에 머물면서 플레이어가 그릴 때 지형을 돌아 다니게하고 싶습니다. 다음과 같이

내가 카메라를 만들어 내 플레이어를 그리기 위해이 코드는 다음과 같습니다

import com.badlogic.gdx.Gdx; 
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.SpriteBatch; 

public class GameState implements Screen 
{ 
    private PixelGame parent; 

    private OrthographicCamera camera; 
    private Player player; 
    private Texture playerTexture; 
    private SpriteBatch spriteBatch; 

    public GameState(PixelGame parent) { 
     this.parent = parent; 
     this.playerTexture = new Texture(Gdx.files.internal("player/sprite.png")); 
     this.player = new Player("Me", 20, playerTexture); 
     this.spriteBatch = new SpriteBatch(); 
    } 

    @Override 
    public void render(float delta) { 
     camera.update(); 
     spriteBatch.setProjectionMatrix(camera.combined); 

     Gdx.gl.glClearColor(0, 0, 0.2f, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     player.draw(spriteBatch); 
     player.update(delta); 
    } 

    @Override 
    public void show() { 
    } 

    @Override 
    public void dispose() { 
    } 

    @Override 
    public void resize(int width, int height) { 
     float ratio = (float)width/(float)height; 
     camera = new OrthographicCamera(2f * ratio, 2f); 
    } 

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public void hide() { 
    } 
} 

플레이어 스프라이트가 이동하지 않는 것,하지만 난 뭔가> 75 플레이어 스프라이트에 값을 변경할 때 스크린을 가로 질러 아무 것도없는 비즈니스처럼 펼쳐진다.

답변

2

xPosyPos은 int입니다. 나는 그것이 문제라고 말하고 싶다. processMovement() 메서드는 초당 100 번처럼 호출되며 50 * delta은 1보다 작기 때문에 int 변수에 값을 저장해야하므로 0으로 반올림됩니다. xPosyPos을 부동으로 변경해보십시오.

그리고 도움이되지 않는 경우 (코드를 시험하지 않고도 확신 할 수없는 경우) 일부 디버깅을 수행하십시오. 중단 점을 넣으십시오. processMovement()이 전혀 호출되지 않았는지 확인한 다음 델타 변수의 값이 무엇인지 확인하십시오. 계산 방법.