我当时正在编写一款受Conway的“生活游戏”启发的游戏。
尽管我已经弄清了整个游戏的逻辑(但未编码),但是在玩家的第一回合结束后,仍然无法使矩形对象的填充颜色发生变化。当我运行程序时,它会跳过对播放器一个人的颜色(Color.BLUE)的要求,而直接转到播放器第二个人的颜色(Color.RED)。
这是代码:
//William Fisher
//July.11.2017
package cellularautomatagame;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.canvas.*;
import javafx.scene.input.MouseEvent;
import static javafx.scene.paint.Color.*;
public class CellularAutomataGame extends Application {
@Override
public void start(Stage stage) {
Group root = new Group();
Scene s = new Scene(root, 300, 300, Color.BLACK);
Canvas canvas = new Canvas(1280,720);
GraphicsContext gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
stage.setScene(s);
stage.show();
gc.setFill(WHITE);
gc.fillRect(0, 0, 5, 720);
gc.fillRect(0, 0, 1280, 5);
gc.fillRect(0, 715, 1280, 5);
gc.fillRect(1275, 0, 5, 720);
Player player1 = new Player();
Player player2 = new Player();
player1.playerFirstMove(root,canvas,Color.BLUE);
player2.playerFirstMove(root,canvas,Color.RED);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
//William Fisher
// July.11.2017
package cellularautomatagame;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import static javafx.scene.paint.Color.*;
import javafx.scene.shape.Rectangle;
public class Player {
int firstMove = 0;
public void playerFirstMove(Group root,Canvas canvas,Color color){
canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
@Override
public void handle (MouseEvent e){
while(firstMove < 1){
if(e.getClickCount() == 1){
Rectangle r = new Rectangle(e.getX(),e.getY(),5,5);
r.setFill(color);
root.getChildren().add(r);
firstMove++;
}
}
}
});
firstMove--;
}
}
/** (07/11/2017)Current Problem: The first player is unable to make their first move. Only the
* second player is able to make a first move.
*
* (07/16/2017)Current Problem: Same as previous problem, changed the code so that a rectangle
* object would spawn upon mouse click event. Problem possibly has to do with "setFill()" function.
*/
在主要JavaFX方法的第52行上,它显示了player1的第一个回合,它应该调用playerFirstMove方法,并在单击鼠标后产生一个蓝色矩形,如从Player类的第18行开始的playerFirstMove方法所示。但是,单击鼠标时,会生成一个红色矩形,而不是蓝色矩形。好像该程序跳过了第52行的player1.playerfirstMove(...)并直接进入了第53行的player2.playerfirstMove(...)。 JavaFX API并搜索互联网。该程序正在执行我想要的操作(每单击一次鼠标仅生成一个矩形),但好像它正在跳过第52行上的指令(player1.playerfirstMove(...))。
在我看来,似乎有一个涉及setFill()函数的错误?
我将不胜感激任何帮助。
谢谢大家!
最佳答案
如果我正确理解,则每次单击鼠标都应绘制当前玩家的矩形,然后将转向转到下一个玩家。如果是这样,我会重新编写您的代码,使Player具有颜色并仅绘制矩形逻辑:
class Player {
private final Color color;
Player(final Color color) {
this.color = color;
}
void doSomething(final Group root, final double x, final double y) {
Rectangle r = new Rectangle(x, y, 5, 5);
r.setFill(color);
root.getChildren().add(r);
}
}
在主类中,我组织了循环迭代(通过使用Google guava collection utils),并且迭代器仅允许与当前播放器一起使用:
Player player1 = new Player(Color.BLUE);
Player player2 = new Player(Color.RED);
Player player3 = new Player(Color.YELLOW);
final Iterator<Player> playerIterator = Iterators.cycle(player1, player2, player3);
canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
if (e.getClickCount() == 1) {
playerIterator.next().doSomething(root, e.getX(), e.getY());
}
});
结果,我什至可能有3个玩家,每次点击只会触发下一个玩家:
顺便说一句,该解决方案允许根据需要拥有任意数量的玩家。