我正在尝试重新创建 Flappy Bird 作为我的 Android 编码练习。在我变得非常困惑之前,我不必走很远。我已经对如何在 onDraw 方法中移动动画进行了大量研究,但我似乎只能找到从一开始就编辑 html 样式内容的教程,您可以在其中添加按钮等等。我想在实际视图中绘制动画,因为我计划在开始创建手势监听器时移动动画。所以我问是否有人可以查看我到目前为止的代码,并告诉我我到底做错了什么。到目前为止,我已经创建了一个数组来添加鸟儿上下拍打翅膀的图像,但它所做的只是将每张图片相互叠加,而不是在无限循环中一次只打印一个图像看起来像' s 拍打着翅膀。谢谢!
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();
}
}