0

我对 Android 还很陌生,我无法加载图像。我当前的代码如下所示:

package com.android.skeleton;

import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.SoundPool;

public class GameModel extends Game {
    private Bitmap background;
    private Bitmap gameover;
    public final int PLAYER = 0;
    public final int ENEMY = 1;
    public final int EXIT = 2;
    public final int BIGENEMY = 3;

    private int health;

    Sprite player;
    Enemy enemy1;
    Enemy enemy2;
    BigEnemy enemy3;
    Enemy enemy4;
    Enemy enemy5;
    Enemy enemy6;
    Exit exit;

    //Images
    Bitmap cat;
    Bitmap jellyfish;
    Bitmap bigdog;
    Bitmap exitTile;

    SoundPool music;
    SoundPool damageSound;

    Context context;
    InputStream inputStream = null;

    private static GameModel gameModel = null;
    public static GameModel getGameModel(){
        if(gameModel == null){
            gameModel = new GameModel();
        }
        return gameModel;
    }
    private GameModel(){

    }

    public void update(){

    }

    public void draw(Canvas canvas){

    }

    public boolean processTouchEvent(int x, int y){
        return false;
    }
    @Override
    public void gameStart() {
        // TODO Auto-generated method stub

         try{
                AssetManager assetManager = context.getAssets();
                inputStream = assetManager.open("/resources/blackcat.png");
                cat = BitmapFactory.decodeStream(inputStream);
                inputStream.close();

                inputStream = assetManager.open("/resources/Jellyfish1.png");
                jellyfish = BitmapFactory.decodeStream(inputStream);
                inputStream.close();

                inputStream = assetManager.open("/resources/Dog.png");
                bigdog = BitmapFactory.decodeStream(inputStream);
                inputStream.close();

                inputStream = assetManager.open("/resources/exit.png");
                exitTile = BitmapFactory.decodeStream(inputStream);
                inputStream.close();

                inputStream = assetManager.open("/resources/kitchen.png");
                background = BitmapFactory.decodeStream(inputStream);
                inputStream.close();

                inputStream = assetManager.open("/resources/endscreen.png");
                gameover = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                }catch(IOException ex){}

         gameSetBackground(background);

        player = new Sprite(cat, 0);
        enemy1 = new Enemy(jellyfish, 0);
        enemy2 = new Enemy(jellyfish, 0);
        enemy4 = new Enemy(jellyfish, 0);
        enemy5 = new Enemy(jellyfish, 0);
        enemy6 = new Enemy(jellyfish, 0);
        enemy3 = new BigEnemy(bigdog, 1.4);
        exit = new Exit (exitTile, 0);

        player.setLocation(320, 480);
        player.setType(0);
        player.setActive();
        health = 10;

        enemy1.setLocation(0, 160);
        enemy1.setVelocity(5, 0);
        enemy1.setType(1);
        enemy1.setActive();

        enemy2.setLocation(640, 320);
        enemy2.setVelocity(-5, 0);
        enemy2.setType(1);
        enemy2.setActive();

        Random random = new Random();
        Random random2 = new Random();
        int i = Math.abs(random.nextInt() % 10);
        int j = Math.abs(random2.nextInt() % 10);

        while (i < 2){
            i = Math.abs(random.nextInt() % 10);
        }
        while (j < 2){
            j = Math.abs(random.nextInt() % 10);
        }

        enemy4.setLocation(640, 0);
        enemy4.setVelocity(-i, j);
        enemy4.setType(1);
        enemy4.setActive();

        Random random3 = new Random();
        Random random4 = new Random();
        int k = Math.abs(random3.nextInt() % 10);
        int l = Math.abs(random4.nextInt() % 10);

        while (k < 2){
            k = Math.abs(random.nextInt() % 10);
        }
        while (l < 2){
            l = Math.abs(random.nextInt() % 10);
        }

        enemy5.setLocation(0, 480);
        enemy5.setVelocity(k, l);
        enemy5.setType(1);
        enemy5.setActive();

        enemy6.setLocation(600, 440);
        enemy6.setVelocity(0, -5);
        enemy6.setType(1);
        enemy6.setActive();

        exit.setLocation(600, 0);
        exit.setVelocity(0, 0);
        exit.setType(2);
        exit.setActive();

        //Add sprites to game
        gameAddSprite(player);
        gameAddSprite(enemy1);
        gameAddSprite(enemy2);
        gameAddSprite(enemy3);
        gameAddSprite(enemy4);
        gameAddSprite(enemy5);
        gameAddSprite(enemy6);
        gameAddSprite(exit);

    }
    @Override
    public void gameEnd() {
        // TODO Auto-generated method stub
        player.setInactive();
        enemy1.setInactive();
        enemy2.setInactive();
        enemy3.setInactive();
        enemy4.setInactive();
        enemy5.setInactive();
        enemy6.setInactive();
        exit.setInactive();
        gameSetBackground(gameover);

    }
    @Override
    public void gameUpdate(Canvas canvas) {
        // TODO Auto-generated method stub
        player.update();

        enemy1.rotate(0.1);
        enemy1.update();

        enemy2.rotate(0.1);
        enemy2.update();

        enemy3.update();

        enemy4.rotate(0.1);
        enemy4.update();

        enemy5.rotate(0.1);
        enemy5.update();

        enemy6.rotate(0.2);
        enemy6.update();

        if(enemy3.getPosy() == 430){
            enemy3.setInactive();
        }

        if(health == 0){
            gameEnd();
        }
    }
    @Override
    public void spriteCollide(Sprite s1, Sprite s2) {
        // TODO Auto-generated method stub

        if(s2.getType() == ENEMY && s1.getType() == PLAYER)
        {
            health -= 1;
        }
        if(s2.getType() == BIGENEMY && s1.getType() == PLAYER)
        {
            health -= 1;
        }
        if (s2.getType() == EXIT && s1.getType() == PLAYER)
        {
            gameEnd();
        }
        if(s2.getType() == ENEMY && s1.getType() == ENEMY)
        {
            double i = s2.getVelx();
            double j = s2.getVely();
            double k = s1.getVelx();
            double l = s1.getVely();
            s2.setVelocity(-i, -j);
            s1.setVelocity(-k,-l);
        }
    }

