如何在两个 CanvasView 对象上使用 onClickListener?

How to use onClickListener on two CanvasView Objects?

我正在尝试制作一款游戏,当用户点击射手时,它会改变其位置并射击,如果用户点击射出的球(或项目中命名的射门),它们就会消失代码在这里

Shooter.JAVA

public class shooter extends View {

       //Consider all variables are declared
        public shooter(int color, Context c) {
            super(c);
            paint = new Paint();
            paint.setColor(color);
            mContext = c;

        }
        public void move() {
           //Moves Cannon On Click (CODE NOT SHOWN PURPOSELY)  
            invalidate();
        }

        public float getPosition()
        {
            return shootPoint;
        }

        public void draw(Canvas canvas)
        {
            super.draw(canvas);
// simply draws a rectangle (shooter)
            cW=getWidth();
            cH=getHeight();
            center=new Point(cW/2,cH);
             left=0; right=center.x;  shootPoint=right/2;

            canvas.drawRect(left,top,right,bottom,paint);
        }
    }

另一个名为 Java Class 的 shoot.java 也可以在点击射击按钮时投篮 但我希望当用户点击那些球(在 canvas 上绘制)时,它们应该重置

主游戏视图Class


public class GameView extends FrameLayout implements View.OnClickListener {
    //Consider all objects and variables are declared as used
    public GameView(Context context, AttributeSet attrs) {
        super(context,attrs);
        //CONTAIN INITIALIZATION OF OBJECTS AS USED  OF OTHER  CLASSES
        bullets = new ArrayList<shoot> ();
        addView(cannon);
        for (int i = 0; i < bullets.size(); i++ ) {
            addView(bullets.get(i));
            bullets.get(i).setOnClickListener(this);// an arrays of objects of shoot class 
        }
        cannon.setOnClickListener(this);
        level=3;level++;
    }

    @Override
    public void onClick(View view) {
       switch()//    Here is the MAIN PROBLEM HOW SHOULD I DIFFERENTIATE THAT CANNON IS CLICKED OR //BULLETS LIKE USING VIEW.GETTAG()
             {
              case ----1:// WHAT CASE SSHOULD I WRITE
              cannon.move();
              break;

              case ----2: // HERE ALSO
              bullets.remove();
              break;


             }
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawGameBoard(canvas);
        try
        {
            Thread.sleep(5);
        } catch (InterruptedException e) {
        }



        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        width = w;
        height = h;
        aliens.setBounds(0,0,width,height);
        for (int i = 0; i < bullets.size(); i++ ) {
            bullets.get(i).setBounds(0,0,width,height);

        }

    }

    public void drawGameBoard(Canvas canvas) {
        cannon.draw(canvas);
        for ( int i = bullets.size() - 1; i >= 0; i--) {
            if (bullets.get(i) != null) {
                bullets.get(i).draw(canvas);
             }
        }
        for (int i = explosions.size() - 1; i >= 0; i--) {
            if (explosions.get(i) != null) {
                if (!explosions.get(i).draw(canvas)) {
                    explosions.remove(i);
                }
            }
        }
           if (aliens != null) {
            aliens.draw(canvas);

            RectF guyRect = aliens.getRect();

            for (int i = bullets.size() - 1; i >= 0; i--) {
                if (RectF.intersects(guyRect, bullets.get(i).getRect())) {
                    explosions.add(new explosion(Color.RED,mContext, aliens.getX()-aliens.dst, aliens.getY()-aliens.dst));
                    aliens.reset();
                    bullets.remove(i);
                    break;
                }
            }
            if (!aliens.move()) {
                aliens = null;
            }
        }
    }
    // Whenever the user shoots a bullet, create a new bullet moving upwards
    public void shootCannon() {
        bullets.add(new shoot(Color.RED, mContext, cannon.getPosition(), (float) (height-100)));

    }
}

我已经展示了我遇到问题的代码部分,即 GAMEVIEW.JAVA 中的重写函数 ONCLICK 通过评论如何知道点击了什么所以要做他们受人尊敬的职能 如果您不明白我的问题,请通知我

如果你想知道你可以用来计算的触摸坐标,如果一个项目被点击,

考虑使用 View.OnTouchListener 而不是 View.OnClickListener

View.OnTouchListener 有这个功能:

onTouch(View v, MotionEvent event)

使用方法:

  1. 检查事件是否为点击:event.getAction() == MotionEvent.ACTION_DOWN
  2. 获取坐标:event.getX()event.getY()

然后,将 cannon.setOnClickListener(this); 替换为 cannon.setOnTouchListener(this);

如果有任何问题,请随时向他们提问!

如果我理解正确的话,你想使用一个 onClickListener 函数来处理你的大炮和子弹的点击事件。

由于两者不同 类 您可以通过“'instanceof'”来区分它们。

这意味着您的 onClickListener 看起来像这样:

@Override
public void onClick(View view) {

    if(view instanceof shooter) {
        cannon.move();
    }
    else if(view instanceof shoot) {
        bullets.remove();
    }
}

希望对您有所帮助。如果还有什么不清楚的,我会很乐意回复:)