我从Mario Zechner的书#34; Beginning Android Games,2nd Edition开始构思这个游戏"所以归功于他!
现在,我想在屏幕上创建和动画很多对象,比如"敌人或块#34;。我想创建一个单独的类,并在这个类中创建一个数组或链接的实例列表,这样我就不必为每个"敌人"编写代码。我想在那里定义那里的运动。我试过......
Enemy e = new Enemy(canvas);
但它不起作用。我是Android的初学者,拼命想学习!我想出了运动,碰撞等,但是卡在这里。请给我一些想法。
public class MyGFXSur extends SurfaceView implements Runnable{
SurfaceHolder holder;
Thread thread = null;
boolean isRunning = false;
public MyGFXSur(Context context) {
super(context);
holder = getHolder();
}
public void pause(){
isRunning = false;
while (true){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
}
public void resume(){
isRunning = true;
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while(isRunning){
if(!holder.getSurface().isValid()){
continue;
}
Canvas canvas = holder.lockCanvas();
canvas.drawRGB(02, 02, 150);
if(x != 0 && y != 0){
Bitmap test = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getWidth()/2), null);
}
holder.unlockCanvasAndPost(canvas);
}
}
}
另一类......
public class GFXSur extends Activity implements View.OnTouchListener{
MyGFXSur mygfxsur;
float x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mygfxsur = new MyGFXSur(this);
mygfxsur.setOnTouchListener(this);
x = 0;
y = 0;
setContentView(mygfxsur);
}
@Override
protected void onResume() {
super.onResume();
mygfxsur.resume();
}
@Override
protected void onPause() {
super.onPause();
mygfxsur.pause();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
return true;
}
}
我尝试了以下操作,但是我收到了这个错误:"无法解析方法getResources()"
public class Enemy {
public Enemy(Canvas canvas) {
Bitmap test = BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
canvas.drawBitmap(test, 100, 100, null);
}
}
答案 0 :(得分:2)
在这里,您应该给出一个如何处理自绘图对象的提示:
定义DrawingObject应该做什么的SuperClass
public abstract class DrawinObject{
public abstract void draw(Canvas canvas);
public abstract void updatePhysics();
}
实现Class,例如绘制一个Rect,但你可以绘制任何东西
public class MyRect extends DrawinObject{
Paint paint;
Context context;
public MyRect(Context context){
//do some init
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
}
public void updatePhysics(){
}
public void draw(Canvas canvas){
//if you need some resources eg drawables just call
//context.getResources().getDrawables(R.drawable.ic_launcher);
canvas.drawRect(30, 30, 80, 80, paint);
}
}
另一个实现
public class MyCircle extends DrawinObject{
Paint paint;
int x, y;
public MyRect(){
//do some init
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
x= 10;
y = 30;
}
public void updatePhysics(){
x++;
y++;
}
public void draw(Canvas canvas){
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
}
现在,只需将它们添加到List并循环遍历游戏循环中的列表
即可//MAIN
private ArrayList<DrawinObject> myDrawingObjects;
public MyGFXSur(Context context){
myDrawingObjects = new ArrayList<DrawinObject>();
myDrawingObjects.add(new MyCircle());
myDrawingObjects.add(new MyRect(MainActivity.this));
}
@Override
public void run() {
while(isRunning){
if(!holder.getSurface().isValid()){
continue;
}
updatePhysics();
Canvas canvas = holder.lockCanvas();
drawObjects(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
private void updatePhysics(){
for(DrawinObject drawObject : myDrawingObjects){
drawObject.updatePhysics();
}
}
private void drawObjects(Canvas canvas){
for(DrawinObject drawObject : myDrawingObjects){
drawObject.draw(canvas);
}
}
答案 1 :(得分:0)
您必须处理从活动到Enemy Class的上下文,在该类中您拥有了decodeBitmap方法。否则为NULLPointer异常