Friday, July 1, 2022
HomeGame DevelopmentJavaFX sport, a number of ranges utilizing AnimationTimer

JavaFX sport, a number of ranges utilizing AnimationTimer


I’m a newbie in JavaFX, have been writing a easy sport, a vertical 2nd shooter, to be taught programming. Initially a quite simple idea, however I’ve been including extra stuff with time… to the purpose a number of the preliminary options used are now not usable.

Initially the sport loop used AnimationTimer class to run participant’s and different gameEntity (impartial objects and enemies) motion (a few of them use advanced paths and use PathTransition although), taking pictures, replace all different stuff. Simplified, it appears like this:

    non-public void gameLoop() {       
        int counter = 0;                                                            //ensures counter is zeroed in the beginning of every stage
        AnimationTimer mainTimer = new AnimationTimer() {
            @Override
            public void deal with(lengthy now) {  
                ++counter;                                                      //used for enemy spawning
                replace();                                                       //manages collision detection, background scrolling and so on.
                movePlayer();                                                   //strikes the participant
                if (isShooting) {
                    shoot();                                                    //controls taking pictures and weapon choice
                }
            }
        };
        mainTimer.begin();      
    }

Initially it additionally managed spawning gameEntities. gameEntity spawning shouldn’t be random, I would like sure of these to seem at sure time, thus the counter within the gameLoop() methodology; principally the thought is as follows:

if (counter == ONE_SEC * 5 && counter == ONE_SEC * 10 ) {
     gameEntity = gameEntity.bomber(airObjectLayer);             //instance - spawns enemy "bomber" after 5 and 10 seconds 
                                                                 //and provides it into one among gamefield layers
}

After the extent ends (when background stops scrolling, sometimes 180 secs), a abstract display screen is displayed, the levelSelector variable is elevated by one and one other stage could be began.

That strategy labored when there was just one stage as I might put all of the gameEntity spawning in the identical deal with() methodology because the replace(), movePlayer() and so on. With a number of ranges I bumped into issues. Tried having each stage use its personal AnimationTimer for testing, however I perceive this isn’t an answer.

May anybody advise learn how to clear up this? Have an AnimationTimer to handle the gameLoop and by some means “feed” the spawning information from completely different ranges into it? Wrap a counter into observable and spawn gameEntities from the Stage class on the identical precept as described above (the “if (counter ==)” strategy)? Or possibly not use AnimationTimer however another means?
Sorry if that is trivial, I wasted a lot time on this I principally bought misplaced.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments