如何使用onTouch让每个青蛙停止?

时间:2015-04-18 04:35:43

标签: android object arraylist android-studio graphic

我正在制作一个游戏,那里有青蛙在屏幕上跳来跳去。一旦青蛙被触摸,我将游戏图像更改为我设置为" deadFrog"它的运动停止了。我将它们全部创建在array-list下,我不确定如何只对个体青蛙进行更改。现在,如果一只青蛙被轻拍,所有的青蛙都会停止移动并变成deadFrog。希望你可以帮助我修复水龙头的战术核武器;)*如果您需要更多信息,请发表评论,我一定会提供它!

编辑有没有办法访问块arraylist中的单个元素?我尝试过做过块(1),但这无效。

以下是宣布青蛙的地方:

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

它们用以下方式呈现:

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

    }

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

2 个答案:

答案 0 :(得分:1)

当然,所有青蛙都会死亡,你应该保持&#34;轻拍&#34;每只青蛙的变量,你的tapped变量适用于所有青蛙。

宣布一个班级

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

然后你的数组应该包含青蛙

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

答案 1 :(得分:0)

从我所看到的,你的&#34;轻拍&#34;布尔值不是每个青蛙的属性。它被声明一次,并且当被触发时,按照你的循环,将使每只青蛙死亡(显然,因为那是你正在经历的事情)。

一次&#34;点击&#34;是的,你的for循环遍历每一个街区并为它分配一只死青蛙。

我认为您需要创建一个Frog类,并在ArrayList中存储这些类的实例。新蛙类的一个变量将被触及&#34;,当触发它时,你将只对该特定实例做一些事情。