java - 改善外星人的行动-LMLPHP

我编写了一个受《太空入侵者》和Moon Patrol启发的迷你Android游戏场景。可以水平射击外星人(见上文)。

也可以垂直射击外星人(见下文)。

java - 改善外星人的行动-LMLPHP

但是添加外星人并不能“缩放”,例如添加15个外星人以应对所有可能的碰撞将非常困难。最初的太空侵略者和月球巡逻队解决了这一问题,是否有可能制定出与我正在使用的策略不同的策略?外星人的确切 Activity 并不重要,仅是“有趣”。

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.Rect;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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

public class ParallaxView extends SurfaceView implements Runnable {

    List<Background> backgrounds;

    private volatile boolean running;
    private Thread gameThread = null;

    // For drawing
    private Paint paint;
    private Canvas canvas;
    private SurfaceHolder ourHolder;

    // Holds a reference to the Activity
    Context context;

    // Control the fps
    long fps = 60;

    // Screen resolution
    int screenWidth;
    int screenHeight;

    private void update() {
        // Update all the background positions
        for (Background bg : backgrounds) {
            bg.update(fps);
        }

    }

    ParallaxView(Context context, int screenWidth, int screenHeight) {
        super(context);

        this.context = context;

        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;

        // Initialize our drawing objects
        ourHolder = getHolder();
        paint = new Paint();

        // Initialize our array list
        backgrounds = new ArrayList<>();

        //load the background data into the Background objects and
        // place them in our GameObject arraylist

        backgrounds.add(new Background(
                this.context,
                screenWidth,
                screenHeight,
                "bg", 0, 120, 50));

        backgrounds.add(new Background(
                this.context,
                screenWidth,
                screenHeight,
                "grass", 70, 110, 200));

        // Add more backgrounds here

    }

    @Override
    public void run() {

        while (running) {
            long startFrameTime = System.currentTimeMillis();

            update();
            if (j > 2000) {
                j = -50;
                k = 0;
            }
            if (o > 2000) {
                o = -50;
                l = 0;
            }
            draw();

            // Calculate the fps this frame
            long timeThisFrame = System.currentTimeMillis() - startFrameTime;
            if (timeThisFrame >= 1) {
                fps = 1000 / timeThisFrame;
            }
        }
    }

    int numberOfshots = 1;
    int[] i = new int[200];
    int j = 0;
    int k = 0;
    int l = 0;
    int m = 0;
    int o = 0;
    boolean down = true;
    long lastTurn = System.currentTimeMillis();
    int xbuggy = 0;
    int xbuggy2 = 0;
    boolean down2 = true;
    long lastTurn2 = System.currentTimeMillis();
    long lastTurn3 = System.currentTimeMillis();
    boolean jump = false;
    boolean shoot = false;
    int ind = 0;

