2013-01-05 3 views
0

키보드 화살표 입력 (위쪽, 아래쪽, 왼쪽, 오른쪽)으로 움직이는 2D지도에서 플레이어의 오버 헤드보기가있는 간단한 2D Java 게임을 코딩했습니다. Play 클래스 내 게임 코드를 모두 가지고있는 게임은 몇 가지 방법으로 구성되어 있습니다. 주요 방법은 실제 플레이어 및지도와 키보드 입력을 처리하고 플레이어를 이동하고 이미지를 업데이트하는 모든 것을 화면에 그려주는 렌더링입니다. 수, 나는 그것의 위치를 ​​업데이트 할 내 업데이트 클래스 내부를 넣어 rectTwo를 업데이트해야하지만이 작업을 수행하는 코드를 작성하는 데 문제가있는 사람자바 2D 게임, 사각형 업데이트

 imports are here 
    public class Play extends BasicGameState{ 
    Animation bucky, movingUp, movingDown, movingLeft, movingRight; 
    Image worldMap; 
    int[] duration = {200, 200};//how long frame stays up for 
    float buckyPositionX = 0; 
    float buckyPositionY = 0; 
    float shiftX = buckyPositionX + 320;//keeps user in the middle of the screem 
    float shiftY = buckyPositionY + 160;//the numbers are half of the screen size 
    java.awt.geom.Rectangle2D.Float rectOne = new Rectangle2D.Float(shiftX, shiftY,90,90); 
    java.awt.geom.Rectangle2D.Float rectTwo = new Rectangle2D.Float(500 + buckyPositionX, 330 + buckyPositionY, 210, 150); 
    public Play(int state){ 
    } 
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{ 
      worldMap = new Image("res/world.png"); 
      Image[] walkUp = {new Image("res/b.png"), new Image("res/b.png")}; //these are the images to be used in the "walkUp" animation 
      Image[] walkDown = {new Image("res/f.png"), new Image("res/f.png")}; 
      Image[] walkLeft = {new Image("res/l.png"), new Image("res/l.png")}; 
      Image[] walkRight = {new Image("res/r.png"), new Image("res/r.png")}; 
    movingUp = new Animation(walkUp, duration, false); 
    movingDown = new Animation(walkDown, duration, false); 
    movingLeft = new Animation(walkLeft, duration, false); 
    movingRight = new Animation(walkRight, duration, false); 
    bucky = movingDown;//facing screen initially on startup 
    } 
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{ 
    worldMap.draw(buckyPositionX, buckyPositionY);//position 0,0 
    bucky.draw(shiftX, shiftY);//makes him appear at center of map 
    g.fillRect((float)rectOne.getX(), (float)rectOne.getY(), (float)rectOne.getWidth(), (float)rectOne.getHeight()); 
    g.fillRect((float)rectTwo.getX(), (float)rectTwo.getY(), (float)rectTwo.getWidth(), (float)rectTwo.getHeight()); 
} 
    public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{ 
    Input input = gc.getInput(); 
    //up 
    if(input.isKeyDown(Input.KEY_UP)){ 
     bucky = movingUp;//changes the image to his back 
     buckyPositionY += 2;;//increase the Y coordinates of bucky (move him up) 
     if(buckyPositionY>162){//if I reach the top 
      buckyPositionY -= 2;//stops any further movement in that direction 
     } 
    } 
    //down 
    if(input.isKeyDown(Input.KEY_DOWN)){ 
     bucky = movingDown; 
     buckyPositionY -= 2; 
     if(buckyPositionY<-550){ 
      buckyPositionY += 2;//basically change the direction if + make - 
    }} 
    //left 
    if(input.isKeyDown(Input.KEY_LEFT)){ 
     bucky = movingLeft; 
     buckyPositionX += 2; 
     if(buckyPositionX>324){ 
      buckyPositionX -= 2;//delta * .1f 
    }} 
    //right 
    if(input.isKeyDown(Input.KEY_RIGHT)){ 
     bucky = movingRight; 
     buckyPositionX -= 2; 
     if(buckyPositionX<-776){ 
      buckyPositionX += 2; 
    }}} 
    public int getID(){ 
     return 1; 
    }} 

: 여기

내 플레이 클래스 내 코드입니다 이걸 좀 도와 주실 래요?

+0

질문을 지정하십시오. 수정 기능에 rectTwo를 가져 오는 것은 (당신이 클래스가 아닌 함수를 의미한다고 가정 할 때) 업데이트 기능에 rectTwo를 쓰는 것으로 끝난다 (그것은 클래스의 일부이다). –

답변

0

업데이트 방법을 의미합니까?

어떤 종류의 업데이트를 원하십니까? 그의 위치? 당신이 당신의 업데이트 방법에 그런 일을 시도했다 :

공공 무효 업데이트 (GameContainer의 GC는, StateBasedGame SBG, INT 델타가) 가 SlickException을 던졌습니다 {

Input input = gc.getInput(); 
//up 
if(input.isKeyDown(Input.KEY_UP)){ 
    rectTwo.setRect(float x, float y, float w, float h) ; 
    .... 

y는 될 수 rectTwo.getY() + 2는 따라 ur 자신의 규칙

+0

고맙습니다. 제 코드를 수정하는 데 도움을주었습니다. –

+0

내가 도와 줄 수 있으면 좋다. –