3

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();

}

}

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3376654
  • 67
  • 1
  • 9
  • I already have done a copy cat of flappy bird using only Android XML and in my case I used an AnimationList, it's a different approach and not the best thing, but as you I was just testing my skills and trying to lern something new. If you want to go that way just take a look at: http://developer.android.com/guide/topics/resources/animation-resource.html – GhostDerfel Mar 11 '14 at 02:52
  • I think I'm going to start learning about the XML soon in my programming class, that was why I was a little cautious to use it since I've never even looked at it before, but also because I read somewhere that you need to do everything in the onDraw and in your View if you plan to change its x-values and make it move and what not. But I just feel like the animation for Android is a lot trickier than for JFrames – user3376654 Mar 11 '14 at 15:11
  • You can do everything just using XML, but sometimes it get a little dificult to control everything in the screen, but basicly to copy flabby bird with XML Layouts in Android, you need some object to be in front of everthing just to control the tap, an container to your bird animation and another for the pipes animation then you just need to control everything with timers :P but as stated before, this is just to prove a concept and it's far from the right way to code games in Android – GhostDerfel Mar 11 '14 at 17:17

0 Answers0