早些时候,我在单词对撞机程序中专门针对animate方法提出了一个问题。这个程序的目的是当两个单词碰撞时,单词会爆炸,每个单独的角色将随机散布在屏幕上。以下是我的程序的代码;
/**
* Write a description of class WordCollider here.
*
* @author
* @version
*/
public class WordCollider
{
// instance variables - replace the example below with your own
private Text word1;
private Text word2;
// the characters contained in word1
private Text[] charWord1;
// the characters contained in word2
private Text[] charWord2;
/**
* Constructor for objects of class WordCollider
*/
public WordCollider(String w1, String w2)
{
// initialise instance variables
word1 = new Text(w1);
word1.randomizePosition();
word1.changeColor("green");
word1.changeSize(48);
word1.makeVisible();
word2 = new Text(w2);
word2.randomizePosition();
word2.changeColor("orange");
word2.changeSize(48);
word2.makeVisible();
charWord1 = new Text[w1.length()];
charWord2 = new Text[w2.length()];
fillChars(charWord1, w1);
fillChars(charWord2, w2);
}
private void fillChars(Text[] a, String w) {
char[] cs = w.toCharArray();
for (int i=0; i<a.length; i++) {
a[i] = new Text(""+cs[i]);
a[i].changeSize(48);
a[i].changeColor("red");
}
}
/**
* Randomize the position of the two words repeatedly and stop
* when the bounding box of the two words overlaps.
*/
public void animate()
{
while(checkOverlap() == false){
word1.randomizePosition();
word2.randomizePosition();
}
while(checkOverlap() == true){
word1.makeInvisible();
word2.makeInvisible();
}
for(int i = 0; i < charWord1.length; i++){
word1.makeVisible();
word1.randomizePosition();
}
for(int i = 0; i < charWord2.length; i++){
word2.makeVisible();
word2.randomizePosition();
}
}
/**
* erase the words and any other characters on the display
*/
public void clearDisplay() {
word1.makeInvisible();
word2.makeInvisible();
}
/**
* check if the bounding box of the two words overlaps.
* @return true when the words overlap and false otherwise.
*/
private Boolean checkOverlap() {
if (word2.getXPosition() < word1.getXPosition() + word1.getTextWidth()){
return true;
}
if (word1.getXPosition() > word2.getXPosition() + word2.getTextWidth()){
return true;
}
if (word2.getYPosition() < word1.getYPosition() - word1.getTextHeight()){
return true;
}
if (word1.getYPosition() > word2.getYPosition() - word2.getTextHeight()){
return true;
}
if (word2.getXPosition() < word1.getXPosition() + word1.getTextWidth() && word1.getXPosition() > word2.getXPosition() + word2.getTextWidth()){
return true;
}
if (word2.getYPosition() < word1.getYPosition() - word1.getTextHeight() && word1.getYPosition() > word2.getYPosition() - word2.getTextHeight()){
return true;
}
return false;
}
}
animate方法的目的是,如果checkOverlap为false,单词将在屏幕上随机化,而如果checkOverlap为true,那么我想让单词不可见并将字母分散在屏幕上
清晰显示方法的目的只是让屏幕上的单词不可见。
checkOverlap方法的目的是检查两个单词是否重叠,如果它们重叠,则return语句将为true,如果它们不重叠,则返回false
我如何解决这个问题,以便我的计划有效?