2015-02-07 70 views
0

我目前正在爲大學制作一款冒險2D遊戲項目。 我畫了一系列圖片(牆壁,地板等),然後是可移動的英雄形象。問題是:如果沒有陣列,這個數字會很好地移動,但是隨着陣列的移動開始變得很難。它是我的電腦嗎?還是我的繪圖方法太「重」?請看看:我的照片移動得太慢

import java.awt.Graphics; 
import java.awt.Rectangle; 

public class World { 
public static int WorldW = 16 , WorldH = 16; 
public SpielElement[][] element = new SpielElement[WorldW][WorldH]; 
public int[][] level0 ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0}, 
         {0,0,0,1,1,1,1,1,1,1,1,6,0,0,0,0}, 
         {0,0,0,1,1,1,5,1,1,1,1,1,0,0,0,0}, 
         {0,0,0,1,1,6,1,1,2,1,5,1,0,0,0,0}, 
         {0,0,0,1,1,1,1,5,1,1,1,1,0,0,0,0}, 
         {0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0}, 
         {0,0,0,0,0,1,1,3,1,1,0,0,0,0,0,0}, 
         {0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; 

public int[][] level1 ={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
         {0,0,0,1,0,0,0,0,1,1,1,1,2,0,1,0}, 
         {0,4,0,1,0,0,5,1,1,1,1,1,1,0,1,0}, 
         {0,1,0,1,0,0,0,0,0,0,1,0,0,0,1,0}, 
         {0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,0}, 
         {0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0}, 
         {0,1,1,1,0,0,1,1,1,1,1,0,0,1,0,0}, 
         {0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0}, 
         {0,1,0,1,1,1,1,1,1,1,1,1,0,0,3,0}, 
         {0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0}, 
         {0,1,0,1,1,1,1,0,0,1,0,1,0,0,1,0}, 
         {0,1,0,1,0,0,1,0,0,1,0,1,1,1,1,0}, 
         {0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,0}, 
         {0,1,1,1,1,6,1,1,0,1,0,0,1,0,0,0}, 
         {0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0}, 
         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; 

public World(){ 
    create(); 
} 

public void create(){ 
    for(int x = 0; x<WorldW; x++){ 
     for(int y = 0; y<WorldH; y++){ 
      element[x][y] = new SpielElement(x,y,level0[y][x]); 
     } 
    } 
} 


public void generate(Graphics g){ 
    for(int x = 0; x<WorldW; x++){ 
     for(int y = 0; y<WorldH; y++){ 
      element[x][y].zeichne(g); 
     } 
    } 
} 

}

嘛int數組是定義我需要哪種圖片的繪製和 我打算先創建所有的面板(SpielElement),然後繪製它們 這是我如何吸引他們:

import java.awt.*; 
    import java.awt.image.BufferedImage; 
    import java.io.IOException; 

    import javax.imageio.ImageIO; 
    import javax.swing.*; 
    public class SpielElement extends JPanel{ 
    int posX,posY,type ; 
    BufferedImage wand, boden, tuer, schluessel; 



public SpielElement(int x, int y,int z){ 
    this.posX = x*40; 
    this.posY = y*40; 
    this.type = z; 

    try { 
     wand = ImageIO.read(SpielElement.class.getResource("/img/wand.png")); 
     boden = ImageIO.read(SpielElement.class.getResource("/img/boden.png")); 
     tuer = ImageIO.read(SpielElement.class.getResource("/img/tuer.png")); 
     schluessel = ImageIO.read(SpielElement.class.getResource("/img/schluessel.png")); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



} 

public void zeichne(Graphics g){ 
    switch(type){ 
    case 0: g.drawImage(wand,posX,posY,boden.getWidth(),wand.getHeight(),null); 
     break; 
    case 1:g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null); 
     break; 
    case 2: g.drawImage(schluessel,posX,posY,schluessel.getWidth(),schluessel.getHeight(),null); 
     break; 
    case 3:  g.drawImage(tuer,posX,posY,tuer.getWidth(),tuer.getHeight(),null); 
     break; 
    case 4:  g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null); 
     break; 
    case 5:g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null); 
     break; 
    case 6:  g.drawImage(boden,posX,posY,boden.getWidth(),boden.getHeight(),null); 
     break; 

} 
} 

}

我希望u得到什麼,我想通過閱讀代碼,希望SE什麼是錯的。沒有任何語法錯誤,但它的速度太慢了。在結束時加入所有的這在其上在JFrame

import java.awt.*; 

import javax.swing.*; 

import java.awt.event.*; 

public class Board extends JPanel implements KeyListener{ 
private final int BREITE = 700; 
private final int HOEHE = 700; 
Hunter Held ; 
World Welt; 



public Board(){ 
    super(); 
    Held = new Hunter(200,200); 
    Welt = new World(); 

    this.setBackground(Color.BLACK); 
} 

public void paint(Graphics g){ 
    super.paint(g); 
    Welt.generate(g); 
    Held.draw(g); 
} 

@Override 
public void keyPressed(KeyEvent e) { 
    // TODO Auto-generated method stub 
    if (e.getKeyCode() == KeyEvent.VK_LEFT) 
     Held.posX = Held.posX - 10; 

    if (e.getKeyCode() == KeyEvent.VK_RIGHT) 
     Held.posX = Held.posX + 10; 

    if (e.getKeyCode() == KeyEvent.VK_UP) 
     Held.posY = Held.posY - 10; 

    if (e.getKeyCode() == KeyEvent.VK_DOWN) 
     Held.posY = Held.posY + 10; 
     this.repaint(); 

} 

@Override 
public void keyReleased(KeyEvent arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void keyTyped(KeyEvent arg0) { 
    // TODO Auto-generated method stub 

} 

}

英雄(保持)被吸入等與其座標其它圖像的確切方式beeing改變每次添加一個JPanel板上I按鍵並重新塗漆。 在這一點上,我將避免進一步的代碼傾銷。這是怎麼看起來像在最後: http://puush.me/account?pool=1468274

(對不起沒有信譽尚未) 上Scrren該圖上KeyInput但隨着滯後移動... 這是我的第一個問題,我希望這是不夠詳細。 Sry提前如果我的問題是愚蠢的,只在2周前開始編程。 建議性的批評被建議。 SRY代碼傾銷...

+1

您的鏈接要求登錄... – hyde 2015-02-07 18:51:18

+0

我的猜測是,你是重繪方式太頻繁。您可能需要更新_only_按鍵事件中的關鍵位置,並使用擺動計時器以某種固定刷新率更新顯示。 – jtahlborn 2015-02-07 19:55:46

+0

當你不需要時,不要重繪大量的小元素,例如,世界,它不會改變那麼多。相反,將世界繪製到BuferredImage並只需一步即可完成。一般來說,單張圖像會更快,然後很多小圖像或圖形元素 – MadProgrammer 2015-02-07 21:31:11

回答

0

直到我看到你所提到的JPanel並JFrame的,我認爲這是一個Android相關的問題...

反正也許你應該畫在一個新的線程,一個無形的畫布上/ image/cache,無論如何......然後將其放在前面並在後臺繪製另一幀。

對不起,我不知道如何用Swing做,但我認爲這就是所有動畫框架的工作原理。在Android人做後臺線程所有這些骯髒的工作...