I'm new to Java and im trying to create a game that uses an AnimationTimer. I want to loop through the question one at a time but at the moment the for loop I use just adds all the question to the canvas at the start of the game. I would be grateful if someone could point me in the right direction as to what to do. Here in a sample of my code (I know it's wrong). Each question is an object stored in an arraylist.

AnimationTimer timer = new AnimationTimer() {

        public void handle(long arg0) {
            // TODO Auto-generated method stub

            gc.fillRect(player.r.getX(), player.r.getY(),
                    player.image.getWidth(), player.image.getHeight());

            gc.drawImage(player.image, player.r.getX(), player.r.getY());

            for (Question quest : questions) {

                gc.drawImage(quest.image, quest.r.getX(), quest.r.getY());

                if (player.r.getBoundsInParent().intersects(
                        quest.r.getBoundsInParent())) {
                    collisionDetected = true;
                    gc.fillRect(quest.r.getX(), quest.r.getY(), 50, 50);

actually, you are looping through them one at the time. it just happens so fast after each other, you wouldn't be able to tell.

just keep the questions in a list, and, instead of a for loop, use a JButton to go to the next question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.