从 ArrayList 打印多个精灵 - LOGCAT

Printing multiple sprites from an ArrayList - LOGCAT

我正在尝试创建 2 个 for 循环,循环并添加一定数量的精灵,然后通过另一个循环并打印精灵。但是它似乎崩溃了。在 update 方法中,它将精灵添加到数组列表中,并在 Do draw 方法打印出来。 Logcat也在下面。谢谢

  package cct.mad.lab;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.PorterDuff;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * This class takes care of surface for drawing and touches
 * 
 */

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

    /* Member (state) fields   */
    private GameLoopThread gameLoopThread;
    private Paint paint; //Reference a paint object 
    /** The drawable to use as the background of the animation canvas */
    private Bitmap mBackgroundImage;
    private Sprite sprite;
    int arraySize;
    private int hitCount;

    private ArrayList<Sprite> spritesArrayList;

    int count = 5;
    int finalScore;
    private boolean gameOver;
    /* For the countdown timer */
    private long  startTime ;           //Timer to count down from
    private final long interval = 1 * 1000;     //1 sec interval
    private CountDownTimer countDownTimer;  //Reference to class
    private boolean timerRunning = false;
    private String displayTime;         //To display time on the screen

    private Bitmap spritebmp;


    public GameView(Context context) {

        super(context);
        // Focus must be on GameView so that events can be handled.
        this.setFocusable(true);
        // For intercepting events on the surface.
        this.getHolder().addCallback(this);
        mBackgroundImage = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.background2);
        spritesArrayList= new ArrayList<Sprite>();
    }



    public void surfaceCreated(SurfaceHolder holder) {
         for (int i = 0; i < count; i++) {
                spritesArrayList.add(sprite);
            }
        mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, getWidth(), getHeight(), true);
        ResetGame();
        gameLoopThread = new GameLoopThread(this.getHolder(), this);
        gameLoopThread.running = true;
        gameLoopThread.start();

    }

    //To initialise/reset game
    private void ResetGame(){
        gameOver = false;
        hitCount = 0;
        sprite = new Sprite(this);
        /* Set paint details */
        paint = new Paint();
        paint.setColor(Color.WHITE); 
        paint.setTextSize(20);

        //Set timer
        startTime = 60;//Start at 10s to count down
        //Create new object - convert startTime to milliseconds
        countDownTimer=new MyCountDownTimer(startTime*1000,interval);
        countDownTimer.start();//Start it running
        timerRunning = true;

    }
    public void GetArrayListSize()
    {
    if(spritesArrayList.isEmpty())
    {
    //nothing
    }
    else
    {
    arraySize = spritesArrayList.size();
    }
    }

    //This class updates and manages the assets prior to drawing - called from the Thread
    public void update(){
        if (gameOver != true)
        {
        sprite.update();
        }

         for (int i = 0; i < count; i++) 
         {              
             sprite = new Sprite(this);
             spritesArrayList.add(sprite);
         }
    }
    /**
     * To draw the game to the screen
     * This is called from Thread, so synchronisation can be done
     */

    public void doDraw(Canvas canvas) {
         Paint textPaint = new Paint();

        canvas.drawBitmap(mBackgroundImage, 0, 0, null);
        //Draw all the objects on the canvas
        canvas.drawText("The Game ",5,25, paint);
        canvas.drawText("Score: " + hitCount, 5, 50, paint);
        canvas.drawText("Time: " +displayTime, 5, 75, paint);
        GetArrayListSize();

         //Loop for sprite creation
         for (int i = 0; i < arraySize; i++) 
         {          
            Sprite sprite = spritesArrayList.get(i);
            sprite.draw(canvas);
         }



        if (gameOver == true)
        {
            canvas.drawText("Final Score: "+finalScore, 5,100, paint);
            int width = this.getMeasuredWidth()/2;
            int height = this.getMeasuredHeight()/2;
            textPaint.setTextAlign(Align.CENTER);
            canvas.drawText("GAME OVER - PRESS BACK BUTTON TO RETURN", width, height, textPaint);
        }

    }
    public int getHitCount()
    {
        return hitCount;
    }
    //To be used if we need to find where screen was touched
    public boolean onTouchEvent(MotionEvent event) {
        if (gameOver != true)
        {

            if (sprite.wasItTouched(event.getX(), event.getY()))
                {

                sprite = new Sprite(this);

                    hitCount++;         
                }

        }
        return true;
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        gameLoopThread.running = false;

        // Shut down the game loop thread cleanly.
        boolean retry = true;
        while(retry) {
            try {
                gameLoopThread.join();
                retry = false;
            } catch (InterruptedException e) {}
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }
    private class MyCountDownTimer extends CountDownTimer {

          public MyCountDownTimer(long startTime, long interval) {
                super(startTime, interval);
          }
          public void onFinish() {
                displayTime = "Times Over!";
                finalScore = hitCount;
                gameOver = true;
                timerRunning = false;
                countDownTimer.cancel();
          }
          public void onTick(long millisUntilFinished) {
                displayTime = " " + millisUntilFinished / 1000;
          }
        }//End of MyCountDownTimer



}

LogCat

    05-04 13:24:27.654: E/AndroidRuntime(1448): FATAL EXCEPTION: Thread-99
05-04 13:24:27.654: E/AndroidRuntime(1448): Process: cct.mad.lab, PID: 1448
05-04 13:24:27.654: E/AndroidRuntime(1448): java.lang.OutOfMemoryError
05-04 13:24:27.654: E/AndroidRuntime(1448):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
05-04 13:24:27.654: E/AndroidRuntime(1448):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
05-04 13:24:27.654: E/AndroidRuntime(1448):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)

在你的全局变量中。 请检查这一行

private ArrayList<Sprite> spritesArrayList;
int arraySize = spritesArrayList.size();

我认为 private ArrayList<Sprite> spritesArrayList; 此时为 null,而在下一行你正在尝试获取它的大小;

编辑:

我认为你应该像这个算法一样return创建一个方法return这个arraylist的大小

public void GetArrayListSize()
{
if( arraylist is empty)
{
//nothing
}
else
{
arraySize = spritesArrayList.size();
}
}

并在需要获取此数组列表的大小时调用此方法

编辑 2:

改变

int arraySize = spritesArrayList.size();

arraySize = spritesArrayList.size();

在您的 GetArrayListSize() 方法中