【问题标题】:How to use onClickListener on two CanvasView Objects?如何在两个 CanvasView 对象上使用 onClickListener?
【发布时间】:2020-01-16 19:32:05
【问题描述】:

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

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

另一个名为 shoot.java 的 Java 类也用于在单击击球按钮时制作投篮球 但我希望当用户点击那些球(画在画布上)时,他们应该重置

主游戏视图类


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

    }
}

我已经展示了我遇到问题的代码部分,即 cmets 重写的函数 ONCLICK in GAMEVIEW.JAVA,比如如何知道点击了什么,所以要执行他们尊重的函数 如果您不理解我的问题,请通知我

【问题讨论】:

  • 忘了提到射击球是画在画布上的圆圈,如果它像 python 中一样有帮助的话,就像射手大炮一样 .type 函数有帮助

标签: java android switch-statement onclicklistener android-canvas


【解决方案1】:

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

由于两者是不同的类,您可以通过 '''instanceof''' 区分它们。

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

@Override
public void onClick(View view) {

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

我希望这对你有帮助。如果还有什么不清楚的地方,我很乐意回复:)

【讨论】:

  • 但是当我点击第一个案例运行时,它并没有区分点击......
  • 这听起来很奇怪。在您的代码示例中,shooter 继承了 View。您的项目符号是shoot 类型,如果shoot 继承shooter,它将解释为什么两种点击类型都会触发第一种情况。
【解决方案2】:

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

考虑使用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);

如果有任何问题,请不要犹豫!

【讨论】:

  • 如何检查拍摄对象是否被点击或射手能否请您更详细地了解在我的项目中使用 ontouchlistener
  • 例如,您的圈子可以存储为Ellipse2d。在onTouch 方法中,比如if([your ellipse2d].contains([your x], [your y]))
猜你喜欢
  • 1970-01-01
  • 2015-11-11
  • 2021-08-27
  • 1970-01-01
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
相关资源
最近更新 更多