我正在尝试重新创建Flappy Bird作为Android编码的练习。在我变得非常困惑之前,我没有走得太远。我已经做了大量关于如何在onDraw方法中移动动画的研究,但我似乎总能找到的教程是在一开始编辑html风格的东西,你可以添加按钮,什么不是。我想在实际的视图中绘制动画,因为我计划在开始创建gestureListeners时移动动画。所以我问是否有人可以看看我到目前为止的代码,并告诉我究竟是什么问题。到目前为止,我已经创建了一个数组,用于添加鸟类上下拍打翅膀的图像,但它所做的只是将每张图片相互叠加,而不是仅在无限循环中一次打印一张图像看起来它正在扇动它的翅膀。谢谢!
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class FlappyView extends View
{
private Paint paint = new Paint(); // Creates Paint object
private Bitmap background; // Declares background
private boolean animation = true;
private Bitmap[] flappyFrames = new Bitmap[3];
Rect flappySize; // Sizes Flappy
public FlappyView(Context context)
{
super(context);
background = BitmapFactory.decodeResource(getResources(), R.drawable.large);
flappyFrames[0] = BitmapFactory.decodeResource(getResources(), R.drawable.flappybird1);
flappyFrames[1] = BitmapFactory.decodeResource(getResources(), R.drawable.flappybird2);
flappyFrames[2] = BitmapFactory.decodeResource(getResources(), R.drawable.flappybird3);
flappySize = new Rect(0, 0, 150, 200);
}
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Rect backgroundSize = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
canvas.drawBitmap(background, null, backgroundSize, paint);
if (animation)
for (int i = 0; i < flappyFrames.length; i++)
{
canvas.drawBitmap(flappyFrames[i], null, flappySize, paint);
}
invalidate();
}
}