2

そこで、Javaでゲームを作成し、drawRect()メソッドを使用して、プレーヤー、敵、およびショットを表現することから始めました。すべてが素晴らしかった。それから私は空想を得ようと決心しました。各オブジェクトの.png画像を作成し、Graphics2D drawImage()メソッドを使用しました。すべてが減速し始めました。プロセスをスピードアップする別の方法はありますか?

私のアニメーションはスイングタイマーに基づいています

    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D)g;
    player1.paintShips(g);
    g2d.drawImage(bGround, 14, 14, this);
    try{
        for(Shot s: liveRounds){ //liveRounds is an ArrayList of Shots
            if(!inBounds.contains(s.getRect()) || villains.collision(s)){
                if(villains.collision(s)){
                    villains.collided(s, this);
                }
                liveRounds.remove(s);
                roundCount--;
            }
            else{
                s.paintShot(g, this);                   
            }
        }
    }catch(ConcurrentModificationException e){};
    villains.paintEnemyGrid(g, this);
    g2d.setColor(Color.cyan);
    g2d.draw(hitZone1);
    g2d.setColor(Color.red);
    g.drawString("X: " + player1.getX(1) + "  Y: " + player1.getY(1), 370, 150);
    g2d.draw(inBounds);
    g.drawString(score + "", 440, 40);
    g.dispose();
} 

アニメーションに関するヒントやチュートリアルはありますか?ありがとう

4

1 に答える 1

1

10 ミリ秒の遅延は、1 秒あたり 100 フレームです。それはほぼ確実に速すぎます。

また、反復処理中にオブジェクトを削除する場合は、次のCollectionようにする必要があります。

Iterator<T> itr = collection.iterator();
while(itr.hasNext()) {
    T obj = itr.next();
    if(removeObj) {
        itr.remove();
    }
}

ConcurrentModificationException非決定的な動作につながります。それらを無視するのではなく、避ける必要があります。

于 2012-04-08T20:10:23.820 に答える