以编程方式绘制计时器文本

时间:2014-08-25 12:47:42

标签: android eclipse timer

这是一个迷宫游戏,球在拖动移动,我有一个问题,我将如何插入计时器,使游戏更令人兴奋。

这是我的代码

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = (w < h) ? w : h;
    height = width; // for now square mazes
    lineWidth = 1; // for now 1 pixel wide walls
    cellWidth = (width - ((float) mazeSizeX * lineWidth)) / mazeSizeX;
    totalCellWidth = cellWidth + lineWidth;
    cellHeight = (height - ((float) mazeSizeY * lineWidth)) / mazeSizeY;
    totalCellHeight = cellHeight + lineWidth;
    textPaint.setTextSize(cellHeight * 0.75f);
    textPaint.setStrokeWidth(1);
    textPaint.setStyle(Style.FILL_AND_STROKE);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Align.LEFT);
    textPaint.setColor(0xA0000000);
    super.onSizeChanged(w, h, oldw, oldh);
}

然后在我的onDraw method

protected void onDraw(Canvas canvas) {
    canvas.drawRect(0, 0, width, height, background);

    boolean[][] hLines = maze.getHorizontalLines();
    boolean[][] vLines = maze.getVerticalLines();
    // iterate over the boolean arrays to draw walls
    for (int i = 0; i < mazeSizeX; i++) {
        for (int j = 0; j < mazeSizeY; j++) {
            float x = j * totalCellWidth;
            float y = i * totalCellHeight;
            if (j < mazeSizeX - 1 && vLines[i][j]) {
                // we'll draw a vertical line
                canvas.drawLine(x + cellWidth, // start X
                        y, // start Y
                        x + cellWidth, // stop X
                        y + cellHeight, // stop Y
                        line);
            }
            if (i < mazeSizeY - 1 && hLines[i][j]) {
                // we'll draw a horizontal line
                canvas.drawLine(x, // startX
                        y + cellHeight, // startY
                        x + cellWidth, // stopX
                        y + cellHeight, // stopY
                        line);
            }
        }
    }
    int currentX = maze.getCurrentX(), currentY = maze.getCurrentY();
    // draw the ball
    canvas.drawCircle((currentX * totalCellWidth) + (cellWidth / 2),
            (currentY * totalCellHeight) + (cellWidth / 2),(cellWidth * 0.45f),
             red);
    // draw timer
    int seconds = timeCounter.getTimeSeconds();
    String text = String.format("%02d:%02d", seconds/60, seconds%60);

    canvas.drawText(text, (mazeFinishX * totalCellWidth)
            + (cellWidth * 0.25f), (mazeFinishY * totalCellHeight)
            + (cellHeight * 0.75f), textPaint);
}

和我的logcat

08-25 22:11:02.977: E/AndroidRuntime(21597): FATAL EXCEPTION: main
08-25 22:11:02.977: E/AndroidRuntime(21597): java.lang.NullPointerException
08-25 22:11:02.977: E/AndroidRuntime(21597):    at com.jforeach.mazegame.GameView.onSizeChanged(GameView.java:82)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.View.setFrame(View.java:14259)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.View.layout(View.java:14171)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.View.layout(View.java:14173)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewGroup.layout(ViewGroup.java:4484)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.View.layout(View.java:14173)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewGroup.layout(ViewGroup.java:4484)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.View.layout(View.java:14173)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewGroup.layout(ViewGroup.java:4484)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2213)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1977)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1166)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5013)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.Choreographer.doFrame(Choreographer.java:548)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.os.Handler.handleCallback(Handler.java:725)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.os.Looper.loop(Looper.java:153)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at android.app.ActivityThread.main(ActivityThread.java:5299)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at java.lang.reflect.Method.invokeNative(Native Method)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at java.lang.reflect.Method.invoke(Method.java:511)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-25 22:11:02.977: E/AndroidRuntime(21597):    at dalvik.system.NativeStart.main(Native Method)

因为我已经添加了计时器,我的游戏力量会在我开始游戏时关闭?谁能给我一些帮助?在此先感谢和无家可归的人。 nullpointer错误位于onSizeChanged method

textPaint.setTextSize(cellHeight * 0.75f);

0 个答案:

没有答案