【问题标题】:How can I make each individual frog stop using onTouch?如何让每只青蛙停止使用 onTouch?
【发布时间】:2015-04-18 04:35:42
【问题描述】:

我正在制作一个游戏,其中有青蛙在屏幕上跳来跳去。一旦触摸到一只青蛙,我将游戏图像更改为我设置的“deadFrog”并且它的运动停止。我在数组列表下创建了它们,我不确定如何只对单个青蛙进行更改。现在,如果点击一只青蛙,所有的青蛙都会停止移动并变成死蛙。希望您能帮我解决一下战术核弹问题;)*如果您需要更多信息,请发表评论,我一定会提供!

编辑没有办法访问块数组列表中的单个元素吗?例如,我尝试过 blocks(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;
}

【问题讨论】:

    标签: android object arraylist android-studio graphic


    【解决方案1】:

    当然所有的青蛙都死了,你应该为每只青蛙保留“tapped”变量,你的 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;
    }
    

    【讨论】:

    • 青蛙类的invalidate方法应该包含什么?
    • invalidate 方法是一个系统方法 - 不要重写它;)它调用 onDraw 方法,你必须重写它
    • 在渲染青蛙方法中,您还必须在使青蛙无效之前将所有青蛙添加到视图中,它们必须使用“addView”方法附加到某个东西上
    • 谢谢莉娜,我会看看我能做什么,一旦我想出来我会发布一个解决方案
    • 我想通了,我应该在此处发布解决方案的副本还是编辑我的主要问题并在底部发布我的新代码?
    【解决方案2】:

    据我所知,您的“点击”布尔值不是每个青蛙的属性。它被声明一次,当触发时,根据你的 for 循环,会让每只青蛙都死掉(显然,因为这就是你所经历的)。

    一旦 "tapped" 为真,你的 for 循环就会遍历每个块并为其分配一只死青蛙。

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

    【讨论】:

    • 好的,我正在关注你 BG_NE。如何访问该特定实例?
    • 我明白了!我应该在此评论框中发布新代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 2013-09-24
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多