2012-10-20 2 views
1

Slick2D로 놀고 있는데 커서 키로 간단한 사각형을 움직일 때 문제가 있습니다.Slick2D 커서 키로 오브젝트를 부드럽게 움직입니다.

코드 기능이 현명합니다. 그러나 커서 키 중 하나를 누른 상태로 유지하면 화면을 보면서 움직이는 원내에 약간의 괴롭힘이 생깁니다.

아무도 코드 개선을 제안하여 사각형을 부드럽게 움직일 수 있습니까?

public class SlickGame extends BasicGame { 
// ================================================================================== 
// Fields 
// ================================================================================== 
private Rectangle mPlayer; 

// ================================================================================== 
// Constructor 
// ================================================================================== 
public SlickGame() { 
    super("SlickGame"); 
} 

// =========================================================== 
// Methods for/from SuperClass/Interfaces 
// =========================================================== 
@Override 
public void init(GameContainer pGameContainer) throws SlickException { 
    // create the player centered on the screen 
    mPlayer = new Rectangle(pGameContainer.getWidth()/2 - 20, pGameContainer.getHeight()/2 - 20, 40, 40); 
} 

@Override 
public void update(GameContainer pGameContainer, int pDelta) throws SlickException { 
    Input input = pGameContainer.getInput(); 
    int speed = 200; 
    float distance = speed * ((float)pDelta/1000); 

    if (input.isKeyDown(Input.KEY_LEFT)) { 
     mPlayer.setX(mPlayer.getX() - distance); 
    } 
    if (input.isKeyDown(Input.KEY_RIGHT)) { 
     mPlayer.setX(mPlayer.getX() + distance); 
    } 
    if (input.isKeyDown(Input.KEY_UP)) { 
     mPlayer.setY(mPlayer.getY() - distance); 
    } 
    if (input.isKeyDown(Input.KEY_DOWN)) { 
     mPlayer.setY(mPlayer.getY() + distance); 
    } 
} 

@Override 
public void render(GameContainer pGameContainer, Graphics pGraphics) throws SlickException { 
    pGraphics.fill(mPlayer); 
} 

// ================================================================================== 
// Methods 
// ================================================================================== 
public static void main(String[] args) { 
    try { 
     // create the game's container app 
     AppGameContainer container = new AppGameContainer(new SlickGame()); 
     // adjust the resolution and disable fullscreen 
     container.setDisplayMode(800, 600, false); 
     // specify desired FPS 
     container.setTargetFrameRate(60); 
     // start the game 
     container.start(); 
    } 
    catch (SlickException e) { 
     e.printStackTrace(); 
    } 
} 
} 
+1

조금 더 시도한 후, 나는 그 줄을 제거하는 것에 주목한다. container.setTargetFrameRate (60); 개선을 제공합니다. 이것이 올바른 방법일까요? 다른 아이디어? – user1761400

+0

글쎄, 나는 이해하지 못했다. 문제는 운동 자체가 뒤떨어 지거나 이미지가 왜곡되어 보인다는 것이다. 문제가 후자라면,'container.setTargetFrameRate (60); 만 제거하면이 문제가 해결되지 않고'container.setVSync (true);와 함께 해결됩니다. – dhblah

답변

0

감사합니다, naughty_hacker : 여기

은 내가 사용하는 테스트 코드입니다. container.setVSync(true);을 추가하면 트릭을 만들었습니다.

관련 문제