开发者社区> 问答> 正文

如何从AnimationTimer对象获取特定字段?

我编写了一个简单的游戏,使用JavaFX的GUI和AnimationTimer的游戏本身,但是发现在AnimationTimer对象内更新的任何字段都不能在该对象之外找到。我已经设法连续记录了游戏的运行时间以及该游戏的得分,但是找不到从AnimationTimer对象中获取这些值的方法,因此无法将其添加到我的GUI中。

我已经尝试过java.lang.Reflect,但似乎无法正常工作。

AnimationTimer animations = new AnimationTimer() //creates animations
         {  
            @Override
            public void handle(long now) 
            {           

            //update frames           
            if(jump == 0) //moves the player model to the starting position
            {
               playerY = 160;               
            }

            else if(jump == 1) //moves the player model up (jumping)
            {
               playerY = 100;              
            }

            if(shout == 1) //makes player immune while "shouting"
            {
               player.setFill(Color.GREEN);
            }

            if(shout == 0) //makes player not immune anymore
               player.setFill(Color.BLUE);

            if(obstacleX == PLAYER_X) //updates the score
            {
               points += 10;   
               score.setText("Score: " + String.valueOf(points));
            }

            if(player.getBoundsInParent().intersects(obstacle.getBoundsInParent())) //detects if the player model touches an obstacles
            {
               if(obstacle.getEndY() == 0)
               {
                  if(player.getFill() == Color.BLUE)   
                     player.setFill(Color.RED);  
               }

               else if(obstacle.getEndY() == 130)
                  player.setFill(Color.RED);       
            }

            if(obstacleX > 0) //moves the obstacles' reference point from the right to the left
               obstacleX -= 5;

            if(obstacleX == 0)
            {
               obstacleX = 400;  
               int[] array = new int[]{0, 0, 130};
               Random randomNum = new Random();
               int i = randomNum.nextInt(array.length);
               random = array[i];
               obstacle.setEndY(random);
            }

            //render frames
            player.setCenterY(playerY); 
            obstacle.setStartX(obstacleX); //this and line 110 move the obstacle across the scene
            obstacle.setEndX(obstacleX);

         try{ //exception handler for outputs           
            if(player.getFill() == Color.GREEN)
            {
               digitalOutput6.setDutyCycle(1); //turns on green LED
               digitalOutput0.setDutyCycle(0);
            }

            if(player.getFill() == Color.BLUE)
            {
               digitalOutput6.setDutyCycle(0); //turns off LEDs
               digitalOutput0.setDutyCycle(0);
            }  

            if(player.getFill() == Color.RED) //stops the animation when the player model reacts to touching an obstacle
            {
               endTime = System.currentTimeMillis() - startTime; 
               finalScore = score.getText();
               this.stop(); //ends game                             
               gameOver = 1;              
               digitalOutput0.setDutyCycle(1);  //turns on red LED
               digitalOutput6.setDutyCycle(0);
               gameStage.setScene(gameEnd); //establishing scene
               gameStage.setTitle("Game Over"); 
               gameStage.show();                                    
            }          
           } //end of try block

           catch(PhidgetException e)
           {
            System.out.println("Phidget Output Error");
           }                              
         } //end of animation handle itself                                                            
        };    //end of animation method

我尝试使用

long finalTime = animations.getLong(endTime);

String endScore = animations.getField(finalScore);

但是没有运气。任何帮助表示赞赏。

展开
收起
垚tutu 2019-12-04 17:00:46 594 0
0 条回答
写回答
取消 提交回答
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载