多个精灵和动画问题

时间:2015-05-07 19:28:44

标签: java android sprite

我正在尝试使用数组向Android游戏中添加多个精灵,但是当时只有一个会显示,而当我点击它而不是更改为新的精灵动画时,它在屏幕上冻结这里是我的代码GameView.java和Sprite.java。

GameView.java:

    /**
     * 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;

        private ArrayList<Sprite> spritesArrayList;

        int SpriteAmount = 3;

        private int hitCount;

        private boolean GameOver = false;

        /* 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

        public GameView(Context context) {
            super(context);
            spritesArrayList = new ArrayList<Sprite>();
            // 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.dbz);

        }

        /* Called immediately after the surface created */
        public void surfaceCreated(SurfaceHolder holder) {


            mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage,
                    getWidth(), getHeight(), true);

            // We can now safely setup the game start the game loop.
            ResetGame();// Set up a new game up - could be called by a 'play again
                        // option'
            gameLoopThread = new GameLoopThread(this.getHolder(), this);
            gameLoopThread.running = true;
            gameLoopThread.start();
        }

        // Sprite List



        // To initialise/reset game
        private void ResetGame() {


            //sprite = new Sprite(this);

            for (int P = 0; P < SpriteAmount; P++) {

                spritesArrayList.add(sprite = new Sprite(this));

            }

            hitCount = 0;

            GameOver = false;

            /* Set paint details */
            paint = new Paint();
            paint.setColor(Color.WHITE);
            paint.setTextSize(20);

            // Set timer
            startTime = 10;// 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;

        }

        // This class updates and manages the assets prior to drawing - called from
        // the Thread
        public void update() {
    for (int P = 0; P < SpriteAmount; P++) {

                GameOver = false;

                spritesArrayList.get(P).update();
            }
        }

        /**
         * To draw the game to the screen This is called from Thread, so
         * synchronisation can be done
         */
        public void doDraw(Canvas canvas) {

            canvas.drawBitmap(mBackgroundImage, 0, 0, null);

            if (GameOver == false) {
                sprite.draw(canvas);
            } else if (GameOver == true) {

            }
            // Draw all the objects on the canvas
            canvas.drawText("Hits Obtained =" + hitCount, 5, 25, paint);
            canvas.drawText("Time Remaining =" + displayTime, 5, 40, paint);

            if (GameOver == true) {

                canvas.drawText("Return To Main Menu using return button", 5, 60,
                        paint);
            }

        }

        // To be used if we need to find where screen was touched
        public boolean onTouchEvent(MotionEvent event) {
            for (int P = 0; P < SpriteAmount; P++) {


                if (spritesArrayList.get(P).wasItTouched(event.getX(), event.getY())) {
                    /* For now, just renew the Sprite */
                    spritesArrayList.add(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) {

        }

        /* Countdown Timer - private class */
        private class MyCountDownTimer extends CountDownTimer {

            public MyCountDownTimer(long startTime, long interval) {
                super(startTime, interval);
            }

            public void onFinish() {
                displayTime = "Times Over!";
                timerRunning = false;
                countDownTimer.cancel();
                GameOver = true;
            }

            public void onTick(long millisUntilFinished) {
                displayTime = " " + millisUntilFinished / 1000;
            }
        }// End of MyCountDownTimer

        public int getHitCount() {
            // TODO Auto-generated method stub
            return hitCount;
        }

    }

Sprite.java:

public class Sprite {

        // Needed for new random coordinates.
        private Random random = new Random();
        // x,y position of sprite - initial position (0,50)
        int x = random.nextInt(500);
        int X = random.nextInt(500);
        int y = random.nextInt(1000);
        int Y = random.nextInt(1000);
        private int xSpeed = 10;// Horizontal increment of position (speed)
        // apply at later date random.nextInt(10)
        private int ySpeed = 10;// Vertical increment of position (speed)
        // apply at later date random.nextInt(10)
        private GameView gameView;
        private Bitmap spritebmp;
        // Width and Height of the Sprite image
        private int bmp_width;
        private int bmp_height;

        private Integer[] imgid = { R.drawable.kidbuu, R.drawable.guko,
                R.drawable.guko_ssj1

        };

        // Calculation for reverse direction
        // x = x - (x - x)
        // y = y - (y - y)

        public Sprite(GameView gameView) {
            this.gameView = gameView;
            spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
                    R.drawable.guko);
            this.bmp_width = spritebmp.getWidth();
            this.bmp_height = spritebmp.getHeight();
        }

        // update the position of the sprite
        public void update() {
            x = x + xSpeed;
            y = y + ySpeed;
            wrapAround(); // Adjust motion of sprite.
        }

        public void ReverseRandom() {

            X = (x - (x - x));
            Y = (y - (y - y));
        }

        public void draw(Canvas canvas) {

            // Draw sprite image
            // y = random.nextInt(500);
            // x = random.nextInt(500);
            canvas.drawBitmap(spritebmp, x, y, null);
        }

        public int calcX(int value) {
            return random.nextInt(value);
        }

        public int calcY(int value) {
            return random.nextInt(value);
        }

        public void wrapAround() {

            ReverseRandom();

            // Code to wrap around
            if (x < 0)
                x = X; // increment x whilst not off screen

            if (x >= gameView.getWidth() - spritebmp.getWidth() - xSpeed) { // if
                                                                            // gone
                                                                            // of
                                                                            // the
                                                                            // right
                                                                            // sides
                                                                            // of
                // screen
                xSpeed = -5;

            }

            if (x + xSpeed <= 0) {
                xSpeed = 5;
            }
            x = x + xSpeed;

            if (y < 0)
                y = Y;// increment y whilst not off screen

            if (y >= gameView.getHeight() - spritebmp.getWidth() - ySpeed) {// if
                                                                            // gone
                                                                            // of
                                                                            // the
                                                                            // bottom
                                                                            // of
                                                                            // screen
                ySpeed = -5;
            }

            if (y + ySpeed <= 0) {
                ySpeed = 5;
            }
            y = y + ySpeed;

        }

        /* Checks if the Sprite was touched. */
        public boolean wasItTouched(float ex, float ey) {
            boolean touched = false;
            if ((x <= ex) && (ex < x + bmp_width) && (y <= ey)
                    && (ey < y + bmp_height)) {
                touched = true;
            }
            return touched;
        }// End of wasItTouched

        public void change(Integer[] P) {
            imgid = P;

            spritebmp = BitmapFactory.decodeResource(gameView.getResources(),
                    R.drawable.guko_ssj1);

        }

    }

0 个答案:

没有答案