改善外星人的移动

时间:2018-05-31 02:37:34

标签: java android sprite collision-detection game-physics

enter image description here

我编写了一个受Space Invaders和Moon Patrol启发的迷你Android游戏场景。可以水平射击外星人(见上文)。

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

enter image description here

但是添加外星人并不会缩放",例如,15个外星人在所有可能的碰撞中移动将非常困难。最初的太空入侵者和月球巡逻队解决了这个问题,是否有可能制定出与我使用的战略不同的策略?外星人的确切运动并不重要,只是它很有趣"。

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

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

enter image description here

相关代码是

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;
    }
}

1 个答案:

答案 0 :(得分:2)

您最大的错误似乎是在绘制例程中分配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);

这些是与最后一个刻度相同的位图,位图是巨型的,从资源中获取它们很慢,而且你正在做 EVERY tick。

    }
}

其他大多数东西都会在这里和那里刮掉半个小时,但是这里可能会让你到达正确的FPS球场。

不要担心,尽管你有其他人。

通过更快地完成例程,可以解决大部分问题。现在是时候说,你做错了。这样做的典型和正确方法是每17ms左右循环一次。其余时间暂停了。有些错误很明显。

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

但是,绘制例程绘制。你绘制了画布上需要发生的东西,就是这样。你没有分配任何你不夸大任何东西的东西,你拿出你所拥有的数字,然后把你已经加载的东西绘制到那个位置的内存中。

你正在勾选并在绘图程序中进行碰撞检测,并且当它们必须在一瞬间被抛入木刻器时为此分配一堆对象。

您不应在初始化之外或在存在新外星人的特殊情况下创建任何对象。你不应该在绘图程序中的任何地方使用“新”。如初。

您正在使用蛮力进行碰撞检测,最终不会。找到你喜欢的漂亮加速结构并使用它。对于1个对象,它无关紧要。

不要打电话给一些外星人,虽然它看起来更漂亮,你想要外星人的边界框的原始数字。您希望将它们保留在某种允许非常快速地引用它们的结构中(您需要在不到17ms的时间内完成帧)。调用一堆宽度命令并没有那么有用,即使它们改变了大小只是更改了hitbox的数量。这些方法允许您为数据提供一些不错的结构,例如具有可以二进制搜索它们的已排序的命中箱数组,并查找移动的对象是否在log(n)时间内通过对结构进行log(n)更新来命中对象,或者一些用于遍历轴对齐边界框树的方法。这是你最终需要的东西,但只要你保持简单,它就可以在没有它的情况下工作。但是,实际上只是你的位图在那里做大多数的减速。

还有很多其他基本问题,比如将边界框放在if语句中,而不是另外制作两个矩形。但还有其他问题,比如制作矩形!你使用实际位置调用绘图而不是一些调用一个大的ass对象来调用函数。只需使用数字调用该函数。

你应该有一个例程,根据东西的位置为你做绘图。它应该能够在不到17ms的时间内绘制所需的一切。如果它不能,你将不会达到你需要击中的60fps。因此,在这种情况下,减少一些东西并做得更好。那个空间背景需要是位图吗?你能不能为天空绘制一堆点,并相应地调整图形。你的绘制程序从不分配任何东西。期。如果你需要分配它应该在init期间的东西。分配是你生存的祸根。

你的触摸更新了东西的位置。 AI / Physics勾选还会更新事物的位置并检查串通。抽奖只根据记忆中的位置和内容绘制内容。

在自己的线程中运行更新位置标记。您只需要处理读取和写入相同数据的并发位。它只需要同步绘制数据的变化数据读取,因此在具有相同对象的同步块中抛出这些部分(触摸位置更新,勾选位置更新,以及获取绘图例程本身的位置)。