我不能用windGame方法用帆布在Game中显示Dialog

时间:2017-09-29 02:42:22

标签: java android dialog surfaceview

MainActivity

public class MainActivity extends StartGame {
    private Player player;
    GamePanel gamePanel;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(new GamePanel(this));

    }
}

GamePanel源代码:

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {

    public static final int WIDTH = 1280;
    public static final int HEGHT = 640;
    public static int MOVESPEED = -5;
    public MainThread thread;
    public Background bg;
    private Player player;
    private ArrayList<SmokePuff> smoke;
    public ArrayList<Missile> missiles;
    public ArrayList<TopBorder> topBorder;
    public ArrayList<BotBorder> botBorder;
    public ArrayList<Carrot> carrots;
    public ArrayList<Heart> hearts;
    private long HeartStartTime;
    private long SmokeStartTime;
    private long MissileStartTime;
    private long CarrotStartTime;
    private int maxBorderHeight;
    private int minBorderHeight;
    private boolean topDown = true;
    private boolean botDown = true;
    public boolean newGameCreated;
    public boolean newGameCreated1;
    private long MissileElapsed;
    private long CarrotElapsed;
    private long HeartElapse;
    DataBase mydb;
    public int life = 3;

    private int progressDenom = 20;

    private Explosion explosion;
    private long StartReset;
    public boolean reset;
    private boolean dissapear;
    private boolean started;
    private static int best;
    private Random rand = new Random();
    public static AlertDialog.Builder dialog;
    public static Context context1;
    public   Runnable runnable;


    public GamePanel(Context context) {
        super(context);

        mydb = new DataBase(context);

        setFocusable(true);

        Cursor res = mydb.getAll1();
        if (res.getCount() > 0) {
            res.getCount();

            res.moveToLast();
        }

context1 = context;

LayoutInflater li = LayoutInflater.from(context1);
final View myView = li.inflate(R.layout.wingame, null);

    runnable = new Runnable() {
        @Override
        public void run() {
            dialog = new AlertDialog.Builder(context1);
            dialog.setView(myView);
            dialog.create().show();


            myView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    intent.setClass(context1,StagesActivity.class);
                   context1.startActivity(intent);
                }
            });


        }
};


    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        int counter = 0;

        while (retry && counter < 1000) {
            counter++;
            try {
                thread.setRunning(false);
                thread.join();
                retry = false;
                thread = null;

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)

    {

        if (StagesActivity.FLAG2 == 1) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas));
        }
        if (StagesActivity.FLAG2 == 2) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas1));
        }
        if (StagesActivity.FLAG2 == 3) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas2));
        }
        if (StagesActivity.FLAG2 == 4) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas3));
        }
        if (StagesActivity.FLAG2 == 5) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas4));
        }
        if (StagesActivity.FLAG2 == 6) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas5));
        }
        if (StagesActivity.FLAG2 == 7) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas6));
        }
        if (StagesActivity.FLAG2 == 8) {
            bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.canvas7));
        }

        player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.rabbit2), 62, 73, 8);
        carrots = new ArrayList<Carrot>();

        smoke = new ArrayList<SmokePuff>();
        missiles = new ArrayList<Missile>();
        topBorder = new ArrayList<TopBorder>();
        botBorder = new ArrayList<BotBorder>();
        hearts = new ArrayList<Heart>();

        SmokeStartTime = System.nanoTime();
        MissileStartTime = System.nanoTime();
        CarrotStartTime = System.nanoTime();
        HeartStartTime = System.nanoTime();
        thread = new MainThread(getHolder(), this);

        thread.setRunning(true);
        thread.start();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (!player.getPlaying() && newGameCreated && reset) {
                player.setUp(true);
                player.setPlaying(true);

            }
            if (player.getPlaying()) {
                if (!started)
                    started = true;
                reset = false;
                player.setUp(true);
            }
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            player.setUp(false);
            return true;
        }

        return super.onTouchEvent(event);

    }

    public void update() {

        if (player.getPlaying()) {
            if (topBorder.isEmpty()) {
                player.setPlaying(false);
                return;
            }

            if (botBorder.isEmpty()) {
                player.setPlaying(false);
                return;
            }

            bg.update();
            player.update();

            // calculate the thretshold of height the borders can have based on the score
            // max and min border heart are update, and the border switched direction when
            // either max or
            // min is met

            maxBorderHeight = 30 + player.getScore() / progressDenom;

            // cap max border height so that borders can only take up a total of 1/2 of
            // screen
            if (maxBorderHeight > HEGHT / 4)
                maxBorderHeight = HEGHT / 4;
            minBorderHeight = 5 + player.getScore() / progressDenom;

            // check bottom borders collision
            for (int i = 0; i < botBorder.size(); i++) {
                if (collision(botBorder.get(i), player))
                    player.setPlaying(false);
            }

            // check top borders collision
            for (int i = 0; i < topBorder.size(); i++) {
                if (collision(topBorder.get(i), player))
                    player.setPlaying(false);
            }

            // update top borders
            this.updateTopBorder();

            // update bot borders
            this.updateBottomBorder();

            // add missile on timer
            // add carrot on timer and stage

            if (StagesActivity.FLAG2 == 1) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 2000000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 4000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 8000000;
            }
            if (StagesActivity.FLAG2 == 2) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 1700000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 4000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 7000000;
            }
            if (StagesActivity.FLAG2 == 3) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 1300000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 3000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 6000000;
            }
            if (StagesActivity.FLAG2 == 4) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 1100000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 3000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 6000000;
            }
            if (StagesActivity.FLAG2 == 5) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 1000000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 3000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 6000000;
            }
            if (StagesActivity.FLAG2 == 6) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 800000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 4000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 5000000;
            }
            if (StagesActivity.FLAG2 == 7) {
                MissileElapsed = (System.nanoTime() - MissileStartTime) / 700000;
                CarrotElapsed = (System.nanoTime() - CarrotStartTime) / 3000000;
                HeartElapse = (System.nanoTime() - HeartStartTime) / 4000000;
            }

            if (MissileElapsed > (2000 - player.getScore() / 4)) {
                System.out.println("making missile");

                if (StagesActivity.FLAG2 == 1) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            60, 51, player.getScore(), 3));

                }
                if (StagesActivity.FLAG2 == 2) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile1),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            67, 50, player.getScore(), 3));

                }
                if (StagesActivity.FLAG2 == 3) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile2),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            83, 70, player.getScore(), 3));
                }
                if (StagesActivity.FLAG2 == 4) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile3),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            79, 56, player.getScore(), 11));

                }
                if (StagesActivity.FLAG2 == 5) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile4),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            122, 71, player.getScore(), 4));
                }
                if (StagesActivity.FLAG2 == 6) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile5),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            81, 83, player.getScore(), 4));
                }
                if (StagesActivity.FLAG2 == 7) {
                    missiles.add(new Missile(BitmapFactory.decodeResource(getResources(), R.drawable.missile6),
                            WIDTH + 10, (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight),
                            75, 77, player.getScore(), 4));
                }

                MissileStartTime = System.nanoTime();

            }
            if (CarrotElapsed > (2000 - player.getScore() / 4)) {
                carrots.add(new Carrot(BitmapFactory.decodeResource(getResources(), R.drawable.carrot), WIDTH + 10,
                        (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight), 59, 76,
                        player.getScore(), 5));
                // reset timer
                CarrotStartTime = System.nanoTime();
            }
            if (HeartElapse > (2000 - player.getScore() / 4)) {
                hearts.add(new Heart(BitmapFactory.decodeResource(getResources(), R.drawable.heart), WIDTH + 10,
                        (int) (rand.nextDouble() * (HEGHT - (maxBorderHeight * 2)) + maxBorderHeight), 52, 40,
                        player.getScore(), 2));
                // reset timer
                HeartStartTime = System.nanoTime();
            }

            collision1();
            collision2();
            collision3();

            // add smokepufs on timer
            long elapsed = (System.nanoTime() - SmokeStartTime) / 1000000;
            if (elapsed > 120) {
                smoke.add(new SmokePuff(player.getX(), player.getY() + 10));
                SmokeStartTime = System.nanoTime();
            }
            for (int i = 0; i < smoke.size(); i++) {
                smoke.get(i).update();
                if (smoke.get(i).getX() < -10) {
                    smoke.remove(i);
                }
            }
        }

        else {
            player.resetDY();
            if (!reset) {
                newGameCreated = false;
                StartReset = System.nanoTime();
                reset = true;
                dissapear = true;
                explosion = new Explosion(BitmapFactory.decodeResource(getResources(), R.drawable.explosion),
                        player.getX(), player.getY() + 0, 96, 96, 15);

            }
            explosion.update();

            long resetElapse = (System.nanoTime() - StartReset) / 1000000;

            if (resetElapse > 2500 && !newGameCreated) {

                newGame();

            }
        }

        if (StagesActivity.FLAG2 == 1 && player.getScore() >= 20 && StagesActivity.getScore < 20) {

            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }
        if (StagesActivity.FLAG2 == 2 && player.getScore() >= 600 && StagesActivity.getScore < 600) {
            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }
        if (StagesActivity.FLAG2 == 3 && player.getScore() >= 750 && StagesActivity.getScore < 750) {
            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }
        if (StagesActivity.FLAG2 == 4 && player.getScore() >= 800 && StagesActivity.getScore < 800) {
            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }
        if (StagesActivity.FLAG2 == 5 && player.getScore() >= 900 && StagesActivity.getScore < 900) {
            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }
        if (StagesActivity.FLAG2 == 6 && player.getScore() >= 1000 && StagesActivity.getScore < 1000) {
            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }
        if (StagesActivity.FLAG2 == 7 && player.getScore() >= 1100 && StagesActivity.getScore < 1100) {
            MissileStartTime = System.nanoTime();
            player.setPlaying(false);
            winGame();

        }

    }

    public boolean collision1() {
        for (int i = 0; i < carrots.size(); i++) {
            carrots.get(i).update();

            if (Rect.intersects(carrots.get(i).getRectangle(), player.getRectangle())) {
                carrots.remove(i);
                player.score = player.score + 30;
                MOVESPEED--;

                return true;

            }
        }
        return false;
    }

    public boolean collision2() {
        for (int i = 0; i < missiles.size(); i++) {
            missiles.get(i).update();

            if (Rect.intersects(missiles.get(i).getRectangle(), player.getRectangle())) {
                missiles.remove(i);

                if (life > 0)
                    life--;
                if (life == 0)
                    player.setPlaying(false);
                return true;

            }
        }
        return false;
    }

    public boolean collision3() {
        for (int i = 0; i < hearts.size(); i++) {
            hearts.get(i).update();

            if (Rect.intersects(hearts.get(i).getRectangle(), player.getRectangle())) {
                hearts.remove(i);

                if (life < 5) {
                    life++;

                }
                return true;

            }
        }
        return false;
    }

    public boolean collision(GamoObject a, GamoObject b) {

        if (Rect.intersects(a.getRectangle(), b.getRectangle())) {
            return true;
        }
        return false;
    }

    @Override
    public void draw(Canvas canvas) {

        final float scaleFactorX = getWidth() / (WIDTH * 1.f);
        final float scaleFactorY = getHeight() / (HEGHT * 1.f);
        if (canvas != null) {
            final int saveState = canvas.save();
            canvas.scale(scaleFactorX, scaleFactorY);
            bg.draw(canvas);
            if (!dissapear) {
                player.draw(canvas);
            }
            // draw smoke puff
            for (SmokePuff sp : smoke) {

                sp.draw(canvas);
            }
            // draw missile
            for (Missile m : missiles) {
                m.draw(canvas);
            }
            for (Carrot c : carrots) {
                c.draw(canvas);
            }
            for (Heart h : hearts) {
                h.draw(canvas);
            }

            // draw topBorder
            for (TopBorder tb : topBorder) {
                tb.draw(canvas);
            }

            // draw botBorder
            for (BotBorder bb : botBorder) {
                bb.draw(canvas);
            }
            // draw expolosion
            if (started) {
                explosion.draw(canvas);
            }

            drawText(canvas);
            canvas.restoreToCount(saveState);

        }

    }

    public void updateTopBorder() {
        // every 50 points,insert randomly place top block that break the patterns
        if (player.getScore() % 40 == 0) {
            topBorder.add(new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks),
                    topBorder.get(topBorder.size() - 1).getX() + 20, 0,
                    (int) ((rand.nextDouble() * (maxBorderHeight - 10)) + 1)));
        }
        for (int i = 0; i < topBorder.size(); i++) {
            topBorder.get(i).update();
            if (topBorder.get(i).getX() < -60) {
                topBorder.remove(i);
                // remove element of arraylist, perplace it by adding a new one

                // calculate topdown witch determines the direction the borders is moveing(upor
                // dwown)
                if (topBorder.get(topBorder.size() - 1).getHeight() >= maxBorderHeight) {
                    topDown = false;
                }
                if (topBorder.get(topBorder.size() - 1).getHeight() <= minBorderHeight) {
                    topDown = true;
                }
                // new borders added will have larger height
                if (topDown) {
                    topBorder.add(new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks),
                            topBorder.get(topBorder.size() - 1).getX() + 20, 0,
                            topBorder.get(topBorder.size() - 1).getHeight() + 1));
                }
                // new borders added will have smaller height
                else {
                    topBorder.add(new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks),
                            topBorder.get(topBorder.size() - 1).getX() + 20, 0,
                            topBorder.get(topBorder.size() - 1).getHeight() - 1));
                }
            }
        }

    }

    public void updateBottomBorder() {
        // every 40 points,insert randomly place top block that break the patterns
        if (player.getScore() % 40 == 0) {
            botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks),
                    botBorder.get(botBorder.size() - 1).getX() + 20,
                    (int) (rand.nextDouble() * maxBorderHeight + 10) + (HEGHT - maxBorderHeight)));
        }
        // update bottom border
        for (int i = 0; i < botBorder.size(); i++) {
            botBorder.get(i).update();
            // if border moving on screen, removind and add a correspondin a new one
            if (botBorder.get(i).getX() < -60) {
                botBorder.remove(i);

                // ditermine if border will be moving up or down
                if (botBorder.get(botBorder.size() - 1).getY() <= HEGHT - maxBorderHeight) {
                    botDown = false;
                }
                if (botBorder.get(botBorder.size() - 1).getY() >= HEGHT - minBorderHeight) {
                    botDown = true;
                }
                if (botDown) {
                    botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks),
                            botBorder.get(botBorder.size() - 1).getX() + 20,
                            botBorder.get(botBorder.size() - 1).getY() + 1));
                } else {
                    botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks),
                            botBorder.get(botBorder.size() - 1).getX() + 20,
                            botBorder.get(botBorder.size() - 1).getY() - 1));

                }

            }
        }

    }

    public void newGame() {
        if (player.getScore() > best) {
            mydb.data1("" + player.getScore());
        }

        try {
            best = NumberFormat.getInstance().parse(mydb.string1).intValue();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        player.setY(HEGHT / 2);
        if (player.getScore() > best) {
            best = player.getScore();
        }
        dissapear = false;
        botBorder.clear();
        topBorder.clear();
        missiles.clear();
        smoke.clear();
        carrots.clear();

        minBorderHeight = 5;
        maxBorderHeight = 30;
        player.resetDY();
        player.resetScore();
        MOVESPEED = -5;
        life = 3;

        player.setY(HEGHT / 2);
        if (player.getScore() > best) {
            best = player.getScore();
        }

        for (int i = 0; i * 20 < WIDTH + 40; i++) {
            // first border create
            if (i == 0) {
                topBorder.add(
                        new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20, 0, 10));
            } else {
                topBorder.add(new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20, 0,
                        topBorder.get(i - 1).getHeight() + 1));
            }
        }

        for (int i = 0; i * 20 < WIDTH + 40; i++) {
            // first border ever created
            if (i == 0) {
                botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20,
                        HEGHT - maxBorderHeight));
            }
            // adding borders until the initial screen isfiles
            else {
                botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20,
                        botBorder.get(i - 1).getY() - 1));

            }

        }

        newGameCreated = true;

    }
}

