Issue
So I have a code, where a circle flows around the screen. If you click the circle, it will add a point to the score. This is working, and the circle is moving.
However, the circle is getting faster, every time it gets clicked. I don't know why this happens, because I have not implemented anything like that. Below is my code and the onClickListener.
//Getting intent and checking what difficulty and setting circle color
Intent cameFrom = getIntent();
String difficulty = cameFrom.getExtras().getString(getString(R.string.difficultyIntentExtra));
if (difficulty.equals(getString(R.string.easy))) {
moverSpeed = (float) 1.0;
DrawableCompat.setTint(toMove.getDrawable(), getColor(R.color.easy));
} else if (difficulty.equals(getString(R.string.normal))) {
moverSpeed = (float) 6.0;
DrawableCompat.setTint(toMove.getDrawable(), getColor(R.color.normal));
} else if (difficulty.equals(getString(R.string.hard))) {
DrawableCompat.setTint(toMove.getDrawable(), getColor(R.color.hard));
moverSpeed = (float) 12.0;
} else {
moverSpeed = (float) 1.0;
DrawableCompat.setTint(toMove.getDrawable(), getColor(R.color.easy));
}
//OnClickListener for circle add score point
toMove.setOnClickListener(v -> {
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
scorenow++;
String newScore = getString(R.string.score) + " " + scorenow;
scoreTxt.setText(newScore);
});
//Creating the mover
final Handler leHandleur = new Handler();
final Runnable mover = new Runnable() {
@Override
public void run() {
//Get coords
float xNow = toMove.getX();
float yNow = toMove.getY();
if (((yNow <= yNext + moverSpeed) && (yNow >= yNext - moverSpeed)) || ((xNow <= xNext + moverSpeed) && (xNow >= xNext - moverSpeed))) {
//If position arrived take new Coords
float[] newCoordiantes = randomGenerator(xWidth, yHeight, 0, 0);
xNext = newCoordiantes[0];
yNext = newCoordiantes[1];
}
if (xNow < xNext) {
xNow += moverSpeed;
} else if (xNow > xNext) {
xNow -= moverSpeed;
}
if (yNow < yNext) {
yNow += moverSpeed;
} else if (yNow > yNext) {
yNow -= moverSpeed;
}
toMove.setX(xNow);
toMove.setY(yNow);
//Handler delay
leHandleur.postDelayed(this, 0);
}
};
//Getting width and height of the layout, after it was created
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
xWidth = rootLayout.getWidth() - toMove.getWidth() - moverSpeed; //Width without max width movement
yHeight = rootLayout.getHeight() - toMove.getHeight() - moverSpeed; //Height without max height movement
Log.d("Max_Heigth_Width", "onCreate: \nHeight: " + yHeight + "\nWidth: " + xWidth); //Log
//Running mover
leHandleur.post(mover);
});
As you can see, there is nothing that could make the circle move faster in the onClickListener.
Solution
I found the problem. My ViewTreeObserver always started the mover runnable as soon, as something in the view changed, what happened, when the circle was clicked. To fix it, I simply stopped the runnable, because it will be activated later, anyways:
leHandleur.removeCallbacks(mover);
Answered By - Tschogge
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.