I'm trying to recreate Flappy Bird as practice for my Android coding. I didn't have to go very far before I became really confused. I've done a ton of research on how to move animations in the onDraw method, but all I can seem to find were tutorials editing the html-style stuff at the very beginning where you can add buttons and what not. I want to draw the animation in the actual View because I plan to move the animation when I begin creating gestureListeners. So I'm asking if anyone can look at my code I have so far, below, and tell me what exactly I'm doing wrong. So far I've created an array to add the images of the bird flapping its wings up and down, but all it is doing is drawing each picture on top of each other, instead of only printing one image at a time in an infinite loop to look like it's flapping its wings. Thanks!
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();
}
}