    private void draw() {

        if (ourHolder.getSurface().isValid()) {
            //First we lock the area of memory we will be drawing to
            canvas = ourHolder.lockCanvas();
            if (jump) {
                xbuggy = xbuggy + 4;
            }
            if (shoot) {
                xbuggy2 = xbuggy2 + 4;
            }

            if (System.currentTimeMillis() - lastTurn3 >= 1000) {
                // Change direction here
                jump = false;
                lastTurn3 = System.currentTimeMillis();
                xbuggy = 0;
            }
            //draw a background color
            canvas.drawColor(Color.argb(255, 0, 0, 0));

            // Draw the background parallax
            drawBackground(0);

            // Draw the rest of the game
            paint.setTextSize(60);
            paint.setColor(Color.argb(255, 255, 255, 255));

            //canvas.drawText("MOONPATROL3000", 350, screenHeight / 100 * 5, paint);

            int resID = context.getResources().getIdentifier("vehicle",
                    "drawable", context.getPackageName());

            int alienResID = context.getResources().getIdentifier("object3_hdpi",
                    "drawable", context.getPackageName());

            int alienResID2 = context.getResources().getIdentifier("object2_hdpi",
                    "drawable", context.getPackageName());

            int alienResID3 = context.getResources().getIdentifier("object1_hdpi",
                    "drawable", context.getPackageName());

            // Load the bitmap using the id
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
            Bitmap alienbitmap = BitmapFactory.decodeResource(context.getResources(), alienResID);
            Bitmap alienbitmap2 = BitmapFactory.decodeResource(context.getResources(), alienResID2);
            Bitmap alienbitmap3 = BitmapFactory.decodeResource(context.getResources(), alienResID3);




            //paint.setTextSize(220);
            for (int i1 = 0; i1 < numberOfshots; i1++) {


                // if horizontal missile hits alien 0
                if (java.lang.Math.abs(j - i[i1]) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alienbitmap.getHeight() + 60)) {
                    //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                    //score += 1;
                    //onScoreListener.onScore(score);
                    Log.d("missile", "missile hit! ");
                    j=-200;
                }

                // if vertical missile hits alien 0
                if (java.lang.Math.abs(j - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(j + 150 + screenHeight / 100 * 45 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                    //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                    //score += 1;
                    //onScoreListener.onScore(score);
                    Log.d("missile", "missile hit! ");
                    j=-200;
                }


                // if horizontal missile hits alien 1, right now this won't happen
                if (java.lang.Math.abs(j - i[i1]) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alienbitmap.getHeight() + 60)) {
                    //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                    //score += 1;
                    //onScoreListener.onScore(score);
                    Log.d("missile", "missile hit! ");
                    j=-200;
                }

                // if vertical missile hits alien 1
                if (java.lang.Math.abs(o + 10 - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(l + screenHeight / 100 * 25 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                    //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                    //score += 1;
                    //onScoreListener.onScore(score);
                    Log.d("missile", "missile hit! ");
                    o=-200;
                }


                canvas.drawText("o", i[i1], (float) (screenHeight * 0.61), paint);
                canvas.drawText("o", 185, screenHeight / 100 * 95 - i[i1] - xbuggy2, paint);




                if (i1 == numberOfshots - 1 && i[i1] > screenWidth) {
                    if (numberOfshots > 0) numberOfshots--;
                    if (ind > 0) ind--;
                }
            }
            if (System.currentTimeMillis() - lastTurn >= 2000) {
                // Change direction here
                down = !down;
                lastTurn = System.currentTimeMillis();
            }

            if (System.currentTimeMillis() - lastTurn2 >= 7000) {
                // Change direction here
                down2 = !down2;
                lastTurn2 = System.currentTimeMillis();
            }

            canvas.drawBitmap(alienbitmap, j, k +150+ screenHeight / 100 * 45, paint);
            canvas.drawBitmap(alienbitmap2, o + 10, l + screenHeight / 100 * 25, paint);
            //canvas.drawBitmap(alienbitmap3, j+20, k+screenHeight / 100 * 5, paint);
            drawBackground(1);
            canvas.drawBitmap(bitmap, 50, (float) (screenHeight * 0.5) - xbuggy, paint);
            // Draw the foreground parallax

            for (int n = 0; n < numberOfshots; n++)
                i[n] = i[n] + 20;

            j = j + 10;
            o = o + 7;
            if (!down)
                k=k+2;
            else
                k=k-2;

            if (!down2)
                l++;
            else
                l--;

            // Unlock and draw the scene
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }

    // Clean up our thread if the game is stopped
    public void pause() {
        running = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) {
            // Error
        }
    }

    // Make a new thread and start it
    // Execution moves to our run method
    public void resume() {
        running = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    private void drawBackground(int position) {

        // Make a copy of the relevant background
        Background bg = backgrounds.get(position);

        // define what portion of images to capture and
        // what coordinates of screen to draw them at

        // For the regular bitmap
        Rect fromRect1 = new Rect(0, 0, bg.width - bg.xClip, bg.height);
        Rect toRect1 = new Rect(bg.xClip, bg.startY, bg.width, bg.endY);

        // For the reversed background
        Rect fromRect2 = new Rect(bg.width - bg.xClip, 0, bg.width, bg.height);
        Rect toRect2 = new Rect(0, bg.startY, bg.xClip, bg.endY);

        //draw the two background bitmaps
        if (!bg.reversedFirst) {
            canvas.drawBitmap(bg.bitmap, fromRect1, toRect1, paint);
            canvas.drawBitmap(bg.bitmapReversed, fromRect2, toRect2, paint);
        } else {
            canvas.drawBitmap(bg.bitmap, fromRect2, toRect2, paint);
            canvas.drawBitmap(bg.bitmapReversed, fromRect1, toRect1, paint);
        }
    }

    // Because we call this from onTouchEvent, this code will be executed for both
    // normal touch events and for when the system calls this using Accessibility
    @Override
    public boolean performClick() {
        super.performClick();
        launchMissile();
        return true;
    }

    private void launchMissile() {
        i[ind] = 350;
        ind++;
        xbuggy2 = 0;
        shoot = true;
    }

    // event listener for when the user touches the screen
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean gameOver = false;
        //if (paused) {
        //   paused = false;
        //}
        int action = MotionEventCompat.getActionMasked(event);
        int coordX = (int) event.getX();
        int coordY = (int) event.getY();
        Log.d("coordY", "coordY " + coordY);
        if (coordX < 220 && xbuggy == 0 && action == MotionEvent.ACTION_MOVE) {
            jump = true;
            shoot = false;
            lastTurn3 = System.currentTimeMillis();
            return true; // do nothing
        }

        if (coordX > 219 && action == MotionEvent.ACTION_DOWN) {
            numberOfshots++;
            performClick();
            return true;
        }
        return true;
    }
}

更新资料

我已经开始根据以下内容封装外星人的逻辑。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Alien {
    public Alien(){}
    public Alien(Context context, String name) {
        setAlienResID(context.getResources().getIdentifier("object3_hdpi",
                "drawable", context.getPackageName()));
        setAlienbitmap(BitmapFactory.decodeResource(context.getResources(), this.getAlienResID()));
    }
    public int getAlienResID() {
        return alienResID;
    }

    public void setAlienResID(int alienResID) {
        this.alienResID = alienResID;
    }

    public Bitmap getAlienbitmap() {
        return alienbitmap;
    }

    public void setAlienbitmap(Bitmap alienbitmap) {
        this.alienbitmap = alienbitmap;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    int alienResID;
    Bitmap alienbitmap;
    int width;
    int height;
}


public class AttackingAlien extends Alien {
    public AttackingAlien(Context context, String name) {
        super(context, name);
    }
}

更新2

我改变了策略。现在我正在画一艘将轰炸月球车的宇宙飞船。

java - 改善外星人的行动-LMLPHP

相关代码是
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.Rect;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

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

public class ParallaxView extends SurfaceView implements Runnable {

    List<Background> backgrounds;

    private volatile boolean running;
    private Thread gameThread = null;

    // For drawing
    private Paint paint;
    private Canvas canvas;
    private SurfaceHolder ourHolder;

    // Holds a reference to the Activity
    Context context;

    // Control the fps
    long fps = 60;

    // Screen resolution
    int screenWidth;
    int screenHeight;

    private void update() {
        // Update all the background positions
        for (Background bg : backgrounds) {
            bg.update(fps);
        }

    }

    ParallaxView(Context context, int screenWidth, int screenHeight) {
        super(context);

        this.context = context;

        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight;

        // Initialize our drawing objects
        ourHolder = getHolder();
        paint = new Paint();

        // Initialize our array list
        backgrounds = new ArrayList<>();

        //load the background data into the Background objects and
        // place them in our GameObject arraylist

        backgrounds.add(new Background(
                this.context,
                screenWidth,
                screenHeight,
                "bg", 0, 120, 50));

        backgrounds.add(new Background(
                this.context,
                screenWidth,
                screenHeight,
                "grass", 70, 110, 200));

        // Add more backgrounds here

    }

    @Override
    public void run() {

        while (running) {
            long startFrameTime = System.currentTimeMillis();

            update();
            if (j > 2000) {
                j = -50;
                k = 0;
            }
            if (o > 2000) {
                o = -50;
                l = 0;
            }
            draw();

            // Calculate the fps this frame
            long timeThisFrame = System.currentTimeMillis() - startFrameTime;
            if (timeThisFrame >= 1) {
                fps = 1000 / timeThisFrame;
            }
        }
    }

    int numberOfshots = 1;
    int[] i = new int[200];
    int j = 0;
    int k = 0;
    int l = 0;
    int m = 0;
    int o = 0;
    boolean down = true;
    long lastTurn = System.currentTimeMillis();
    int xbuggy = 0;
    int xbuggy2 = 0;
    boolean down2 = true;
    long lastTurn2 = System.currentTimeMillis();
    long lastTurn3 = System.currentTimeMillis();
    long lastTurn4 = System.currentTimeMillis();
    boolean jump = false;
    boolean shoot = false;
    int ind = 0;
    int numberOfAlienshots = 1;
    int missileOffSetY = 0;
    private void draw() {

        if (ourHolder.getSurface().isValid()) {
            //First we lock the area of memory we will be drawing to
            canvas = ourHolder.lockCanvas();
            if (jump) {
                xbuggy = xbuggy + 4;
            }
            if (shoot) {
                xbuggy2 = xbuggy2 + 4;
            }



            if (System.currentTimeMillis() - lastTurn4 >= 2000) {
                // Change direction here
               //jump = false;
                lastTurn4 = System.currentTimeMillis();
                missileOffSetY = 0;
            }

            if (System.currentTimeMillis() - lastTurn3 >= 1000) {
                // Change direction here
                jump = false;
                lastTurn3 = System.currentTimeMillis();
                xbuggy = 0;
            }
            //draw a background color
            canvas.drawColor(Color.argb(255, 0, 0, 0));

            // Draw the background parallax
            drawBackground(0);

            // Draw the rest of the game
            paint.setTextSize(60);
            paint.setColor(Color.argb(255, 255, 255, 255));

            //canvas.drawText("MOONPATROL3000", 350, screenHeight / 100 * 5, paint);

            int resID = context.getResources().getIdentifier("vehicle",
                    "drawable", context.getPackageName());

            Alien alien1 = new AttackingAlien(context, "right_side_hdpi");
            Alien alien2 = new AttackingAlien(context, "object2_hdpi");
            Alien alien3 = new AttackingAlien(context, "object1_hdpi");

            int alienResID = context.getResources().getIdentifier("right_side_hdpi",
                    "drawable", context.getPackageName());

            int alienResID2 = context.getResources().getIdentifier("right_side_hdpi",
                    "drawable", context.getPackageName());

            int alienResID3 = context.getResources().getIdentifier("right_side_hdpi",
                    "drawable", context.getPackageName());

            // Load the bitmap using the id
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
            Bitmap alienbitmap = BitmapFactory.decodeResource(context.getResources(), alienResID);
            Bitmap alienbitmap2 = BitmapFactory.decodeResource(context.getResources(), alienResID2);
            Bitmap alienbitmap3 = BitmapFactory.decodeResource(context.getResources(), alienResID3);

            //paint.setTextSize(220);

            //for (int i1 = 0; i1 < numberOfAlienshots; i1++) {
            if (missileOffSetY < 300) {
                canvas.drawText("|", o + 10 + alienbitmap2.getWidth() / 2, l + screenHeight / 100 * 25 + 75 + missileOffSetY, paint);

                missileOffSetY = missileOffSetY + 10;
            }

            for (int i1 = 0; i1 < numberOfshots; i1++) {


                // if horizontal missile hits alien 0
                if (java.lang.Math.abs(j - i[i1]) * 2  < (alien1.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alien1.getHeight() + 60)) {
                    //y1[i2] = -random.nextInt(1000); // reset to new vertical position
                    //score += 1;
                    //onScoreListener.onScore(score);
                    Log.d("missile", "missile hit! ");
                    j=-200;
                }

                // if vertical missile hits alien 0
                if (java.lang.Math.abs(j - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(j + 150 + screenHeight / 100 * 45 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                    j=-200;
                }


                // if horizontal missile hits alien 1, right now this won't happen
                if (java.lang.Math.abs(j - i[i1]) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(k +150+ screenHeight / 100 * 45 - (float) (screenHeight * 0.61)) * 2  < (alienbitmap.getHeight() + 60)) {
                    j=-200;
                }

                // if vertical missile hits alien 1
                if (java.lang.Math.abs(o + 10 - 185) * 2  < (alienbitmap.getWidth() + 60) && java.lang.Math.abs(l + screenHeight / 100 * 25 - (screenHeight / 100 * 95 - i[i1] - xbuggy2)) * 2  < (alienbitmap.getHeight() + 60)) {
                    o=-200;
                }


                canvas.drawText("o", i[i1], (float) (screenHeight * 0.61), paint);
                canvas.drawText("o", 185, screenHeight / 100 * 95 - i[i1] - xbuggy2, paint);




                if (i1 == numberOfshots - 1 && i[i1] > screenWidth) {
                    if (numberOfshots > 0) numberOfshots--;
                    if (ind > 0) ind--;
                }
            }
            if (System.currentTimeMillis() - lastTurn >= 2000) {
                // Change direction here
                down = !down;
                lastTurn = System.currentTimeMillis();
            }

            if (System.currentTimeMillis() - lastTurn2 >= 7000) {
                // Change direction here
                down2 = !down2;
                lastTurn2 = System.currentTimeMillis();
            }

          //  canvas.drawBitmap(alien1.getAlienbitmap(), j, k +150+ screenHeight / 100 * 45, paint);
            canvas.drawBitmap(alienbitmap2, o + 10, l + screenHeight / 100 * 25, paint);
            //canvas.drawBitmap(alienbitmap3, j+20, k+screenHeight / 100 * 5, paint);
            drawBackground(1);
            canvas.drawBitmap(bitmap, 50, (float) (screenHeight * 0.5) - xbuggy, paint);
            // Draw the foreground parallax

            for (int n = 0; n < numberOfshots; n++)
                i[n] = i[n] + 20;

            j = j + 10;
            o = o + 7;
            if (!down)
                k=k+2;
            else
                k=k-2;

            if (!down2)
                l++;
            else
                l--;

            // Unlock and draw the scene
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }

    // Clean up our thread if the game is stopped
    public void pause() {
        running = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) {
            // Error
        }
    }

    // Make a new thread and start it
    // Execution moves to our run method
    public void resume() {
        running = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    private void drawBackground(int position) {

        // Make a copy of the relevant background
        Background bg = backgrounds.get(position);

        // define what portion of images to capture and
        // what coordinates of screen to draw them at

        // For the regular bitmap
        Rect fromRect1 = new Rect(0, 0, bg.width - bg.xClip, bg.height);
        Rect toRect1 = new Rect(bg.xClip, bg.startY, bg.width, bg.endY);

        // For the reversed background
        Rect fromRect2 = new Rect(bg.width - bg.xClip, 0, bg.width, bg.height);
        Rect toRect2 = new Rect(0, bg.startY, bg.xClip, bg.endY);

        //draw the two background bitmaps
        if (!bg.reversedFirst) {
            canvas.drawBitmap(bg.bitmap, fromRect1, toRect1, paint);
            canvas.drawBitmap(bg.bitmapReversed, fromRect2, toRect2, paint);
        } else {
            canvas.drawBitmap(bg.bitmap, fromRect2, toRect2, paint);
            canvas.drawBitmap(bg.bitmapReversed, fromRect1, toRect1, paint);
        }
    }

    // Because we call this from onTouchEvent, this code will be executed for both
    // normal touch events and for when the system calls this using Accessibility
    @Override
    public boolean performClick() {
        super.performClick();
        launchMissile();
        return true;
    }

    private void launchMissile() {
        i[ind] = 350; // what does it do?
        ind++;
        xbuggy2 = 0;
        shoot = true;
    }

    // event listener for when the user touches the screen
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean gameOver = false;
        //if (paused) {
        //   paused = false;
        //}
        int action = MotionEventCompat.getActionMasked(event);
        int coordX = (int) event.getX();
        int coordY = (int) event.getY();
        Log.d("coordY", "coordY " + coordY);
        if (coordX < 220 && xbuggy == 0 && action == MotionEvent.ACTION_MOVE) {
            jump = true;
            shoot = false;
            lastTurn3 = System.currentTimeMillis();
            return true; // do nothing
        }

        if (coordX > 219 && action == MotionEvent.ACTION_DOWN) {
            numberOfshots++;
            performClick();
            return true;
        }
        return true;
    }
}

最佳答案

您最大的错误似乎是在绘制例程中分配了4个位图。在onCreate中分配这些位图,并仅调用初始化onCreate()的全局位图。那将解决您的问题。您可以在它们的位置绘制它们。

    private void draw() {
        Alien alien1 = new AttackingAlien(context, "right_side_hdpi");
        Alien alien2 = new AttackingAlien(context, "object2_hdpi");
        Alien alien3 = new AttackingAlien(context, "object1_hdpi");

您分配了一堆调用上下文的内存对象,并扩展了drawable和一堆其他工作。您可能刚刚使用了最后一刻的相同外星人。
        int alienResID = context.getResources().getIdentifier("right_side_hdpi",
                "drawable", context.getPackageName());

        int alienResID2 = context.getResources().getIdentifier("right_side_hdpi",
                "drawable", context.getPackageName());

        int alienResID3 = context.getResources().getIdentifier("right_side_hdpi",
                "drawable", context.getPackageName());

外星人的ID与上一个滴答滴答声没有变化。
        // Load the bitmap using the id
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resID);
        Bitmap alienbitmap = BitmapFactory.decodeResource(context.getResources(), alienResID);
        Bitmap alienbitmap2 = BitmapFactory.decodeResource(context.getResources(), alienResID2);
        Bitmap alienbitmap3 = BitmapFactory.decodeResource(context.getResources(), alienResID3);

这些是来自最后一个刻度的相同位图,并且位图很大,并且从资源中获取它们的速度很慢,而您正在做每个刻度。
    }
}

其他大多数东西在这里和那里都会缩短半毫秒,但这可能会让您进入正确的FPS球场。

即使您还有其他人也不要担心。

通过使例程更快,可以解决您的大部分问题。现在是个好时机,您做错了。通常的正确方法是每17毫秒左右就有一个循环滴答声。其余时间将暂停。有些错误很明显。

您最大的错误似乎是在绘制例程中分配了4个位图。

但是,绘制例程只能绘制。您在 Canvas 上绘制了需要发生的事情,仅此而已。您不会分配任何东西,也不会给任何东西充气,您会获得东西所在位置的数字,然后绘制该位置已加载到内存中的东西。

您正在绘制例程中进行滴答并进行碰撞检测,并为此分配了一堆对象,而无论如何它们都必须在一秒钟内被扔到刨木机中。

在初始化之外或存在新外星人的特殊情况下,请勿创建任何对象。您不应在绘制例程中的任何地方使用"new"符号。曾经

您正在使用蛮力进行碰撞检测,最终却没有。找到一个喜欢的不错的加速结构并使用它。对于1个对象,这无关紧要。

不要称呼外星人的类,虽然看起来更漂亮,但您想要外星人的边界框的原始数字。然后,您希望将它们保持在某种结构中,从而可以非常快速地引用它们(您需要不到17ms的帧)。调用一堆宽度命令实际上并没有太大帮助,即使它们更改了大小也只是更改了Hitbox的数量。这些方法为您提供了一些不错的数据结构,例如具有一系列按顺序排列的匹配框,您可以对它们进行二进制搜索,并通过对结构进行log(n)更新来查找被移动的对象是否在log(n)时间内击中了对象,或者遍历“轴对齐的边界框树”的一些方法。这是您最终需要的东西,但是只要您保持简单,它就可以工作。不过,实际上,这只是您的位图在执行大部分减慢操作。

还有很多其他基本问题,例如将边界框放在if语句内,而不是制作两个其他矩形。但是还有其他问题,例如完全制作矩形!您可以使用实际位置来调用绘图,而不是使用一些分配大对象的对象来调用函数。只需使用数字调用该函数即可。

您应该有一个例程,该例程根据物料的位置为您绘制图形。它应该能够在不到17毫秒的时间内绘制出所需绘制的所有内容。如果不能,您将永远无法达到60fps。因此,在这种情况下,减少工作量并做得更好。该空间背景是否需要为位图?您能为天空画一些点,然后相应地调整图形吗?绘制例程从不分配任何东西。期。如果您需要分配一些东西,它应该已经在初始化期间进行了。分配是您生存的祸根。

您的触摸会更新内容的位置。 AI/Physics勾号还会更新事物的位置并检查是否存在合谋。绘制仅根据内存中的位置和内容绘制内容。

在其自己的线程中运行更新位置刻度。您只需要处理将要读取和写入相同数据的并发位。它只需要同步绘制数据的变化数据读取,以便将这些部分(触摸中的位置更新,刻度中的位置更新,以及绘制例程本身的位置)扔到同一个对象的同步块(synchronized block)中。

07-27 22:40