    public void actionPerformed() {
        // TODO Auto-generated method stub

            //Create random spawn positions for bigdogs
            Random random = new Random();
            Random random2 = new Random();
            int xPos = Math.abs(random.nextInt() % 640);
            int yVel = Math.abs(random2.nextInt() % 20);

            //Make sure y velocity is between 10 and 30. Set this way for now
            //to hide problems with individual enemy3 sprites becoming inactive.
            while (yVel < 10)
                yVel = Math.abs(random.nextInt() % 30);

            //Spawn big dogs with random x position and y velocity
            //according to timer settings.
                enemy3.setLocation(xPos, 0);
                enemy3.setActive();
                enemy3.setVelocity(0, -yVel);
                enemy3.setType(3);

    }
@Override
public void gameCheckInputs() {
    // TODO Auto-generated method stub

}

}

我知道我可能正在做一些愚蠢的事情,这可能是一个非常快速的解决方案。

已更新完整代码

这是日志文件:

04-30 03:49:19.490: W/dalvikvm(749): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-30 03:49:19.560: E/AndroidRuntime(749): FATAL EXCEPTION: main
04-30 03:49:19.560: E/AndroidRuntime(749): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.skeleton/com.android.skeleton.Android_SkeletonActivity}: java.lang.NullPointerException
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.os.Looper.loop(Looper.java:137)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 03:49:19.560: E/AndroidRuntime(749):  at java.lang.reflect.Method.invokeNative(Native Method)
04-30 03:49:19.560: E/AndroidRuntime(749):  at java.lang.reflect.Method.invoke(Method.java:511)
04-30 03:49:19.560: E/AndroidRuntime(749):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 03:49:19.560: E/AndroidRuntime(749):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 03:49:19.560: E/AndroidRuntime(749):  at dalvik.system.NativeStart.main(Native Method)
04-30 03:49:19.560: E/AndroidRuntime(749): Caused by: java.lang.NullPointerException
04-30 03:49:19.560: E/AndroidRuntime(749):  at com.android.skeleton.Game.gameSetBackground(Game.java:61)
04-30 03:49:19.560: E/AndroidRuntime(749):  at com.android.skeleton.GameModel.gameStart(GameModel.java:98)
04-30 03:49:19.560: E/AndroidRuntime(749):  at com.android.skeleton.GameSurfaceView.<init>(GameSurfaceView.java:23)
04-30 03:49:19.560: E/AndroidRuntime(749):  at com.android.skeleton.Android_SkeletonActivity.onCreate(Android_SkeletonActivity.java:18)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.Activity.performCreate(Activity.java:4465)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-30 03:49:19.560: E/AndroidRuntime(749):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

这是游戏设置背景

public void gameSetBackground(Bitmap i){
        backgroundImage = i;
        if (clearList == null){
            clearList = new LinkedList<Rect>(); }
        clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
    }

这是Game.java

package com.android.skeleton;

import java.util.LinkedList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.SurfaceHolder;


public abstract class Game implements Runnable {

    private LinkedList<Sprite> spriteList;

    public LinkedList<Sprite> sprites(){
        return spriteList;
    }
    private LinkedList<Rect>clearList;
    private Thread gameThread;
    private Bitmap backgroundImage=null;
    Canvas canvas;
    SurfaceHolder surfaceHolder = null;
    Canvas backGraphics;

