gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:53 24 4
gpt4 key购买 nike

我已经编写了一个简单的游戏,使用 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);

但运气不佳。任何帮助表示赞赏。

最佳答案

AnimationTimer 本身不公开任何属性,创建一个将 AnimationTimer 对象包装在其中的自定义类,并保存您想要访问的任何属性。

当您初始化自定义类时,初始化 AnimationTimer 对象并覆盖该函数(如您所做的那样),还更新您想要公开的任何属性。

我对javafx不熟悉,可能还有其他更好的方法,这是我能想到的一个快速解决方案。

快速搜索一下,这些示例可能会有所帮助:
https://netopyr.com/2012/06/14/using-the-javafx-animationtimer/
https://tbeernot.wordpress.com/2011/11/12/javafx-2-0-bubblemark/

关于java - 如何从 AnimationTimer 对象获取特定字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59166518/

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