开发者社区> 问答> 正文

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

我正在尝试制作一个游戏,以便当用户单击射击游戏时,它会改变位置并射击,如果用户单击射击球(或项目中命名的射击),它们就会消失。

射手


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

单击hootbutton时,还有一个名为shot.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)));

    }
}

我已经在代码部分显示了我遇到的问题,该问题是GAMEVIEW.JAVA中的重写函数ONCLICK,例如如何知道被单击的注释等,以便执行其受尊敬的函数,如果您不理解我的问题,请通知我

展开
收起
Puppet 2020-01-17 09:21:48 522 0
1 条回答
写回答
取消 提交回答
  • 如果我正确理解了该问题,则希望使用一个onClickListener函数来处理您的加农炮和子弹的单击事件。

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

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

    
    @Override
    public void onClick(View view) {
    
        if(view instanceof shooter) {
            cannon.move();
        }
        else if(view instanceof shoot) {
            bullets.remove();
        }
    }
    

    我希望这可以帮助你。如果仍然不清楚,我会很乐意回应:)

    2020-01-17 13:25:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载