gpt4 book ai didi

java - OnMouseRelease 的作用类似于 OnMouseClick

转载 作者:行者123 更新时间:2023-12-02 06:02:45 26 4
gpt4 key购买 nike

我有一个应用程序,它的球会打破一些方 block ,但当释放鼠标按钮而不是单击时,游戏会重新启动。

我创建了该应用程序,以便通过单击鼠标即可开始游戏。如果球击中底部 Pane 而不是 Racket ,则游戏将重置并且玩家会失去生命。 Racket 也是由鼠标控制的,因此理论上,当球未击中并且游戏重置时,鼠标仍然会被按下以使用 Racket 。不幸的是,我似乎无法解决释放鼠标按钮来控制桨重新启动游戏的问题。我尝试过添加强制释放、计数器、使用 onMouseDragReleased 和 onDragDetected 以及其他组合。下面是一些部分代码,其中包含我认为与我的问题相关的区域。我尝试过的一些不同方面已被注释掉。

public class BBController {
boolean start = true; //check if can start game
int pl = 0; //counter for checking mouse release
int cl = 0; //counter for checking mouse clicks
Timeline animateBall; //move ball
ArrayList<Rectangle> rec; //ArrayList to house all of the "blocks"

//GUI components
...

//Start the game
@FXML void startGameWithMouseClicked(MouseEvent event) {
if (cl == 0){ //haven't clicked to start game
animateBall = new Timeline( //for making the ball move around
new KeyFrame(Duration.millis(12),
new EventHandler<ActionEvent>() {
int dx = 2;
int dy = 2;

@Override
public void handle(ActionEvent e){
ball.setLayoutX(ball.getLayoutX() + dx);
ball.setLayoutY(ball.getLayoutY() + dy);
Bounds b = pane.getBoundsInLocal();

if (hitSides(b)){ //ball hits left or right side
...
}
//ball hits top of pane, the paddle, or a block
if (hitTop(b) || hitPaddle() || hitBlock()){
...
}
if (rec.size() == 0){ //if hit all of the blocks
...
}
if (hitBottom(b)){ //if ball hits bottom pane
//stop the bouncing
animateBall.setCycleCount(0);
animateBall.stop();
//next mouse click can start game if lives left
cl -= 1;
cb.setText(Integer.toString(cl));
/*if (pl==0){cl -= 1;}
cb.setText(Integer.toString(cl));
//reset paddle
if (pl > 0){
Event.fireEvent(pane, new MouseEvent(
MouseEvent.MOUSE_RELEASED,0, 0, 0, 0,
MouseButton.PRIMARY, 1, true, true, true,
true, true, true, true, true, true, true,
null));
}*/




//if (pl > 0){pl -=1;}
//pb.setText(Integer.toString(pl));

paddle.setLayoutX(250);
//reset the ball's position
ball.setLayoutX(300);
ball.setLayoutY(371);
//lose a life
livesTotalBox.setText(Integer.toString(
Integer.parseInt(
livesTotalBox.getText()) - 1));
if (Integer.parseInt( //out of lives
livesTotalBox.getText()) == 0){
start = false; //can't play any more
gameOver.setStyle(
"-fx-background-color: YELLOW");
gameOver.setVisible(true);
}
}//end hitBottom
}//end handle
}//end EventHandler
)//end KeyFrame
);//end Timeline

if(start == true){//if lives avail & mouse events cleared
cl += 1; //future clicks don't try to start game again
cb.setText(Integer.toString(cl));
animateBall.setCycleCount(Timeline.INDEFINITE);
animateBall.play();
}
} //end if (cl == 0)
}//end startGameWithMouseClicked

private boolean hitSides(Bounds b){ //check if ball hits sides of pane
...
}

private boolean hitTop(Bounds b){ //Check if ball hits top of pane
...
}

private boolean hitBottom(Bounds b){ //check if ball hits bottom of pane
...
}

private boolean hitPaddle(){ //check if ball hits paddle
...
}

private boolean hitBlock(){ //check if ball hits a block
...

} //end hitBlock

//Control the paddle
@FXML void selectPaddleWithMousePressed(MouseEvent event) {
// pl += 1; //mouse button pressed outside of starting game
// pb.setText(Integer.toString(pl));
paddle.getLayoutX();
}
@FXML void movePaddleWithMouseDragged(MouseEvent event) { //move paddle
if (event.getSceneX() <= 0){ //paddle can't go beyond left side pane
paddle.setLayoutX(0);
}
else if (event.getSceneX() >= 500){ //paddle can't go beyond right side
paddle.setLayoutX(500);
}
else {paddle.setLayoutX(event.getSceneX());} //paddle anywhere btw sides
}
@FXML void stopPaddleWithMouseReleased(MouseEvent event) {
/* if (pl > 0) {//mouse was pressed outside of starting game
pl -= 1;
pb.setText(Integer.toString(pl));
//leave paddle where you released
paddle.setLayoutX(paddle.getLayoutX());
}
else {//game needs to reset
paddle.setLayoutX(250);
pb.setText(Integer.toString(pl));
//next mouse click can start game if lives left
cl -= 1;
cb.setText(Integer.toString(cl));
}*/

if (cl == 0) { paddle.setLayoutX(250); }
else { paddle.setLayoutX(paddle.getLayoutX()); }
}

// called by FXMLLoader to initialize the controller
public void initialize() {
...
}//end initialize
}//end BBController

我点击并开始游戏。当我第二次单击鼠标来控制桨时,保留点击计数器可以防止游戏重新启动。然而,计数器及其位置的任何变化都不能阻止游戏在鼠标释放时立即开始。我正在使用场景生成器,目前我已尝试以下操作:

startGameWithMouseClicked 作为 Pane 的 onMouseClicked 事件selectPaddleWithMousePressed 作为桨的 onDragDetected 或 onMousePressedmovePaddleWithMouseDragged 作为桨 onMouseDragReleased 或 onMouseDraggedstopPaddleWithMouseReleased 作为桨 onMouseDragReleased 或 onMouseReleased

我还尝试将它们设置为 Pane 的所有事件,而不将其作为桨,以防来回拖动桨导致光标在拖动和释放时不再位于桨上。

任何建议将不胜感激,因为这是我的应用程序的一个恼人的错误。谢谢。

最佳答案

我发现鼠标事件总是按下、释放、单击 - 所以我认为从拖动 Action 释放后它会停止,但单击事件仍然必须发生。我最终创建了一个变量,并让它在游戏开始时在点击事件中增加(在 if var == 0 内),在每次按下/释放时增加和减少,然后在点击事件中最终减少以获得将其返回到 0(仅当球不再移动时)作为 else 语句(因此当 var != 0 时)。

这是我的逻辑:

click event
if counter = 0
start game
counter + 1
if counter != 0
if ball moving, do nothing //for if you press/release multiple times while dragging paddle
if ball not moving, counter - 1 //next click can start game
end click event

press event
counter + 1
end press event

drag event
move paddle
end drag event

release event
counter -1
end release event

关于java - OnMouseRelease 的作用类似于 OnMouseClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com