2014-02-12 99 views
0

現在我有MainStarting類,其中游戲循環和油漆每60 FPS發生。如何讓角色腿移動在Java

我有3個狀態爲字符1-迴避,2-跳躍和3-行走,並且每個狀態都有它自己的圖片,我將這張圖片稱爲一個按鍵。

現在我想讓他的腿不僅移動,而且我不理解這樣做的邏輯。 我需要多少圖片? 60圖像覆蓋整個運動循環[從左腿開始,然後從右腿開始,然後站立] 60 cups的FPS?

多數民衆贊成在我所說的圖片

public void init() { 

    setSize(800, 480); 
    setBackground(Color.BLACK); 
    setFocusable(true); 
    addKeyListener(this); 
    Frame frame = (Frame) this.getParent().getParent(); 
    frame.setTitle("Q-Bot Alpha"); 
    try { 
     base = getDocumentBase(); 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 

    // Here are the images , Only 3 images standing and jumping and ducking 
    character = getImage(base, "data/character.png"); 
    characterDown = getImage(base, "data/down.png"); 
    characterJumped = getImage(base, "data/jumped.png"); 
    currentSprite = character; 
    background = getImage(base, "data/background.png"); 
    heliboy = getImage(base, "data/heliboy.png"); 
} 

,這裏是遊戲圈

public void run() { 
    while (true) { 
     robot.update(); 
     if (robot.isJumped()) { 
      currentSprite = characterJumped; 
     } else if (robot.isJumped() == false && robot.isDucked() == false) { 
      currentSprite = character; // use switch case in case the character is moving so u can use 10 pictures of the Stick man animation with switch case 
     } 

     ArrayList projectiles = robot.getProjectiles(); 
     for (int i = 0; i < projectiles.size(); i++) { 
      Projectile p = (Projectile) projectiles.get(i); 
      if (p.isVisible() == true) { 
       p.update(); 
      } else { 
       projectiles.remove(i); 
      } 
     } 

     bg1.update(); 
     bg2.update(); 
     hb.update(); 
     hb2.update(); 
     repaint(); 
     try { 
      Thread.sleep(17); // To get 60 FPS per second 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

然後我使用塗料的方法只是油漆的方法。 因此,我如何解決這個問題的任何幫助?

+0

_「我需要多少圖片」_這取決於您希望動畫的順利程度。 60個動畫幀聽起來像是矯枉過正,但取決於你。 – Michael

+0

嗯我不明白你是什麼矯枉過正的意思是,我知道60張圖片是很多,特別是我會做很多動畫,我不想消耗大量的CPU資源。那麼是否有另一種方法或解決方案? – user3293240

+0

overkill =超過需要 – Michael

回答

0

如果你能澄清你的代碼多一點,這將是偉大的,但現在,在此基礎上snipppet,

character = getImage(base, "data/character.png"); 
characterDown = getImage(base, "data/down.png"); 
characterJumped = getImage(base, "data/jumped.png"); 
currentSprite = character; 
background = getImage(base, "data/background.png"); 
heliboy = getImage(base, "data/heliboy.png"); 

在我看來,你有你的角色的每個可能的狀態獨立的精靈。你需要的是「在兩者之間」精靈/幀給你的性格「運動」(左/右小腿部分上,右/左小腿伸展,左/右小腿下去)。

我沒有做過Java中很多遊戲編程,多爲Android少了,但我認爲你可以從Pyganim提起的概念,並從那裏開始。 (Pyganim在Python中,使用PyGame。)

+0

這就是真的,我得到了精靈表,並使用幀計數器,每隔幾幀將繪製一張新紙...它仍然給我一些閃爍。我敢打賭,有一種更好的方式,但我會繼續搜索。 – user3293240