我如何正确使用.equals?

时间:2013-06-07 03:16:48

标签: android string

所以这是我的代码,

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;

检查“看到”正确的pic字符串。我是否需要检查代码“自动创建”的数组?有没有办法检查并查看字符串的值是什么?比如“看到”实际被“精灵”或“精灵”“持有”的东西?

2 个答案:

答案 0 :(得分:0)

一个问题是您没有提供Sprite的源代码,但可能是looks like the code here?鉴于那里的代码,对于你的课程问题没有简洁的解决方案。

所以,我如何解决这个问题的方法是向每个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中我们不扔鱼,我们教人们钓鱼。

答案 1 :(得分:0)

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

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;

问候!