mardi 13 février 2018

Android: If statement in onCLick method does not get executed

I have two views, in this case circleView1 and circleView2. You have to touch them both at the same time so they move to a random position on the screen.

This is what my onClickListener looks like:

View.OnClickListener ocl = new View.OnClickListener() {

                    public void onClick(View v) {

                        if (circleView1.isPressed() && circleView2.isPressed()) {
                            setCirclesRandomPosition(circleView1, circleView2);
                            counter++;
                            scoreGM2.setText(Integer.toString(counter));
                            startGame2.setVisibility(View.INVISIBLE);
                        }


                        if (circleView1.isPressed() && circleView2.isPressed() && counter == 1) {
                            timer.start();
                        }
                    }
        };

        circleView1.setOnClickListener(ocl);
        circleView2.setOnClickListener(ocl);

The following code gets executed

if (circleView1.isPressed() && circleView2.isPressed() && counter == 1) {timer.start();}

But this doesn't

if (circleView1.isPressed() && circleView2.isPressed()) {
                            setCirclesRandomPosition(circleView1, circleView2);
                            counter++;
                            scoreGM2.setText(Integer.toString(counter));
                            startGame2.setVisibility(View.INVISIBLE);
                        }

The "same" code from another class works with just one view and looks like this:

neoncircle.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               setCircleRandomPosition(neoncircle);
               counter++;
               scoreGM1.setText(Integer.toString(counter));
               startGame1.setVisibility(View.INVISIBLE);

               if (counter == 1) {
                   timer.start();
               }
           }
       });

The onClickListener for circleView1 and circleView2 looks different than for one view because this was the only way I figured out how to make it work for two simultaneous clicks.

I didn't post the whole code because it's really long. But if you need the whole code I'll post it.

Aucun commentaire:

Enregistrer un commentaire