首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnMouseRelease的行为类似于OnMouseClick

OnMouseRelease的行为类似于OnMouseClick
EN

Stack Overflow用户
提问于 2019-05-03 10:37:48
回答 1查看 153关注 0票数 0

我有一个应用程序,它有一个球打破了一些块,但游戏重新开始时,鼠标按键释放,而不是点击。

我创建了这个应用程序,这样游戏只需单击鼠标即可开始。如果球击中底部窗格而不是球拍,那么游戏将被重置,玩家将失去一条生命。球拍也是由鼠标控制的,所以在理论上,当球没有命中并且游戏重新开始时,鼠标在使用球拍时仍然会被按下。不幸的是,我似乎不能修复的问题,松开鼠标键从控制桨重新启动游戏。我已经尝试添加了强制释放、计数器、结合使用onMouseDragReleased和onDragDetected,以及其他组合。下面是一些部分代码,其中包含了我认为与我的问题相关的领域。我尝试过的一些不同方面都被注释掉了。

代码语言:javascript
复制
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

我点击它,它就会开始游戏。当我再次点击鼠标来控制球拍时,保持点击计数器可以防止游戏重新开始。然而,计数器和它们的位置的变化都不能阻止游戏在鼠标释放时立即开始。我正在使用Scene Builder,并且我目前已经尝试了以下操作:

startGameWithMouseClicked作为窗格的onMouseClicked事件selectPaddleWithMousePressed作为划板的onDragDetected或onMousePressed movePaddleWithMouseDragged作为划板onMouseDragReleased或onMouseDragged stopPaddleWithMouseReleased作为划板onMouseDragReleased或onMouseReleased

我还尝试将面板的所有事件和无事件都设置为pane,以防pane来回拖动导致光标在拖动和释放时不再位于pane上。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-04 23:11:22

我发现鼠标事件总是被按下,释放,点击-所以在我认为它会在从拖动运动中释放后停止的地方,点击事件仍然会发生。我最终创建了一个变量,让它在游戏开始时在点击事件中增加(在if var == 0内),在每次按下/释放时增加和减少,然后在点击事件中最后减少,使它返回到0(只有当球不再移动时)作为else语句(所以当var != 0时)。

这是我的逻辑:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55962615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档