mercredi 18 avril 2018

Is it possible to make a method run for a period of time?(beginner)

I've got a uni ball maze java project(CBallMaze on internet & Github), and I want to make the ball move automatically to end goal by calling methods for each 4 directions. I wanted to know if I could place time interval to each method, that would cause the methods to last for their set time interval - or if there are other approaches to this matter.

Below, is the current code for my Run button

        if (source == buttonRun) {
        timer = new Timer(1000, this);
        timer.start();
        System.out.println("The program's timer has started");
        if (Maze == Maze1) {

            for (int i=0;i<2;i++) {
            moveLeft();
            moveLeft();
            moveLeft();
            //2nd section//
            moveDown();
            moveDown();
            moveDown();
            //3rd section//
            moveLeft();
            moveLeft();
            //4th section
            moveDown();
            moveDown();
            moveDown();
            //5th section//
            moveRight();
            moveRight();
            //6th section//
            moveDown();
            moveDown();
            moveDown();
            //7th section//
            moveRight();
            moveRight();
            moveRight();
            //8th section//
            moveDown();
            moveDown();
            moveDown();
            //9th section//
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
            moveLeft();
        }
    }
    }

The code does move the ball, but does not get it to its destination and cannot seem to make the ball move to each tile on its own at a decent pace for a user to see. Any help / guidance will be appreciated.

Aucun commentaire:

Enregistrer un commentaire