2011-06-11 4 views
0

자바 게임을 만들고 사용자가 일부 키를 누르면 스프라이트가 그 방향으로 이동하고 스프라이트가 일치하도록 변경됩니다 사용자가 입력하고있는 방향 이 웹 사이트에 현재 게임, 머리를보고 싶다면 :스프라이트 이동하기, 마지막으로 이동 한 위치에 스프라이트가 있어야합니다.

int x_posI = (int) x_pos; 
    int y_posI = (int) y_pos; 


    if (downPressed && leftPressed) { 
     g.drawImage(hero225, x_posI, y_posI, this); 
     spr270 = false; 
    } else if (downPressed && rightPressed) { 
     spr270 = false; 
     g.drawImage(hero135, x_posI, y_posI, this); 
    } else if (upPressed && rightPressed) { 
     spr270 = false; 
     g.drawImage(hero45, x_posI, y_posI, this); 
    } else if (upPressed && leftPressed) { 
     g.drawImage(hero315, x_posI, y_posI, this); 
     spr270 = false; 
    } else if (leftPressed == true) { 
     g.drawImage(hero270, x_posI, y_posI, this); 
     spr270 = true; 
    } else if (rightPressed == true) { 
     g.drawImage(hero90, x_posI, y_posI, this); 
     spr270 = false; 
    } else if (upPressed == true) { 
     g.drawImage(hero, x_posI, y_posI, this); 
     spr270 = false; 
    } else if (downPressed == true) { 
     g.drawImage(hero180, x_posI, y_posI, this); 
     spr270 = false; 
    } 
     else{ 
      g.drawImage(hero, x_posI, y_posI, this); 
     } 
    if(spr270) { 
     g.drawImage(hero270, x_posI, y_posI, this); 
    } 

나는 왼쪽을 누르면, 이런 것입니다 : 여기 http://thetutspace.org/acropolis/beta/

을 내가 사용하는 코드입니다 i.stack .imgur은 [점] 내가 가게되면, 이것이 COM/owT3z.png

내가 무슨 : i.stack.imgur.com/2Wrjr[dot]png

어떻게 그래서 그것을 만들 수 있습니다 캐릭터가 왼쪽을 향하고 있습니까?

+0

: 왜'경우 (foo는 == true)를 {'있나요? 왜''if (foo) {'? –

+0

나는 그것을 생각하지 않았다. 고마워. – Derek

+0

이제 당신의 현재 문제에 관해서, 나는 당신의 문제가 무엇인지 혼란스러워합니다. 문제의 세부 사항을 설명하는 원본 게시물에 대한 설명의 단락이나 두 개 또는 해결하려고 할 때 작동하지 않는 부분이 필요할 수 있습니다. –

답변

0

내부 페인트 (그래픽 g) 방법입니다. 맞습니까?

휘발성 이미지 필드 스프라이트를 클래스에 추가하십시오 ("protected volatile Image sprite;"). 변경 로직 :

이 문제에 무관하지만 가독성에 관련
int x_posI = (int) x_pos; 
int y_posI = (int) y_pos; 

if (downPressed && leftPressed) { 
    this.sprite = hero225; 
} else if (downPressed && rightPressed) { 
    this.sprite = hero135; 
} else if (upPressed && rightPressed) { 
    this.sprite = hero45; 
} else if (upPressed && leftPressed) { 
    this.sprite = hero315; 
} else if (leftPressed == true) { 
    this.sprite = hero270; 
} else if (rightPressed == true) { 
    this.sprite = hero90; 
} else if (upPressed == true) { 
    this.sprite = hero; 
} else if (downPressed == true) { 
    this.sprite = hero180; 
} 

// this.sprite will contain value set on last "movement" 
g.drawImage(this.sprite, x_posI, y_posI, this); 
+0

절대적으로 환상적입니다. 고마워요 :) – Derek

+0

내 기쁨 (- : – mvmn

관련 문제