这里调用对话框。但游戏崩溃了!

public void winGame(){
 runnable.run(); // the dialog !
    if (player.getScore() > best){
        mydb.data1(""+player.getScore());
        mydb.data(""+player.getScore());

    }else if (player.getScore() < best){
        mydb.data(""+player.getScore());
    }

    try {
        best = NumberFormat.getInstance().parse(mydb.string1).intValue();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    player.setY(HEGHT / 2);
    if (player.getScore() > best) {
        best = player.getScore();
    }

    dissapear = false;
    botBorder.clear();
    topBorder.clear();
    missiles.clear();
    smoke.clear();

    minBorderHeight = 5;
    maxBorderHeight = 30;
    player.resetDY();
    player.resetScore();
    MOVESPEED = - 5;
    life = 3;

    player.setY(HEGHT / 2);
    if (player.getScore() > best) {
        best = player.getScore();
    }

    //creating initial borders

    //initial top border
    for (int i = 0; i * 20 < WIDTH + 40; i++) {
        //first border create
        if (i == 0) {
            topBorder.add(new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20, 0, 10));
        } else {
            topBorder.add(new TopBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20, 0,
                    topBorder.get(i - 1).getHeight() + 1));
        }
    }


    //initial bot borders
    for (int i = 0; i * 20 < WIDTH + 40; i++) {
        // first border ever created
        if (i == 0) {
            botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20, HEGHT - maxBorderHeight));
        }
        // adding borders until the initial screen isfiles
        else {
            botBorder.add(new BotBorder(BitmapFactory.decodeResource(getResources(), R.drawable.bricks), i * 20, botBorder.get(i - 1).getY() - 1));

        }


    }

    newGameCreated = true;
    newGameCreated1 = true;


}

