2013-05-06 57 views
0

我在使用圖像而不是矢量圖形開發我的第一個Java遊戲時遇到了一些麻煩。目前,我正在嘗試使用AffineTransform對象在按鍵上移動圖像。只有它不會移動。我究竟做錯了什麼? 我還沒有嘗試過使用線程,但我不知道如何做到這一點。如果您認爲這會有所幫助,請將其納入您的回答。AffineTransform在按鍵上不移動圖像

這裏是我的代碼:

import java.awt.*; 
import java.net.*; 
import java.awt.event.*; 
import java.awt.geom.*; 
import java.applet.*; 
public class DisplayImage extends Applet implements KeyListener{ 
    AffineTransform identity = new AffineTransform(); 
    boolean left = false; 
    boolean right = false; 
    public URL getURL(String filename) 
    { 
     URL url = null; 
     try 
     { 
      url = this.getClass().getResource(filename); 
     } 
     catch(Exception e){e.printStackTrace();} 
     return url; 
    } 
    private Image image; 
    public void init() 
    { 
     image = getImage(getURL("spaceship.png")); 
     addKeyListener(this); 
    } 
    public void update(Graphics g){paint(g);} 
    public void paint(Graphics g) 
    { 
     AffineTransform trans = new AffineTransform(); 
     trans.setTransform(identity); 

     Graphics2D g2d = (Graphics2D)g; 
     g2d.setColor(Color.BLACK); 
     g2d.fillRect(0,0,getSize().width,getSize().height); 

     if(left==true) 
     { 
      trans.translate(-10,0); 
      left = false; 
      repaint(); 
     } 
     if(right==true) 
     { 
      trans.translate(10,0); 
      right = false; 
      repaint(); 
     } 
     g2d.drawImage(image,0,0,this); 
    } 
    public void keyPressed(KeyEvent e) 
    { 
     int keycode = e.getKeyCode(); 
     switch(keycode) 
     { 
     case KeyEvent.VK_RIGHT: 
      right = true; 
      repaint(); 
     case KeyEvent.VK_LEFT: 
      left = true; 
      repaint(); 
     } 
    } 
    public void keyTyped(KeyEvent e){} 
    public void keyReleased(KeyEvent e){} 

} 

是否有人可以發表評論或回答?我感覺到一個「風滾草」徽章正在朝我的方向前進。

+0

您使用AffineTransform移動而不是標準世界座標系的任何特定原因?這可能有助於瞭解你的目標是什麼類型的遊戲,因爲這些機制有更好的方法。 – Nikki 2013-05-08 08:04:09

+0

@Nikki我是一個完整的遊戲noob。如果有幫助,這是一個基於小程序的遊戲,我正在從書中進行調整,並正在操作圖像。這本書說這是一個很好的計劃。如果你有更好的解決方案,請告訴我! – imulsion 2013-05-08 09:33:46

回答

1

總體而言,您從書中學到的方法對於遊戲來說遠非理想,但無論如何我都會觸及代碼。

import java.awt.*; 
import java.net.*; 
import java.awt.event.*; 
import java.awt.geom.*; 
import java.applet.*; 
public class DisplayImage extends Applet implements KeyListener{ 

int coordX=0;//ship x axis position 
int coordY=0;//ship y axis position 
/* 
    0-------------------------x 
0| 
    | 
    | 
    | 
    | 
    | 
    y 
*/ 
public URL getURL(String filename) 
{ 
    URL url = null; 
    try 
    { 
     url = this.getClass().getResource(filename); 
    } 
    catch(Exception e){e.printStackTrace();} 
    return url; 
} 
private Image image; 
public void init() 
{ 
    image = getImage(getURL("spaceship.png")); 
    addKeyListener(this); 
} 
public void update(){repaint();}//let update control the calls to repaint 
public void paint(Graphics g) 
{ 

    Graphics2D g2d = (Graphics2D)g; 
    g2d.setColor(Color.BLACK); 
    g2d.fillRect(0,0,getSize().width,getSize().height); 

    g2d.drawImage(image,coordX,coordY,this);//paint the spaceship to the updated coordinates 
} 
public void keyPressed(KeyEvent e) 
{ 
    int keycode = e.getKeyCode(); 
    //37=left arrow. 39=right arrow. 38=up & 40=down 
    if(keycode==37){coordX-=1;}else if(keycode==39){coordX+=1;} 
    if(keycode==38){coordY-=1;}else if(keycode==40){coordY+=1;} 

update();//request repaint when logic has updated 
} 
public void keyTyped(KeyEvent e){} 
public void keyReleased(KeyEvent e){} 

} 

乾杯

編輯:: 這是你想要的工作的基本概念。

private void mainGameLoop(){ 
while(pause==false){ 
    updateLogic();//process movement, detect collisions etc 
    renderGraphics();//draw a new frame 
    try{Thread.sleep(25);}catch(InterruptedException e){e.printStackTrace();}//sleep the thread to control the frame rate. (there are more efficient ways to sleep a thread but this is the most simplistic) 
} 
} 
+0

該方法是否不能平滑移動圖像?只要按住按鍵,我就需要一種「滑動」動作。我正在製造太空入侵者,所以顯然需要從左向右平滑移動。我**認爲**這就是爲什麼我的書使用'AffineTransform' – imulsion 2013-05-11 07:15:59

+0

@imulsion AffineTransform將不會提供你所追求的。如果你想平滑移動,你需要使用浮點數進行運動計算,並且使用鎖定時間循環來控制幀速率,所有這一切都回到我在開始時所說的話。我會編輯我的答案。 – Nikki 2013-05-11 07:42:22

+0

謝謝你的幫助:) – imulsion 2013-05-11 07:47:43