为什么Log.i在绘图时没有在drawCourt()方法下记录任何内容(位于SquashCourtView类下)?我只能在updateCourt()下看到Log.i结果;方法。我正在尝试记录每一步,以便更好地了解这个简单的游戏如何在后台运行。
package com.packetpub.retrosquash;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.util.Random;
public class MainActivity extends Activity {
Canvas canvas;
SquashCourtView squashCourtView;
//Sound
//initialize sound variables
private SoundPool soundPool;
int sample1 = -1;
int sample2 = -1;
int sample3 = -1;
int sample4 = -1;
//For getting display details like the number of pixels
Display display;
Point size;
int screenWidth;
int screenHeight;
//Game objects
int racketWidth;
int racketHeight;
Point racketPosition;
Point ballPosition;
int ballWidth;
//for ball movement
boolean ballIsMovingLeft;
boolean ballIsMovingRight;
boolean ballIsMovingUp;
boolean ballIsMovingDown;
//for racket movement
boolean racketIsMovingLeft;
boolean racketIsMovingRight;
//stats
long lastFrameTime;
int fps;
int score;
int lives;
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i("Step 1: onCreate ", "");
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
squashCourtView = new SquashCourtView(this);
setContentView(squashCourtView);
//Sound code
soundPool = new SoundPool(10,
AudioManager.STREAM_MUSIC, 0);
Log.i("Step 1: onCreate ", " Loading Music Files");
try {
//Create objects of the 2 required classes
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor;
//create our three fx in memory ready for use
descriptor =
assetManager.openFd("sample1.ogg");
sample1 = soundPool.load(descriptor, 0);
descriptor =
assetManager.openFd("sample2.ogg");
sample2 = soundPool.load(descriptor, 0);
descriptor =
assetManager.openFd("sample3.ogg");
sample3 = soundPool.load(descriptor, 0);
descriptor =
assetManager.openFd("sample4.ogg");
sample4 = soundPool.load(descriptor, 0);
} catch (IOException e) { }
//Could this be an object with getters and setters
//Don't want just anyone changing screen size.
//Get the screen size in pixels
Log.i("Step 1: onCreate ", " Getting Display Size");
display = getWindowManager().getDefaultDisplay();
Log.i("Step 1: onCreate ", " display = " + display);
size = new Point();
display.getSize(size);
Log.i("Step 1: onCreate ", " display.getSize(size) = " + display);
screenWidth = size.x;
Log.i("Step 1: onCreate ", " screenWidth = " + screenWidth);
screenHeight = size.y;
Log.i("Step 1: onCreate ", " screenHeight = " + screenHeight);
//The game objects
racketPosition = new Point();
Log.i("Step 1: onCreate ", " racketPosition = " + racketPosition);
racketPosition.x = screenWidth / 2;
Log.i("Step 1: onCreate ", " racketPosition.x = " + racketPosition.x);
racketPosition.y = screenHeight - 45;
Log.i("Step 1: onCreate ", " racketPosition.y = " + racketPosition.y);
racketWidth = screenWidth / 8;
Log.i("Step 1: onCreate ", " racketWidth = " + racketWidth);
racketHeight = 10;
Log.i("Step 1: onCreate ", " racketHeight = " + racketHeight);
ballWidth = screenWidth / 35;
Log.i("Step 1: onCreate ", " ballWidth = " + ballWidth);
ballPosition = new Point();
Log.i("Step 1: onCreate ", " ballPosition = " + ballPosition);
ballPosition.x = screenWidth / 2;
Log.i("Step 1: onCreate ", " ballPosition.x = " + ballPosition.x);
ballPosition.y = 1 + ballWidth;
Log.i("Step 1: onCreate ", " ballPosition.y = " + ballPosition.y);
lives = 3;
Log.i("Step 1: onCreate ", " lives= " + lives);
}
class SquashCourtView extends SurfaceView implements Runnable
{
Thread ourThread = null;
SurfaceHolder ourHolder;
volatile boolean playingSquash;
Paint paint;
public SquashCourtView(Context context) {
super(context);
Log.i("Step 2/1: SCourtView: ", " SquashCourtView(Context context)");
ourHolder = getHolder();
Log.i("Step 2/1: SCourtView: ", " ourHolder" + ourHolder);
paint = new Paint();
Log.i("Step 2/1: SCourtView: ", " paint = " + paint);
ballIsMovingDown = true;
Log.i("Step 2: SCourtView:", " ballIsMovingDown = " + ballIsMovingDown);
//Send the ball in random direction
Log.i("Step 2/1: SCourtView:", " Send the ball in random direction");
Random randomNumber = new Random();
Log.i("Step 2/1: SCourtView: ", " randomNumber = " + randomNumber);
int ballDirection = randomNumber.nextInt(3);
Log.i("Step 2/1: SCourtView: ", " ballDirection = " + ballDirection);
switch (ballDirection) {
case 0:
Log.i("Step 2: Switch: ", " Case 0");
ballIsMovingLeft = true;
ballIsMovingRight = false;
Log.i("Step 2: Switch: ", " ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2: Switch: ", " ballIsMovingRight = " + ballIsMovingRight);
break;
case 1:
Log.i("Step 2/1: Switch: ", " Case 1");
ballIsMovingRight = true;
ballIsMovingLeft = false;
Log.i("Step 2/1: Switch: ", " ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2/1: Switch: ", " ballIsMovingRight = " + ballIsMovingRight);
break;
case 2:
Log.i("Step 2/1: Switch: ", " Case 2");
ballIsMovingLeft = false;
ballIsMovingRight = false;
Log.i("Step 2/1: Switch: ", " ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2/1: Switch: ", " ballIsMovingRight = " + ballIsMovingRight);
break;
}
}
@Override
public void run()
{
Log.i("Step 2/2: Run:", " ");
while (playingSquash) {
Log.i("Step 2/2: Run: ", "While loop (playingSquash) = " + playingSquash);
drawCourt();
updateCourt();
controlFPS();
}
}
public void updateCourt()
{
Log.i("Step 2/2/1:updateC: ", "");
Log.i("Step 2/2/1:updateC: ", "racketIsMovingRight = " + racketIsMovingRight);
Log.i("Step 2/2/1:updateC: ", "racketIsMovingLeft = " + racketIsMovingLeft);
if (racketIsMovingRight) {
racketPosition.x = racketPosition.x + 10;
Log.i("Step 2/2/1:updateC: ", "1- IF");
Log.i("Step 2/2/1:updateC: ", "if (racketIsMovingRight) = True");
Log.i("Step 2/2/1:updateC: ", "racketPosition.x + 10 = " + racketPosition.x );
}
if (racketIsMovingLeft) {
racketPosition.x = racketPosition.x - 10;
Log.i("Step 2/2/1:updateC: ", " 2- IF");
Log.i("Step 2/2/1:updateC: ", "if (racketIsMovingLeft) = True");
Log.i("Step 2/2/1:updateC: ", "racketPosition.x - 10 = " + racketPosition.x );
}
//detect collisions
Log.i("Step 2/2/1:updateC: ", " detect collisions");
//hit right of screen
if (ballPosition.x + ballWidth > screenWidth) {
ballIsMovingLeft = true;
ballIsMovingRight = false;
soundPool.play(sample1, 1, 1, 0, 0, 1);
Log.i("Step 2/2/1:updateC: ", " 3- IF - hit right of screen");
Log.i("Step 2/2/1:updateC: ", "if ballPosition.x + ballWidth > screenWidth ");
Log.i("Step 2/2/1:updateC: ", "ballPosition.x = " + ballPosition.x);
Log.i("Step 2/2/1:updateC: ", "ballWidth = " + ballWidth);
Log.i("Step 2/2/1:updateC: ", "screenWidth= " + screenWidth);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingLeft= " + ballIsMovingLeft);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingRight= " + ballIsMovingRight);
}
//hit left of screen
if (ballPosition.x < 0) {
ballIsMovingLeft = false;
ballIsMovingRight = true;
soundPool.play(sample1, 1, 1, 0, 0, 1);
Log.i("Step 2/2/1:updateC: ", " 4- IF - hit Left of screen");
Log.i("Step 2/2/1:updateC: ", "if ballPosition.x < 0 ");
Log.i("Step 2/2/1:updateC: ", "ballPosition.x = " + ballPosition.x);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingLeft= " + ballIsMovingLeft);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingRight= " + ballIsMovingRight);
}
//Edge of ball has hit bottom of screen
if (ballPosition.y > screenHeight - ballWidth)
//if (ballPosition.y > screenHeight)
{
Log.i("Step 2/2/1:updateC: ", " Edge of ball has hit bottom of screen");
Log.i("Step 2/2/1:updateC: ", " LookLook **************************");
Log.i("Step 2/2/1:updateC: ", " 5- IF - Edge of ball has hit bottom of screen");
Log.i("Step 2/2/1:updateC: ", "if (ballPosition.y > screenHeight - ballWidth) ");
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y);
Log.i("Step 2/2/1:updateC: ", "screenHeight= " + screenHeight);
Log.i("Step 2/2/1:updateC: ", "ballWidth= " + ballWidth);
Log.i("Step 2/2/1:updateC: ", "if " + ballPosition.y + " > " + (screenHeight - ballWidth));
lives = lives - 1;
Log.i("Step 2/2/1:updateC: ", "lives - 1= " + lives);
if (lives == 0)
{
Log.i("Step 2/2/1:updateC: ", "5/1 - IF= ");
Log.i("Step 2/2/1:updateC: ", "if (lives == 0");
lives = 3;
score = 0;
soundPool.play(sample4, 1, 1, 0, 0, 1);
Log.i("Step 2/2/1:updateC: ", "lives = " + lives);
Log.i("Step 2/2/1:updateC: ", "score = " + score);
}
Log.i("Step 2/2/1:updateC: ", "End of IF 5 - maybe an else");
Log.i("Step 2/2/1:updateC: ", "back to top of screen");
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = 1 + ballWidth");
ballPosition.y = 1 + ballWidth;//back to top of screen
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y);
Log.i("Step 2/2/1:updateC: ", "ballWidth = " + ballWidth);
//what horizontal direction should we use
//for the next falling ball
Log.i("Step 2/2/1:updateC: ", "End of all IFs");
Log.i("Step 2/2/1:updateC: ", "what horizontal direction should we use for the next falling ball ");
Random randomNumber = new Random();
Log.i("Step 2/2/1:updateC: ", "randomNumber = " + randomNumber);
int startX = randomNumber.nextInt(screenWidth - ballWidth) + 1;
Log.i("Step 2/2/1:updateC: ", "startX = " + startX);
ballPosition.x = startX + ballWidth;
Log.i("Step 2/2/1:updateC: ", "ballPosition.x (startX + ballWidth) = " + ballPosition.x);
int ballDirection = randomNumber.nextInt(3);
Log.i("Step 2/2/1:updateC: ", "switch (ballDirection) = " + ballDirection);
switch (ballDirection) {
case 0:
ballIsMovingLeft = true;
ballIsMovingRight = false;
Log.i("Step 2/2/1:switch: ", "Case 0");
Log.i("Step 2/2/1:switch: ", "ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2/2/1:switch: ", "ballIsMovingRight = " + ballIsMovingRight);
break;
case 1:
ballIsMovingRight = true;
ballIsMovingLeft = false;
Log.i("Step 2/2/1:switch: ", "Case 1");
Log.i("Step 2/2/1:switch: ", "ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2/2/1:switch: ", "ballIsMovingRight = " + ballIsMovingRight);
break;
case 2:
ballIsMovingLeft = false;
ballIsMovingRight = false;
Log.i("Step 2/2/1:switch: ", "Case 2");
Log.i("Step 2/2/1:switch: ", "ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2/2/1:switch: ", "ballIsMovingRight = " + ballIsMovingRight);
break;
}
}
//we hit the top of the screen
if (ballPosition.y <= 0) {
ballIsMovingDown = true;
ballIsMovingUp = false;
ballPosition.y = 1;
soundPool.play(sample2, 1, 1, 0, 0, 1);
Log.i("Step 2/2/1:updateC: ", " 6- IF - we hit the top of the screen");
Log.i("Step 2/2/1:updateC: ", "if (ballPosition.y <= 0)");
Log.i("Step 2/2/1:updateC: ", "ballIsMovingDown = " + ballIsMovingDown);
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y );
}
//depending upon the two directions we should
//be moving in adjust our x any positions
if (ballIsMovingDown) {ballPosition.y += 6;
Log.i("Step 2/2/1:updateC: ", " 7- IF - depending upon the two directions we should be moving in adjust our x any positions");
Log.i("Step 2/2/1:updateC: ", "(ballIsMovingDown) ballPosition.y += 6;");
Log.i("Step 2/2/1:updateC: ", "ballIsMovingDown = " + ballIsMovingDown);
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y );
}
if (ballIsMovingUp) {ballPosition.y -= 10;
Log.i("Step 2/2/1:updateC: ", " 8- IF - depending upon the two directions we should be moving in adjust our x any positions");
Log.i("Step 2/2/1:updateC: ", "if (ballIsMovingUp) {ballPosition.y -= 10;");
Log.i("Step 2/2/1:updateC: ", "ballIsMovingDown = " + ballIsMovingDown);
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y );
}
if (ballIsMovingLeft) {ballPosition.x -= 12;
Log.i("Step 2/2/1:updateC: ", " 9- IF - depending upon the two directions we should be moving in adjust our x any positions");
Log.i("Step 2/2/1:updateC: ", "if (ballIsMovingLeft) {ballPosition.x -= 12;");
Log.i("Step 2/2/1:updateC: ", "ballIsMovingLeft = " + ballIsMovingLeft);
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y );
}
if (ballIsMovingRight) {ballPosition.x += 12;
Log.i("Step 2/2/1:updateC: ", " 10- IF - depending upon the two directions we should be moving in adjust our x any positions");
Log.i("Step 2/2/1:updateC: ", " if (ballIsMovingRight) {ballPosition.x += 12;");
Log.i("Step 2/2/1:updateC: ", "ballIsMovingRight = " + ballIsMovingRight);
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y );
}
//Has ball hit racket?
//if (ballPosition.y + ballWidth >= (racketPosition.y - racketHeight / 2))
if ((ballPosition.y + ballWidth) - 45>= (racketPosition.y))
//if (ballPosition.y + ballWidth >= (screenHeight - 200))
{
Log.i("Step 2/2/1:updateC: ", " 11- IF - ball hit the racket");
Log.i("Step 2/2/1:updateC: ", " if (ballPosition.y >= (racketPosition.y))");
Log.i("Step 2/2/1:updateC: ", "ballPosition.y = " + ballPosition.y );
Log.i("Step 2/2/1:updateC: ", "racketPosition.y = " + racketPosition.y );
Log.i("Step 2/2/1:updateC: ", "ballWidth = " + ballWidth);
Log.i("Step 2/2/1:updateC: ", " if " + (ballPosition.y) + ">=" + (racketPosition.y));
int halfRacket = racketWidth / 2;
Log.i("Step 2/2/1:updateC: ", "halfRacket = " + halfRacket);
if (ballPosition.x + ballWidth > (racketPosition.x - halfRacket) && ballPosition.x - ballWidth < (racketPosition.x + halfRacket))
{
//rebound the ball vertically and play a sound
Log.i("Step 2/2/1:updateC: "," rebound the ball vertically and play a sound");
soundPool.play(sample3, 1, 1, 0, 0, 1);
score++;
ballIsMovingUp = true;
ballIsMovingDown = false;
Log.i("Step 2/2/1:updateC: ", "score++ = " + score);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingUp = " + ballIsMovingUp);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingDown =" + ballIsMovingDown);
Log.i("Step 2/2/1:updateC: ", " 11/1- rebound the ball vertically and play a sound");
Log.i("Step 2/2/1:updateC: "," if (ballPosition.x + ballWidth > (racketPosition.x - halfRacket) && ballPosition.x - ballWidth < (racketPosition.x + halfRacket))");
Log.i("Step 2/2/1:updateC: ", "ballPosition.x = " + ballPosition.x );
Log.i("Step 2/2/1:updateC: ", "ballWidth = " + ballWidth);
Log.i("Step 2/2/1:updateC: ", "racketPosition.x = " + racketPosition.x );
Log.i("Step 2/2/1:updateC: ", "halfRacket = " + halfRacket);
Log.i("Step 2/2/1:updateC: ", "if " + (ballPosition.x + ballWidth) + " > " + (racketPosition.x - halfRacket) + " && " + (ballPosition.x - ballWidth) + " < " + (racketPosition.x + halfRacket));
//now decide how to rebound the ball horizontally
if (ballPosition.x > racketPosition.x)
{
Log.i("Step 2/2/1:updateC: ", " 11/1/1 If- now decide how to rebound the ball horizontally");
ballIsMovingRight = true;
ballIsMovingLeft = false;
Log.i("Step 2/2/1:updateC: ", "ballIsMovingRight = " + ballIsMovingRight);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingLeft = " + ballIsMovingLeft);
} else
{
Log.i("Step 2/2/1:updateC: ", " 11/1/1 Else- now decide how to rebound the ball horizontally");
ballIsMovingRight = false;
ballIsMovingLeft = true;
Log.i("Step 2/2/1:updateC: ", "ballIsMovingRight = " + ballIsMovingRight);
Log.i("Step 2/2/1:updateC: ", "ballIsMovingLeft = " + ballIsMovingLeft);
}
}
}
}
public void drawCourt() {
Log.i("Step 2/2/2:drawCourt: ", "");
if (ourHolder.getSurface().isValid()) {
canvas = ourHolder.lockCanvas();
//Paint paint = new Paint();
canvas.drawColor(Color.BLACK);//the background
Log.i("Step 2/2/2:drawCourt: ", "");
paint.setColor(Color.argb(255, 255, 255, 255));
paint.setTextSize(45);
canvas.drawText("Score:" + score + " Lives:" + lives + " fps: " + fps, 20, 40, paint);
//Draw the squash racket
Log.i("Step 33:drawCourt: ", "");
canvas.drawRect(racketPosition.x - (racketWidth / 2), racketPosition.y - (racketHeight / 2), racketPosition.x + (racketWidth / 2), racketPosition.y + racketHeight, paint);
//canvas.drawRect(racketPosition.x - (racketWidth / 2), screenHeight - 200, racketPosition.x + (racketWidth / 2), screenHeight - 190, paint);
//canvas.drawRect(racketPosition.x - (racketWidth / 2),1100, racketPosition.x + (racketWidth / 2),1110, paint);
//canvas.drawRect(336,1000,432,1174, paint);
// int a = (racketPosition.x - (racketWidth / 2));
// int b = racketPosition.y - (racketHeight / 2);
// int c = racketPosition.x + (racketWidth / 2);
// int d = racketPosition.y + racketHeight;
//canvas.drawRect(a,b,c,d, paint);
//Draw the ball
canvas.drawRect(ballPosition.x, ballPosition.y, ballPosition.x + ballWidth, ballPosition.y + ballWidth, paint);
Log.i("Step 2/2/2:drawCourt: ", "");
ourHolder.unlockCanvasAndPost(canvas);
Log.i("Step 2/2/2:drawCourt: ", "");
}
}
public void controlFPS() {
Log.i("Step 2/2/2:controlFPS: ", "");
long timeThisFrame =
(System.currentTimeMillis() - lastFrameTime);
long timeToSleep = 15 - timeThisFrame;
if (timeThisFrame > 0) {
fps = (int) (1000 / timeThisFrame);
}
if (timeToSleep > 0) {
try {
ourThread.sleep(timeToSleep);
} catch (InterruptedException e) {
}
}
lastFrameTime = System.currentTimeMillis();
}
public void pause()
{
playingSquash = false;
try {
ourThread.join();
} catch (InterruptedException e) {
}
}
public void resume()
{
playingSquash = true;
ourThread = new Thread(this);
ourThread.start();
}
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent)
{
switch (motionEvent.getAction() &
MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
if (motionEvent.getX() >= screenWidth /
2) {
racketIsMovingRight = true;
racketIsMovingLeft = false;
} else {
racketIsMovingLeft = true;
racketIsMovingRight = false;
}
break;
case MotionEvent.ACTION_UP:
racketIsMovingRight = false;
racketIsMovingLeft = false;
break;
}
return true;
}
@Override
protected void onStop() {
super.onStop();
while (true) {
squashCourtView.pause();
break;
}
finish();
}
@Override
protected void onPause() {
super.onPause();
squashCourtView.pause();
}
@Override
protected void onResume() {
super.onResume();
squashCourtView.resume();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
squashCourtView.pause();
finish();
return true;
}
return false;
}
}
答案 0 :(得分:1)
问题在于:Log.i("Step 2/2/2:drawCourt: ", "");
Log.i(tag, msg)
有两个参数。
标记 - 用于标识日志消息的来源。通常 标识发生日志调用的类或活动。
msg - 你要记录的消息。
tag
是一种查找日志来源的方法。如果您的msg
为空,则日志中不会显示任何内容。因此,这不会被记录。因此,对于Log.i
或Log.d
等,要记录您需要非空msg
。