    public abstract void gameStart();

    public abstract void gameEnd();

    public abstract void gameUpdate(Canvas c);

    public abstract void spriteCollide(Sprite s1, Sprite s2);

    public void init(){
        spriteList = new LinkedList<Sprite>();
        clearList = new LinkedList<Rect>();
        canvas = new Canvas();
    }

    public void setDebugOn(){
        for(Sprite s: spriteList)
            s.showRectangle();
    }
    public void setDebugOff(){
        for(Sprite s: spriteList)
            s.hideRectangle();
    }

    public void gameAddSprite(Sprite s){
        spriteList.add(s);
    }

    public void gameAddSprites(Sprite []slist){
        for(Sprite sp: slist)
            spriteList.add(sp);
    }

    public void gameSetBackground(Bitmap i){
        backgroundImage = i;
        if (clearList == null){
            clearList = new LinkedList<Rect>(); }
        clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
    }

    public void start(){
        gameThread  = new Thread(this);
        gameThread.start();
        gameStart();
    }

    public void stop(){
        gameEnd();
        gameThread = null;
    }

    public void update(Canvas canvas){
        for(Sprite s: spriteList){
            if(s.needsCleared()){

        }
        gameUpdate(backGraphics);
        paint(canvas);}
    }

    public void paint(Canvas canvas){
        if(backgroundImage != null) {
            for(Rect r: clearList){
                backGraphics.drawBitmap(backgroundImage, 
                                       r.left, r.top, null);
            }
        } else {
            for(@SuppressWarnings("unused") Rect r: clearList){
            }
        }
        clearList.clear();
        int n = spriteList.size();
        for(int i=n-1; i>=0; i--){
            spriteList.get(i).draw(backGraphics);
        }
    }

    public void run() {
        while(gameThread == Thread.currentThread()){
            if(surfaceHolder.getSurface().isValid()){
                canvas = new Canvas();
            canvas = surfaceHolder.lockCanvas();
            gameCheckInputs();
            checkCollisions();
            paint(canvas);
            try{
                Thread.sleep(40);
            }
            catch(InterruptedException ex){
                ex.printStackTrace();
            }
        }
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }

    private void checkCollisions(){
        if(spriteList.size() > 1){
            int nsprites = spriteList.size();
            for(int i=0; i<nsprites; i++) if(spriteList.get(i).isActive()){
                for(int j = i+1; j<nsprites; j++) if(spriteList.get(j).isActive()){
                    if(spriteList.get(i).getRect().intersect(spriteList.get(j).getRect())){
                        spriteCollide(spriteList.get(i), spriteList.get(j));
                    }
                }
            }
        }           
    }

    public void gameCheckInputs() {
        // TODO Auto-generated method stub

    }

    public void gameStart(Context context) {
        // TODO Auto-generated method stub

    }
}
4

4 回答 4

1

嗨,nutriletter,我可以知道你真正想要做什么 -

public void gameSetBackground(Bitmap i){
    backgroundImage = i;
    if (clearList == null){
        clearList = new LinkedList<Rect>(); }
    clearList.add(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()));
}

你没有使用 backgroundImage我对吗?

尝试这个 -

if(backgroundImage!=null) 

你的代码的其余部分..

于 2012-04-30T05:33:39.333 回答
0

基本上我做错了什么是我没有在我的 Game.java 类中调用 Init,我加入了一些捕获,例如

if (clearList == null){
        clearList = new LinkedList<Rect>(); }

我现在遇到的问题是它只显示一个空白屏幕,但如果我要为这个问题提出一个新问题可能会更好,但是谢谢大家的帮助!

于 2012-05-01T04:42:48.687 回答
0

将您的图像放入 assets 文件夹而不是将其放入 assets/resources 文件夹并使用以下代码检索图像。

try {
    InputStream bitmap=getAssets().open("icon.png");
    Bitmap bit=BitmapFactory.decodeStream(bitmap);

} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
于 2012-04-30T04:51:19.093 回答
0

我很难遵循您的代码,但我看到的一个问题,至少要解决您的异常,是您getWidth()可能在画布被绘制之前调用画布。在onCreate()一个activity的方法中,你的activity还没有被绘制到屏幕上,所以现在不是操作a的canvas的好时机View。我可以肯定知道的最好建议是扩展SurfaceView和覆盖该onDraw()方法,然后SurfaceView在您的 XML 布局中使用您的自定义,并使用完全量化的名称,例如<com.android.skeleton.MySurfaceView />.

例如:

public class MySurfaceView extends SurfaceView {

    public MySurfaceView(Context context) {
        this(context, null, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        // Draw some stuff
    }
}

如果你在 XML 中扩展你的布局,你会像这样使用你的自定义视图:

<com.mypackage.MySurfaceView
    ...
/>
于 2012-04-30T18:58:14.623 回答