我有这个代码,每当球穿过矩形中的间隙时,应该为scoreTextView添加1个点。我的方法.getTheRight得到了矩形的右边。这是我的代码:
public void onResume() //app moved to foreground (also occurs at app startup)
{
final TextView scoreTextView = (TextView)findViewById(R.id.scoreTextView);
final int[] blackRectangleTopHeights = {0, (mScrHeight/10) * 1, (mScrHeight/10) * 2,(mScrHeight/10) * 3,
(mScrHeight/10) * 4,(mScrHeight/10) * 5,(mScrHeight/10) * 6,
(mScrHeight/10) * 7};
final int[] blackRectangleBottomHeights = {(mScrHeight/10) * 3,(mScrHeight/10) * 4,(mScrHeight/10) * 5,
(mScrHeight/10) * 6, (mScrHeight/10) * 7,(mScrHeight/10) * 8,
(mScrHeight/10) * 9,mScrHeight};
final Random randomGenerator = new Random();
runCircle();
//************Set up rectangle moving mechanism and Logic*********************
mTmr2 = new Timer();
mTsk2 = new TimerTask() {
@Override
public void run() {
int randomNumber = randomGenerator.nextInt(blackRectangleTopHeights.length);
int topBlackRectHeight = blackRectangleTopHeights[randomNumber];
int bottomBlackRectHeight = blackRectangleBottomHeights[randomNumber];
int rectWhite1Left = rectWhite1.getTheLeft() - mRectSpeed;
int rectWhite1Right = rectWhite1.getTheRight() - mRectSpeed;
rectWhite1.setX(rectWhite1Left, rectWhite1Right);
int rectBlack1Left = rectBlack1.getTheLeft() - mRectSpeed;
int rectBlack1Right = rectBlack1.getTheRight() - mRectSpeed;
rectBlack1.setX(rectBlack1Left, rectBlack1Right);
int rectWhite2Left = rectWhite2.getTheLeft() - mRectSpeed;
int rectWhite2Right = rectWhite2.getTheRight() - mRectSpeed;
rectWhite2.setX(rectWhite2Left, rectWhite2Right);
int rectBlack2Left = rectBlack2.getTheLeft() - mRectSpeed;
int rectBlack2Right = rectBlack2.getTheRight() - mRectSpeed;
rectBlack2.setX(rectBlack2Left, rectBlack2Right);
int rectWhite3Left = rectWhite3.getTheLeft() - mRectSpeed;
int rectWhite3Right = rectWhite3.getTheRight() - mRectSpeed;
rectWhite3.setX(rectWhite3Left, rectWhite3Right);
int rectBlack3Left = rectBlack3.getTheLeft() - mRectSpeed;
int rectBlack3Right = rectBlack3.getTheRight() - mRectSpeed;
rectBlack3.setX(rectBlack3Left, rectBlack3Right);
//Cycles rectangles and randomizes where middle rectangle will be postitoned vertically
if (rectWhite1.getTheRight() > 0 && rectWhite1.getTheRight() < 10) {
rectWhite1.setX(mScrWidth, rectWhite1.getRectWidth() + mScrWidth);
}
if (rectBlack1.getTheRight() > 0 && rectBlack1.getTheRight() < 10) {
rectBlack1.setX(mScrWidth, rectBlack1.getRectWidth() + mScrWidth);
rectBlack1.setY(topBlackRectHeight, bottomBlackRectHeight);
}
if (rectWhite2.getTheRight() > 0 && rectWhite2.getTheRight() < 10) {
rectWhite2.setX(mScrWidth, rectWhite2.getRectWidth() + mScrWidth);
}
if (rectBlack2.getTheRight() > 0 && rectBlack2.getTheRight() < 10) {
rectBlack2.setX(mScrWidth, rectBlack2.getRectWidth() + mScrWidth);
rectBlack2.setY(topBlackRectHeight, bottomBlackRectHeight);
}
if (rectWhite3.getTheRight() > 0 && rectWhite3.getTheRight() < 10) {
rectWhite3.setX(mScrWidth, rectWhite3.getRectWidth() + mScrWidth);
}
if (rectBlack3.getTheRight() > 0 && rectBlack3.getTheRight() < 10) {
rectBlack3.setX(mScrWidth, rectBlack3.getRectWidth() + mScrWidth);
rectBlack3.setY(topBlackRectHeight, bottomBlackRectHeight);
}
//Keeps track of score
if (rectWhite1.getTheRight() == Math.round(mBallPos.x)) {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore() + "");
Log.d(TAG, "If statement has been run!!!");
}
if (rectWhite2.getTheRight() == Math.round(mBallPos.x)) {
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore() + "");
Log.d(TAG, "If statement has been run!!!");
}
if (rectWhite3.getTheRight() == Math.round(mBallPos.x)) {
// Update TextView here
mScoreboard.addPoint();
scoreTextView.setText(mScoreboard.getScore() + "");
Log.d(TAG, "If statement has been run!!!");
}
RedrawHandler2.post(new Runnable() {
public void run() {
rectWhite1.invalidate();
rectWhite2.invalidate();
rectWhite3.invalidate();
rectBlack1.invalidate();
rectBlack2.invalidate();
rectBlack3.invalidate();
}
});
}
};
mTmr2.schedule(mTsk2, 10, 10); //start timer (Timer 2)
super.onResume();
} // onResume
相反,没有发生TextView更新,分数保持不变。我不明白为什么每次球的x位置等于要传递的矩形的右点的x位置时它都不会更新。