Search code examples
javaandroidswitch-statementonclicklistenerandroid-canvas

How to use onClickListener on two CanvasView Objects?


I am trying make a game such that when user click on shooter it changes its position and and shoots and if user click on shooted balls (or shoots as named in project) they disappear CODE HERE

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

Another Java Class Named shoot.java is also there to make shoot balls when shootbutton is clicked but I want that when user click on those balls(drawn on canvas ) they should reset

Main Game View 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)));

    }
}

I have showed the part of the code where I am having the problem that is the overridden function ONCLICK in GAMEVIEW.JAVA by comments like how to know what is clicked so todo their respected functions please notify me if you didn't understand my question


Solution

  • If I understand the question correctly, you want to use one onClickListener function to handle the click events for both your cannon and your bullets.

    Since both are different classes you may differentiate between them via '''instanceof'''.

    That means your onClickListener would look something like this:

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

    I hope this helps you. If anything remains unclear I'll gladly response :)