簡體   English   中英

如何從 AnimationTimer 對象獲取特定字段?

[英]How can I get specific fields from an AnimationTimer object?

我已經編寫了一個簡單的游戲,使用 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://netopy.com/2012/06/14/using-the-javafx-animationtimer/
https://tbeernot.wordpress.com/2011/11/12/javafx-2-0-bubblemark/

首先不要在匿名類上使用反射它會工作,但會導致很多低效率和臟代碼。 其次,您嘗試訪問句柄(現在很長時間)的方法范圍內而不是類范圍內的字段。

        AnimationTimer t = new AnimationTimer() {
            int fieldInt = 0; // would be accessible
            @Override
            public void handle(long now) {
                //do something

            }
        };

        AnimationTimer t = new AnimationTimer() {

            @Override
            public void handle(long now) {
                int fieldInt = 0; // is not accessible as regular field
                //do something

            }
        };

您可以執行以下操作:

使用擴展 Animationtimer 的類

public class Scheduler extends AnimationTimer{

    private int gameEnd = 42;
    private String someValue = "hello";

    @Override
    public void handle(long now) {

        gameEnd += 1;

        if(gameEnd >= 1000000) {
            someValue ="Bye bye";
        }
    }

    public String getSomeValue() {
        return someValue;
    }

在這里你可以使用:


Scheduler s = new Scheduler();

s.start();

//whatever you want to do

System.out.Println(s.getSomeValue());

或者當您想使用匿名類方法時,您應該創建一個位於 AnimationTimer 之外的屬性並使用它。 (這是我更喜歡的事情,因為您可以在屬性上有一個 changeListener 並在值更改時接收回調)

    public void whateverMethod() {

        SimpleIntegerProperty gameEndProp = new SimpleIntegerProperty(42);
        AnimationTimer t = new AnimationTimer() {

            @Override
            public void handle(long now) {
                gameEndProp.set(gameEndProp.get()+1);

            }
        };

        t.start();
        //Do something
        System.out.println(gameEndProp.get());

    }


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM