JavaFX:如何同时移动两个矩形?
我有两个矩形代表我的乒乓球游戏的桨。我对一个矩形使用W/S,对第二个矩形使用UP/DOWN。当我按W移动一个矩形,然后按UP移动第二个矩形时,第一个矩形将停止移动,然后第二个矩形将移动。如何使两个矩形可以同时移动JavaFX:如何同时移动两个矩形?,java,javafx,keyboard,keyboard-events,Java,Javafx,Keyboard,Keyboard Events,我有两个矩形代表我的乒乓球游戏的桨。我对一个矩形使用W/S,对第二个矩形使用UP/DOWN。当我按W移动一个矩形,然后按UP移动第二个矩形时,第一个矩形将停止移动,然后第二个矩形将移动。如何使两个矩形可以同时移动 GraphicsContext-gc; 矩形播放器11; 矩形播放器22; 圆球; 私人int y1; 私有整数p1y=381; 私人互联网y2; 私有int p2y=381; 使对象动画化; 帆布; 私有AnimationTimer=新的AnimationTimer(){ 公共无效句
GraphicsContext-gc;
矩形播放器11;
矩形播放器22;
圆球;
私人int y1;
私有整数p1y=381;
私人互联网y2;
私有int p2y=381;
使对象动画化;
帆布;
私有AnimationTimer=新的AnimationTimer(){
公共无效句柄(长){
//更新桨叶位置
p1y+=y1;
p2y+=y2;
如果(p1y<0){
p1y=0;
}
if(p2y<0){
p2y=0;
}
player11.setY(p1y);
player22.setY(p2y);
}
};
public EventHandler keyreased=new EventHandler(){
公共无效句柄(KeyEvent事件){
//如果释放的键负责拨杆,则将移动设置为0
开关(event.getCode()){
案例W:
案例S:
y1=0;
打破
个案:
按大小写:
y2=0;
打破
}
}
};
private EventHandler keyPressed=new EventHandler(){
公共无效句柄(KeyEvent事件){
//根据按下的键开始移动
开关(event.getCode()){
案例W:
y1=-6;
打破
案例S:
y1=6;
打破
个案:
y2=-6;
打破
按大小写:
y2=6;
打破
}
}
};
公共静态void main(字符串[]args){
发射();
}//主要
公众假期开始(阶段){
舞台剧名(“Pong”);
组根=新组();
画布=新画布(1000800);
root.getChildren().add(画布);
场景=新场景(根,颜色.绿色);
舞台场景;
player11=新矩形(30,p1y,20,70);
player11.setFill(颜色:红色);
player22=新矩形(750,p2y,20,70);
player22.setFill(颜色:蓝色);
root.getChildren().add(player11);
root.getChildren().add(player22);
场景。设置按键按下(按键按下);
场景。设置按键释放(按键释放);
ball=新圆圈(10,颜色为深蓝色);
root.getChildren().add(ball);
重新定位(500350);
gc=canvas.getGraphicsContext2D();
animate=新的AnimateObjects();
设置.start()的动画;
stage.show();
}//开始
公共类AnimateObject扩展了AnimationTimer{
公共无效句柄(长){
}//AnimateObject类中的handle方法
}//AnimateObject类
}//乒乓球课
您需要捕获KeyCodes
。使用向上、向下、z和x控制拨杆。右挡板将不会移动超过游戏板的上限和下限。左边的桨叶会划动。代码中有更多注释!下面是一个工作示例
import java.util.HashSet;
import java.util.Set;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class App extends Application
{
Rectangle leftPaddle, rightPaddle;
AnimationTimer gameLoop;
Set<KeyCode> input;
Pane gameBoard;
@Override
public void start(Stage primaryStage)
{
leftPaddle = new Rectangle(7, 100, Color.BLACK);
leftPaddle.setX(3);
leftPaddle.setY(0);
rightPaddle = new Rectangle(7, 100, Color.BLACK);
rightPaddle.setX(500 - 10);
rightPaddle.setY(0);
input = new HashSet(); //This set is used to keep up with keys that are currently being pressed.
gameBoard = new Pane(leftPaddle, rightPaddle);
VBox.setVgrow(gameBoard, Priority.ALWAYS);
gameBoard.setOnKeyPressed(event -> input.add(event.getCode()));//add keys that are currently being pressed to the set
gameBoard.setOnKeyReleased(event -> input.remove(event.getCode()));//remove keys from the set after they are released
gameLoop = new AnimationTimer()
{
@Override
public void handle(long l)
{
movePaddle();//Call method to move paddle based on certain keys in the set
//System.out.println("playing");
}
};
Button btnStartGame = new Button("Play");
btnStartGame.setMaxWidth(Double.MAX_VALUE);
btnStartGame.setOnAction((event) -> {
gameBoard.requestFocus();//Request gameboard focus to capture keyevents
gameLoop.start();
btnStartGame.setDisable(true);
});
VBox root = new VBox(gameBoard, btnStartGame);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
//Use to move paddles based on keycodes in set
private void movePaddle()
{
if (input.contains(KeyCode.UP)) {
rightPaddle.setY(rightPaddle.getY() - 10);
if (rightPaddle.getY() < 0) {
rightPaddle.setY(0);
}
}
else if (input.contains(KeyCode.DOWN)) {
rightPaddle.setY(rightPaddle.getY() + 10);
if (rightPaddle.getY() + rightPaddle.getHeight() > gameBoard.getHeight()) {
rightPaddle.setY(gameBoard.getHeight() - rightPaddle.getHeight());
}
}
if (input.contains(KeyCode.Z)) {
leftPaddle.setY(leftPaddle.getY() - 10);
}
else if (input.contains(KeyCode.X)) {
leftPaddle.setY(leftPaddle.getY() + 10);
}
}
}
import java.util.HashSet;
导入java.util.Set;
导入javafx.animation.AnimationTimer;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.input.KeyCode;
导入javafx.scene.layout.Pane;
导入javafx.scene.layout.Priority;
导入javafx.scene.layout.VBox;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Rectangle;
导入javafx.stage.stage;
/**
*
*@author blj0011
*/
公共类应用程序扩展应用程序
{
矩形左桨,右桨;
动画定时器游戏循环;
设置输入;
窗格游戏板;
@凌驾
公共无效开始(阶段primaryStage)
{
LeftPable=新矩形(7100,颜色为黑色);
左桨组(3);
左桨。setY(0);
右桨=新矩形(7100,颜色为黑色);
右桨。setX(500-10);
右桨。setY(0);
input=new HashSet();//此集合用于跟踪当前正在按下的键。
gameBoard=新窗格(左桨、右桨);
VBox.setVgrow(gameBoard,Priority.ALWAYS);
gameBoard.setOnKeyPressed(事件->输入.add(事件.getCode());//将当前正在按下的键添加到集合中
gameBoard.setOnKeyReleased(事件->输入.remove(事件.getCode());//释放密钥后从集中移除密钥
gameLoop=新的AnimationTimer()
{
@凌驾
公共无效句柄(长l)
{
MovePable();//调用方法根据集合中的某些键移动拨片
//System.out.println(“播放”);
}
};
按钮btnStartGame=新按钮(“播放”);
btnStartGame.setMaxWidth(Double.MAX_值);
btnStartGame.setOnAction((事件)->{
gameBoard.requestFocus();//请求gameBoard focus以捕获关键事件
gameLoop.start();
btnStartGame.setDisable(true);
});
VBox根=新的VBox(gameBoard,btnStartGame);
场景=新场景(根,500500);
setTitle(“你好,世界!”);
初级阶段。场景(场景);
primaryStage.show();
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
发射(args);
}
//用于根据设置中的按键代码移动拨杆
私有void moveblade()
{
if(input.contains(KeyCode.UP)){
rightbail.setY(rightbail.getY()-10);
if(rightbail.getY()<0){
右桨。setY(0);
}
}
else if(input.contains(KeyCode.DOWN)){
右桨.setY(右桨.getY()+10);
if(rightbail.getY()+rightbail.getHeight()>gameBoard.getHeight()){
setY(gameBoard.getHeight()-rightball.getHe