2014-11-02 66 views
0

我在初學者水平和工作在一個Java 2D遊戲。 所以,我想修改已經存在的代碼,我希望當我按下按鍵A.任何人都可以告訴什麼,在哪裏我做錯了以動畫的對象?如何通過按鍵Java 2D遊戲動畫圖像?

在此先感謝

我的代碼是:

package object; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.geom.AffineTransform; 
import javax.imageio.ImageIO; 
import java.net.URL; 
import java.awt.*; 
import javax.swing.*; 
import java.io.*; 



@SuppressWarnings("serial") 
public class PlayerA extends JPanel { 
    // Named-constants 
    static final int CANVAS_WIDTH = 400; 
    static final int CANVAS_HEIGHT = 480; 
    public static final String TITLE = "Test Player"; 

    private String[] imgFilenames = { 
     "object/images/image1.png", "object/images/image2.png", "object/images/image3.png",}; 
    private Image[] imgFrames;  
    private int currentFrame = 0; 
    private int frameRate = 5; 
    private int imgWidth, imgHeight; 
    private double x = 50.0, y = 200.0; 
    private double speed = 8;   
    private double direction = 0;  
    private double rotationSpeed = 10; 
    private AffineTransform transform = new AffineTransform(); 


    public PlayerA() { 

     loadImages(imgFilenames); 
     final Thread animationThread = new Thread() { 
     @Override 
     public void run() { 
      while (true) { 
       update(); 
       repaint(); 
       try { 
        Thread.sleep(1000/frameRate); 
       } catch (InterruptedException ex) { } 
      } 
     } 
     }; 

     addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyPressed(KeyEvent evt) { 
      switch(evt.getKeyCode()) { 
       case KeyEvent.VK_A: 
        animationThread.start(); 

      } 
      } 
     }); 

     setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT)); 
     requestFocus(); 

    } 

    private void loadImages(String[] imgFileNames) { 
     int numFrames = imgFileNames.length; 
     imgFrames = new Image[numFrames]; 
     URL imgUrl = null; 
     for (int i = 0; i < numFrames; ++i) { 
     imgUrl = getClass().getClassLoader().getResource(imgFileNames[i]); 
     if (imgUrl == null) { 
      System.err.println("Couldn't find file: " + imgFileNames[i]); 
     } else { 
      try { 
       imgFrames[i] = ImageIO.read(imgUrl); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
     } 
     imgWidth = imgFrames[0].getWidth(null); 
     imgHeight = imgFrames[0].getHeight(null); 
    } 


    public void update() { 
     x += speed * Math.cos(Math.toRadians(direction)); 
     if (x >= CANVAS_WIDTH) { 
     x -= CANVAS_WIDTH; 
     } else if (x < 0) { 
     x += CANVAS_WIDTH; 
     } 
     y += speed * Math.sin(Math.toRadians(direction)); 
     if (y >= CANVAS_HEIGHT) { 
     y -= CANVAS_HEIGHT; 
     } else if (y < 0) { 
     y += CANVAS_HEIGHT; 
     } 
     direction += rotationSpeed; 
     if (direction >= 360) { 
     direction -= 360; 
     } else if (direction < 0) { 
     direction += 360; 
     } 
     ++currentFrame;  
     if (currentFrame >= imgFrames.length) { 
     currentFrame = 0; 
     } 
    } 


    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     setBackground(Color.WHITE); 
     Graphics2D g2d = (Graphics2D) g; 

     transform.setToIdentity(); 
     transform.translate(x - imgWidth/2, y - imgHeight/2); 
     transform.rotate(Math.toRadians(direction), 
      imgWidth/2, imgHeight/2); 

     g2d.drawImage(imgFrames[currentFrame], transform, null); 
    } 


    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      JFrame frame = new JFrame(TITLE); 
      frame.setContentPane(new PlayerA()); 
      frame.pack(); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLocation(900, 0); 
      frame.setVisible(true); 
     } 
     }); 
    } 
} 

回答

0

我沒有看過你的代碼,但你應該是這樣的:

update(float deltaTime) { 
    if(isKeyPressed(Keys.A)) 
    animatePlayer(deltaTime); 
//other things 
} 

private void animatePlayer(float deltaTime) { 
//animation grounded on deltaTime here 
} 

記住使用的DeltaTime移動/動畫東西,因爲你的遊戲將不同作用於快速和慢速PC /智能手機/平板電腦。