我试图重新创建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();
}