即時通訊使用2個類來製作遊戲。一類是用來製造外星人,另一類是實施遊戲,但是當我使用外星人課程時,它給了我一些錯誤。如果我將外星人課程整合到遊戲課程中,那麼它可以正常工作。我無法弄清楚我錯過了什麼。令人困惑的android錯誤
GAME CLASS:
package com.saad.alien;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class Game extends Activity implements OnTouchListener {
CustomView Cview; // my custom view
// /////////////////////////////////////////////////////////////////////////////
// /////////////////// MAN VARIABLES ////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////
float x, y, left = 0, top = 0, right = 0, bottom = 0;
float dx, dy, length, length1, height, height1;
Bitmap man; // the pictures
int done = 0;
RectF rman = new RectF(50, 50, 150, 150);// sample rect in start
// ///////////////////////////////////////////////////////////////////////
// ///////////////// ALIEN1 VARIABLES /////////////////////////////////
// ///////////////////////////////////////////////////////////////////
makeAlien alien1;
int setScreenX, setScreenY;
int sWidth, sHeight; // screen size and width
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Cview = new CustomView(this);
Cview.setOnTouchListener(this);
Display display = getWindowManager().getDefaultDisplay();
sWidth = display.getWidth();
sHeight = display.getHeight();
setScreenX = 480/sWidth;
setScreenY = 800/sHeight;
// //////////////////////////////////////////////////
///set up man //////////////////////////////////
// ///////////////////////////////////////////////
x = 50 * setScreenX;
y = 650 * setScreenY;
dx = dy = 0;
man = BitmapFactory.decodeResource(getResources(), R.drawable.man);
// ////////////////////////////////////////////////////////////////
// ///////////// set up alien 1///////////////////////////////////
// ///////////////////////////////////////////////////////////////
alien1.setAlien(50, 300, 10, setScreenX, setScreenY);
/////////////////////////////////////////////////////////////////////////
setContentView(Cview);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Cview.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Cview.resume();
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
dx = event.getX();
dy = event.getY();
length = x - ((man.getWidth()/2) * setScreenX);
length1 = x + (3 * (man.getWidth())/2) * setScreenX;
height = y - (man.getHeight()/2) * setScreenY;
height1 = y + 3 * (man.getHeight()/2) * setScreenY;
if (dx > length && dx < length1 && dy > height && dy < height1) {
done = 1;
}
break;
case MotionEvent.ACTION_MOVE:
if (done == 1) {
x = event.getX();
y = event.getY();
}
break;
case MotionEvent.ACTION_UP:
if (done == 1) {
x = event.getX();
y = event.getY();
done = 0;
}
break;
}
return true;
}
public class CustomView extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public CustomView(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause() {
isRunning = false;
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
public void run() {
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas can = ourHolder.lockCanvas();
can.drawRGB(0, 0, 0);
// //////////////////////////////////////////////////////////////////
// ////////////// MAN ////////////////////////////////////
// /////////////////////////////////////////////////////////////////
left = (x - (man.getWidth()/2)) * setScreenX;
right = (x + (man.getWidth()/2)) * setScreenX;
top = (y - (man.getHeight())) * setScreenY;
bottom = (y + (man.getHeight()/3)) * setScreenY;
rman.set(left, top, right, bottom);
can.drawBitmap(man, null, rman, null);
// ///////////////////////////////////////////////////////////////
// ////////////////// ALIEN /////////////////////////////////
// ///////////////////////////////////////////////////////////////
alien1.moveAlien(sWidth);
can.drawBitmap(alien1.al, null, alien1.ral, null);
// //////////////////////////////////////////////////////////////////
// ///////////////// COLLISON //////////////////////////////
// //////////////////////////////////////////////////////////////
ourHolder.unlockCanvasAndPost(can);
}
}
}
}
ALIEN CLASS:
package com.saad.alien;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
public class makeAlien extends Activity{
Bitmap al;
int alX, alY;
Rect ral = new Rect(10, 10, 15, 15); // rect for alien
int aleft, aright, atop, abot; // dimension of ral
int alspeed; // speed of alien
int setScreenX, setScreenY;
int sWidth, sHeight; // screen size and width
public void setAlien(int x, int y, int speed, int setScreenX, int setScreenY){
al = BitmapFactory.decodeResource(getResources(), R.drawable.al);
alX = x * setScreenX;
alY = y * setScreenY;
alspeed = speed * setScreenX;
aleft = alX;
aright = alX + (al.getWidth() * setScreenX);
atop = alY;
abot = alY + (al.getHeight() * setScreenY);
}
public void moveAlien(int sWidth){
if (aright >= sWidth || aleft <= 0)
alspeed = -alspeed;
aleft = aleft + alspeed;
aright = aright + alspeed;
ral.set(aleft, atop, aright, abot);
}
}
「給我一些錯誤」有點含糊 – Henry
它在setAlien方法中給出錯誤。但這種方法工作正常,如果我把它放在遊戲類 – user1750990
它的空指針異常 – user1750990