Search code examples
androidobjectarraylistandroid-studiographic

How can I make each individual frog stop using onTouch?


I am making a game where there there are frogs hopping around the screen. Once a frog is touched, I change the game image to what I have set as "deadFrog" and its movement stops. I have them all created under and array-list, and I am unsure as to how to only make changes to the individual frog. Right now, if one frog is tapped, all of the frogs stop moving and change to deadFrog. Hopefully you can help me fix the tactical nuke of a tap ;) *If you need any more information, just comment and I'll be sure to provide it!

Edit Is there not a way to access a single element in the blocks arraylist? I've tried doing blocks(1) for example but that's invalid.

Here is where the frogs are declared:

public void init() {
    blocks = new ArrayList<Block>();
    for (int i = 0; i < 5; i++) {
        Block b = new Block(i * 200, MainActivity.GAME_HEIGHT - 95,
                BLOCK_WIDTH, BLOCK_HEIGHT);
        blocks.add(b);
        tapped = false;
    }
}

They are rendered with this:

private void renderFrogs(Painter g) {
    if (!tapped) {
        for (int i = 0; i < blocks.size(); i++) {
            Block b = blocks.get(i);
            if (b.isVisible()) {
                Assets.runAnim.render(g, (int) b.getX(), (int) b.getY());
            }
        }
    }
    if (tapped) {
            for (int i = 0; i < blocks.size(); i++) {
                Block b = blocks.get(i);
                if (b.isVisible()) {
                    g.drawImage(Assets.deadfrog, (int) b.getX(), (int) b.getY());
                }
            }
        }

    }

And this is the onTouchListener:

public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
    if (e.getAction() == MotionEvent.ACTION_DOWN) {
        recentTouchY = scaledY;
    } else if (e.getAction() == MotionEvent.ACTION_UP) {
        for (int i = 0; i < blocks.size(); i++) {
            Block b = blocks.get(i);
            if ((scaledY >= b.getY() - BLOCK_HEIGHT || scaledY <= b.getY()) &&   (scaledX >= b.getX() || scaledX <= b.getX() + BLOCK_WIDTH)) {
                tapped = true;
            }
        }

    }
    return true;
}

Solution

  • Of course all frogs turn dead, you are supposed to keep "tapped" variable for each frog, your tapped variable is for all frogs at once.

    Declare a class

    public class Frog extends View{
       public Drawable liveFrog;
       public Drawable deadFrog;
       public boolean isDead;
       public Point location;
       public int width;
       public int height;
    
      public Frog(Context context, int x, int y,int width,int height){
          super(context);
          this.isDead = false;
          this.location = new Point(x,y);
          this.width = width;
          this.height = height;
       }
    
    
    public void onDraw(Canvas c){
       super.onDraw(c);
          if(!isDead){
            //draw live frog at x,y
          }else {
              //draw dead frog at x,y
          }
     }
    }
    

    then you array is supposed to contain frogs

    public void init(Context context) {
        blocks = new ArrayList<Frog>();
        for (int i = 0; i < 5; i++) {
          Frog b = new Frog(context,i * 200, MainActivity.GAME_HEIGHT - 95,
                BLOCK_WIDTH, BLOCK_HEIGHT);
            blocks.add(b);
    
        }
    }
    
    private void renderFrogs() {
            for(Frog f : blocks){
                   //cause redraw
                     f.invalidate();
             }
        }
    
    here comes the fun part, when you tap the frog
    
    
       public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            recentTouchY = scaledY;
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
            for (int i = 0; i < blocks.size(); i++) {
                Frog frog = blocks.get(i);
                if ((scaledY >= frog.getY() - BLOCK_HEIGHT || scaledY <= frog.getY()) &&   (scaledX >= frog.getX() || scaledX <= frog.getX() + BLOCK_WIDTH)) {
                    frog.isDead = true;
                     //cause one frog redraw
                    frog.invalidate();
    
                   //if the event was handled, stop here (unless you can have multiple frogs one on top of the other ? 
                  return true;
                }
            }
    
        }
         //if the event was not handled, let it bubble up
        return false;
    }