Question

I posted a question and got a fab answer.

I took their advice and tried to implement a timer to get my Java game looping.

    public static void run()
    {
        init();
        long lastTime = System.nanoTime();
        final double amountOfTicks = 60D;
        double ns = 1000000000/ amountOfTicks;
        double delta = 0;

        while(running)
        {
            long now = System.nanoTime();//gets time from pc in nano seconds 
            //tick();//this is the problem, slow computer will slow things down
            delta += (now - lastTime) / ns;
            lastTime = now;
            if(delta >=1)
            {
                tick();//limited to 60 times a second 
                delta--;
            }
            render();//render and draw graphics many times a second// as fast as we can 
        }
        stop();
    }

    public void tick()
    {
        //update all game components
        runTimer();
    }

    public void render()
    {
        //draw the images 
        //first time game runs
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null)//bs equalnothing create 
        {
            createBufferStrategy(3);
            return;//exit render method until called again 
        }

        Graphics g = bs.getDrawGraphics();//paint brush, paints all sorts, draw text lines images etc. paint brush yeah
        //render here i guess
        createGUI();            
        //end the render please
        g.dispose();
        bs.show();


    }

    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Cannot make a static reference to the non-static method init() from the type           CbabyBall
Cannot make a static reference to the non-static method tick() from the type  CbabyBall
Cannot make a static reference to the non-static method render() from the type CbabyBall
Cannot make a static reference to the non-static method stop() from the type CbabyBall

I have tried implanting a timer from looking at the oracle and other examples but my code that I've implemented does not like it.

What I have is a mouse click and when you click on to a grid (2D array), the player X and Y pos should go to that position.

    private void runTimer()
{

    if(BPOS_X!=MOUSE_X && BPOS_Y != MOUSE_Y){
        if(BPOS_X<MOUSE_X)
        {
            if(MOUSE_X+1==this)
            {
                //moveBallR();
                //moveRight();
                moveUp();
            }
            else
            {
                moveRight();
            }
        }
        if(BPOS_X>MOUSE_X)
        {
            moveLeft();
        }
        if(BPOS_Y<MOUSE_Y)
        {
            moveDown();
        }
        if(BPOS_Y>MOUSE_Y)
        {
            moveUp();
        }
        }
        else if(BPOS_X==MOUSE_X && BPOS_Y == MOUSE_Y)
        {
        }

}

I tried catching this in a loop and Eclipse has crashed and i'm too scared to try the loop again.

Can someone please give me a healing hand?

    public class CbabyBall extends JFrame

    public KeyListener listenKey;
    public ActionListener listenAction;
    public ChangeListener  listenChange;
Was it helpful?

Solution

You state:

What I have is a mouse click and when you click on to a grid (2D array), the player X and Y pos should go to that position.

This doesn't sound like it needs a "Game Loop" at all. What you need for this behavior is simply a MouseListener, and that's it.

Also and to reiterate my comment, I fear that the advice you received in your other recent question was not good advice, since they advised you to use a java.util.Timer with a Swing application, and that can be dangerous. Usually it's better to use a javax.swing.Timer also known as a "Swing Timer".

For better advice, you will want to give more details of just what you're trying to create.


Edit
You state:

I want to refresh my Jpanel and make it look like as if the square is moving to that position.

So do that -- in a MouseListener.

OTHER TIPS

if the latter code which you supplied is actually inside a loop you have an immediate problem; it's always going to move if the mouse is not on the same spot.

Secondly, what is most likely the problem is that init() is not actually a static method, in which you have dubbed run as one; this results in a clash because you have not explicitly changed init() to a static and all variables inside of a static method must be have a static access modifier.

If it is not necessary for run to be a static method then I would change it. A way which you could change it would be like this:

class Main {
    public Thread runThread = new Thread() {
        public void run() {
        //Put Code Here...
        }
    }

    public static void main(String args[]) {
         Main m = new Main();
         m.runThread().start();
    }
}

Hope This Helps :)

P.S @HoverCraftFullOfEels means that you shouldn't focus on the timer you should work on actually making sure the rest of your code works before you do anything else

P.P.S Here Is How The JPanel Extension Works:

`class mainRun extends JPanel {

 public void paintComponent() {//Code In Here}

 public static void main(String args[]) {
 JFrame frame = new JFrame("Title"); 
 //JFrame Code

 mainRun mr = new mainRun();

 KeyEvent event = new KeyEvent() {
 //Default Code
 }

 frame.add(mr);
 frame.addKeyListener(event);

 }
 }
 `
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top