1

So here is my code,

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;
}
}

What I am trying to do is get,

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

To check to see if somewhere in this code,

@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);

Holds the value of one of the pics that are being deleted, but I am not sure how to code this properly. I am not sure if I need to place the pics into an array each time the randomizer runs or what but the code is taken from the "edu4java" tutorial from youtube.

I have the program on a loop as you can tell that I can delete the pics on touch and the score was right I just am not sure how to get the,

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

To check to "see" the proper pic string. Do I need to check the array that the code "auto creates"? Is there a way to check and see what the value of a string is? Such as "seeing" what is actually being "held" by "sprite" or "sprites" ?

Sam
  • 7,252
  • 16
  • 46
  • 65
Joel Shoda
  • 11
  • 1

2 Answers2

0

One problem is that you do not supply the source code for Sprite, but perhaps it looks like the code here? Given the code there, there is no neat solution to your problem with the class as it is.

So, how I would approach solving this problem is to add to each sprite a resource ID:

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

Note that I add the extra resource parameter to the constructor. Furthermore, I would add to the Sprite class a method int Sprite.getResource(), so your collision detection code becomes:

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 ...
}

Note: this code is in no way optimal, but hopefully this will point you in the right direction to discover for yourself a better solution. Here in Stack Overflow we don't throw fishes, we teach people to fish.

Community
  • 1
  • 1
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • i know you don't :) .......and i have tried something like that .... because there is a sprite class but it isn't like what you have .....thank you .....i will give this a shot :) – Joel Shoda Jun 07 '13 at 13:14
0

You can't compare a bitmap with a resource Id, and actually trying to do it manually your self would end up in quiet exhaustive performance for a simple validation, what i would do and to keep it simple, i would create my own class that extends from Sprite, and in stead of passing the context and bitmap, i would pass the context and resource Id, then within this class i would decode the resource and keep a reference of the resource id, i would override the equal method from Sprites and use the reference used to create the object to do the comparison, this would be my class

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;
    }
}

This way i keep it as a simple int comparison, and most important you can use it to compare two objects of same bmpID, or something like what u wanted by doing this:

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

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • this looks like it would be a more optimal solution ...... i have a LOT to learn lol ......thank you i will see how this goes as well .......THANK YOU ALL for helping me :) – Joel Shoda Jun 07 '13 at 13:15
  • If you liked any of the answers, i recommend you to mark one of them as correct, other way for further questions people might not answer your questions because of a low answers marked rate... – Martin Cazares Jun 07 '13 at 15:55