1

画面を横切り、画面の真ん中で止まって2回引っ掻く猫がいます。私の現在のコードは次のようになります

private void scratch(){
for (int i = xPos; i < getWidth(); i+=0) {
    xPos = i;
    // swap images
    if (currentImage == nekoPics[0]) 
        currentImage = nekoPics[2];
    else if (currentImage == nekoPics[2])
        currentImage = nekoPics[4];
    else if (currentImage == nekoPics[4])
        currentImage = nekoPics[5];
    else if (currentImage == nekoPics[5])
        currentImage = nekoPics[4];
    else if (currentImage == nekoPics[4]) 
        currentImage = nekoPics[5];
    else 
        currentImage = nekoPics[0]

if else ステートメントをこのような巨大な円に入れるよりも簡単に作成する方法はありますか?

事前に感謝します(PS:ある種のカウンターでこれを行うことができると思いますが、これについてどうすればよいかわかりませんでした。助けていただければ幸いです)

4

4 に答える 4

2

現在の画像のインデックスを保持し、反復ごとにインクリメントできます。たとえば、次のようになります。

currentImage = nekoPics[currentIndex%6];
currentIndex++;

また

currentImage = nekoPics[currentIndex];
if (++currentIndex==6) currentIndex=0;

これには、nekoPics の画像をアニメーションの順序に従って並べ替える必要があります。

于 2013-02-21T12:00:02.357 に答える
1

他の場所で提案されているマップに加えて、配列を使用することもできます。現在の画像のインデックスを追跡する必要があります。

int[5] nextImageList
  = { 2, ?, 4, 5, 4 }

next = nextImageList[currentImageIndex];
currentImage = nekoPics[next];
currentImageIndex = next;

currentImage と currentImageIndex を初期化した後は、「if」は必要ありません。1 がどこでも有効なインデックスであるかどうかはわかりませんでした。そうでない場合は、配列の 1 スロットに何でも入ることができます。

于 2013-02-21T12:10:23.567 に答える
0

その猫が画面の前に出ないようにすれば、おそらくコーディングが簡単になるでしょう...

ただし、真剣に、一連の写真を定義するオブジェクトを作成することで、これを解決できます。

于 2013-02-21T12:00:12.093 に答える
0

配列を使用して、 rcookに似た回答を投稿するつもりでした。理解するのが最も簡単な解決策だと思います。

ただし、彼の答えには、配列の次元に関してわずかな誤りがあります。完全を期すためにこれを投稿していますが、クレジットは彼に向けられるべきです.

// Elsewhere, in your initialization:
int currentImageIndex = 0; // Assuming [0] is your first image.
int[] nextImageList = { 2, -1, 4, -1, 5, 4 };
// Given the current index, this array will direct you
// to the next image index. Those -1 are unknown (to us).
// Set them to the values you need.

private void scratch() {
    for (int i = xPos; i < getWidth(); ) {
        xPos = i;

        // Swap images.
        currentImageIndex = nextImageList[currentImageIndex];
        currentImage = nekoPics[currentImageIndex];

        // What else you were doing here.
    }
}
于 2013-02-21T15:21:46.750 に答える