1

所以这是我的代码,

public class GameView extends SurfaceView {

private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private List<Sprite> sprites = new ArrayList<Sprite>();
private long lastClick;

public int d = 0;
public int color;
TextView tv;
public int score;

public GameView(Context context) {
    super(context);

    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            createSprites();
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int arg2, int height) {
        }

    });

}

private void createSprites() {

    int c = 10;
    {
        Random rnd = new Random();
        color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256),
                rnd.nextInt(256));
        for (int b = 1; b <= c; b++) {

            int random = (int) Math.ceil(Math.random() * 24);
            if (random == 1) {
                sprites.add(createSprite(R.drawable.bad1));
            } else if (random == 2) {
                sprites.add(createSprite(R.drawable.bad2));
            } else if (random == 3) {
                sprites.add(createSprite(R.drawable.bad3));
            } else if (random == 4) {
                sprites.add(createSprite(R.drawable.bad4));
            } else if (random == 5) {
                sprites.add(createSprite(R.drawable.bad5));
            } else if (random == 6) {
                sprites.add(createSprite(R.drawable.bad6));
            } else if (random == 7) {
                sprites.add(createSprite(R.drawable.bad7));
            } else if (random == 8) {
                sprites.add(createSprite(R.drawable.bad8));
            } else if (random == 9) {
                sprites.add(createSprite(R.drawable.bad9));
            } else if (random == 10) {
                sprites.add(createSprite(R.drawable.bad10));
            } else if (random == 11) {
                sprites.add(createSprite(R.drawable.bad11));
            } else if (random == 12) {
                sprites.add(createSprite(R.drawable.bad12));
            } else if (random == 13) {
                sprites.add(createSprite(R.drawable.bad13));
            } else if (random == 14) {
                sprites.add(createSprite(R.drawable.bad14));
            } else if (random == 15) {
                sprites.add(createSprite(R.drawable.bad15));
            } else if (random == 16) {
                sprites.add(createSprite(R.drawable.bad16));
            } else if (random == 17) {
                sprites.add(createSprite(R.drawable.bad17));
            } else if (random == 18) {
                sprites.add(createSprite(R.drawable.good1));
            } else if (random == 19) {
                sprites.add(createSprite(R.drawable.good2));
            } else if (random == 20) {
                sprites.add(createSprite(R.drawable.good3));
            } else if (random == 21) {
                sprites.add(createSprite(R.drawable.good4));
            } else if (random == 22) {
                sprites.add(createSprite(R.drawable.good5));
            } else if (random == 23) {
                sprites.add(createSprite(R.drawable.good6));
            } else if (random == 24) {
                sprites.add(createSprite(R.drawable.good7));
            }
        }

    }
}

private Sprite createSprite(int resource) {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
    return new Sprite(this, bmp);

}

@SuppressLint({ "WrongCall", "DrawAllocation" })
@Override
public void onDraw(Canvas canvas) {

    canvas.drawColor(color);
    Paint paint = new Paint();
    paint.setColor(Color.CYAN);
    canvas.drawText("SCORE  " + score, 10, 10, paint);

    for (Sprite sprite : sprites) {
        sprite.onDraw(canvas);
    }

}

// this is the ontouch event to destroy the sprites and make the blood splat
// effect
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 200) {
        lastClick = System.currentTimeMillis();
        synchronized (getHolder()) {
            float x = event.getX();
            float y = event.getY();
            for (int i = sprites.size() - 1; i >= 0; i--) {
                Sprite sprite = sprites.get(i);

                if (sprite.isCollition(x, y)) {
                    {
                        if ((sprites).equals    (R.drawable.bad1))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad2))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad3))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad4))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad5))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad6))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad7))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad8))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad9))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad10))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad11))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad12))
                            score = score + 5;
                        else if ((sprites).equals(R.drawable.bad13))
                            score = score + 5;
                        else
                            score = score - 5;
                    }
                    d++;
                    if (d >= 10) {
                        d = 0;
                        createSprites();
                    }
                    break;
                }
            }
        }
    }

    return true;
}
}

我想做的是得到,

   if ((sprites).equals(R.drawable.bad1))
score = score + 5;

要检查此代码中是否有某处,

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (System.currentTimeMillis() - lastClick > 200) {
        lastClick = System.currentTimeMillis();
        synchronized (getHolder()) {
            float x = event.getX();
            float y = event.getY();
            for (int i = sprites.size() - 1; i >= 0; i--) {
                Sprite sprite = sprites.get(i);

保存正在删除的其中一张图片的值,但我不确定如何正确编码。我不确定每次随机发生器运行时是否需要将图片放入数组中,或者代码取自 youtube 的“edu4java”教程。

我有一个循环的程序,你可以告诉我我可以删除触摸的照片并且分数是正确的我只是不知道如何获得,

if ((sprites).equals(R.drawable.bad1))
score = score + 5;

检查以“查看”正确的图片字符串。我是否需要检查代码“自动创建”的数组?有没有办法检查字符串的值是什么?例如“看到”“精灵”或“精灵”实际上“持有”的是什么?

4

2 回答 2

0

您无法将位图与资源 ID 进行比较,并且实际上尝试手动执行此操作最终会以安静的详尽性能进行简单验证,我会做什么并保持简单,我将创建自己的类从Sprite扩展,而不是传递上下文和位图,而是传递上下文和资源ID,然后在这个类中我将解码资源并保留资源ID的引用,我将覆盖Sprites的equal方法和使用用于创建对象的引用进行比较,这将是我的类

public class MySprite extends Sprite{
    private int bmpID;
    public MySprite(Context context, int bmpID){
        this.bmpID = bmpID;
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), bmpID);
        super(context, bmp);
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof MySprite))return false;
        return this.bmpID == MySprite.class.cast(o).getBmpId();
    }

    public int getBmpId(){
        return bmpID;
    }
}

这样,我将其保留为简单的 int 比较,最重要的是,您可以使用它来比较具有相同 bmpID 的两个对象,或者通过执行以下操作来比较您想要的东西:

if ((sprites).getBmpId() == R.drawable.bad1))
score = score + 5;

问候!

于 2013-06-07T04:09:24.153 回答
0

一个问题是您没有提供源代码Sprite,但它可能看起来像这里的代码?鉴于那里的代码,对于您的类问题,没有任何巧妙的解决方案。

所以,我将如何解决这个问题是为每个精灵添加一个资源 ID:

private Sprite createSprite(int resource) {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
    return new Sprite(this, bmp, resource);
}

请注意,我将额外的resource参数添加到构造函数中。此外,我会在Sprite类中添加一个方法int Sprite.getResource(),因此您的碰撞检测代码变为:

if (sprite.isCollition(x, y))
{
    if (sprite.getResource() == R.drawable.bad1)
        score = score + 5;
    else if (sprite.getResource() == R.drawable.bad2)
        score = score + 5;
    else ...
}

注意:此代码绝不是最佳的,但希望这将为您指明正确的方向,为您自己发现更好的解决方案。在 Stack Overflow,我们不扔鱼,我们教人们钓鱼。

于 2013-06-07T03:54:47.730 回答