public void drawText(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(30);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    canvas.drawText("DISTANCE:  " + (player.getScore() * 3), 10, HEGHT - 10, paint);
    canvas.drawText("BEST:  " + best * 3, WIDTH - 215, HEGHT - 10, paint);
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.life1) , 10 , HEGHT - 70,paint  );
    canvas.drawText("" + life, 80 , HEGHT -40,paint );
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fart),WIDTH - 260, HEGHT - 80,paint);
    int speed = MOVESPEED - MOVESPEED - MOVESPEED;
    canvas.drawText("SPEED :" + speed, WIDTH - 215, HEGHT - 40, paint);
}

MainThread类:

public class MainThread extends Thread {

    private int FPS = 30;
    private double averageFPS;
    private SurfaceHolder surfaceHolder;
    Player player;

    private GamePanel gamePanel;
    private boolean running;
    public static Canvas canvas;

    public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel) {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gamePanel = gamePanel;

    }

    @Override
    public void run() {
        long startTime;
        long timeMillis;
        long waitTIme;
        long totalTime = 0;
        int frameCount = 0;
        long targetTime = 1000 / FPS;

        while (running) {
            startTime = System.nanoTime();
            canvas = null;
            // try looking the canvas for pixel edditing
            try {
                canvas = this.surfaceHolder.lockCanvas();

                synchronized (surfaceHolder) {
                    this.gamePanel.update();
                    this.gamePanel.draw(canvas);

                }

            } catch (Exception e) {
            }

            finally {
                if (canvas != null) {
                    try {
                        surfaceHolder.unlockCanvasAndPost(canvas);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            timeMillis = (System.nanoTime() - startTime) / 1000000;
            waitTIme = targetTime - timeMillis;
            try {
                this.sleep(waitTIme);
            } catch (Exception e) {
            }
            totalTime += System.nanoTime() - startTime;
            frameCount++;
            if (frameCount == FPS) {
                averageFPS = 1000 / ((totalTime / frameCount) / 1000000);
                frameCount = 0;
                totalTime = 0;
                System.out.println(averageFPS);
            }
        }
    }

    public void setRunning(boolean b) {
        running = b;

    }

}

我在logcat中没有任何错误!在logcat中(部分代码缓存集合,代码= 29KB,数据= 27KB  I / art:代码缓存收集后,代码= 20KB,数据= 22KB  将代码缓存容量增加到128KB  I / System.out:25.0)

0 个答案:

没有答案