我正在制作一个游戏,其中我使用2个类。一个类用于制作外星人,另一个类实现游戏但是当我使用外星人类时它给我一些错误。如果我将外星人类集成到游戏类中,那么它可以正常工作。我无法弄清楚我失踪了什么。
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);
}
}
答案 0 :(得分:1)
为什么“makeAlien”类会扩展Activity? 请检查Java命名约定,因为“makeAlien”对于类来说是一个坏名称。
答案 1 :(得分:0)
你得到的另一个错误可能就是调用getResources()
,因为那是Activity
的一部分。只需将drawable传递给setAlien()
方法,而不是从那里检索它。