What is Tick() for?

I’ve been going through this (https://github.com/matheus23/TinyWorld/blob/master/src/org/matheusdev/screens/Screen.java) game made by mathus23 and have been figuring out what certain things do and how they work as to help me in creating a game. So-far I’ve figured everything that I’ve needed to out but there is one thing that I can’t and that is the tick() method.

After going through a few of the classes that extend the Screen class and looking at what’s happening inside of the tick() method I came to the conclusion that every time the game loop does a loop it is telling all of the classes that use the tick() method to run their tick() methods. After trying this in my own program as to make sure this was what it was really doing it ended up doing nothing. What does the tick() method do?

The game loop on github is (TinyWorld / src / org / matheusdev / GameCanvas.java) and pretty much everything in (TinyWorld / src / org / matheusdev / screens /) uses the tick() method.

Without looking at the code, I would guess that the tick() method is called every frame (so 60 times a second, or whatever the fps is). This allows game Objects to update themselves before being rendered.

As for your own program, you’re going to have to post some code for us to comment on if you want a more specific answer.

I’m using the exact same Screen.java code as is on github and my game loop is the same as well, here is where I’m trying to test the tick() method.

package World;

import Functions.KeyboardListener_Old;
import Interface.Screen;
import Player.Player;
import Sprite.Sprite;
import java.awt.Graphics2D;

public class WorldCore extends Screen
{
    private int currentArea;
    private int playerXPosition = 0, playerYPosition = 0;
    private Player player;
    private Sprite sprite;
    private KeyboardListener_Old keyboardListener;
    
    public WorldCore()
    {
       player = new Player();
       sprite = new Sprite();
       keyboardListener = new KeyboardListener_Old();
    }
    
    public void distributeInformation()
    {
        player.setCurrentArea(currentArea);
        sprite.setCurrentArea(currentArea);
    }
    
    //Get methods.
    public int getCurrentArea()
    {
        return currentArea;
    }
    
    //Set methods
    public void setCurrentArea(int x)
    {
        currentArea = x;
    }

    @Override
    public void tick() 
    {
        playerXPosition = keyboardListener.getXPosition();
        playerYPosition = keyboardListener.getYPosition();
        System.out.println(playerXPosition+"    "+playerYPosition);
    }

    @Override
    public void render(Graphics2D g)
    {
    }
}

The reason I have a #tick() and a #render(…) method is ‘speration of game logic and rendering’ (<- not really… ).

You’re using #tick() completely right. In tick() all the stuff in the game world is recursively updated, using the #tick() method. Then, in the next step, recursively everything is drawn using the #render() method.

What you’re missing is the code inside #render(). You have to draw the player at that part.

So you'd do something like this:

    @Override
    public void render(Graphics2D g) {
        g.drawRect(playerXPosition-5, playerYPosition-5, 10, 10);
    }

The render() method here is just so I can test the tick() method, my actual rendering method is in another class. For some reason, when I run the program, nothing is being printed by the System.out.println(playerXPosition+" "+playerYPosition); that’s what led me to believe that tick() wasn’t working.

This can’t be. Are you sure, you have set the Screen using GameCanvas.setScreen(…) ?

Just did a ctrl+f search on my classes. It doesn’t look like I’ve used that anywhere; where should it have been?

Edit: I just noticed it being used in your code on github, going through it now.

Edit 2: Managed to get it kinda-sorta working but the screen is having a seizure (Tons of flickering.) The problem is probably with how I wrote the code originally, I’ll just keep doing my re-write and see if it gets fixed along the way.

Edit 3: Looked at a few more classes on github, it looks like only one class should be set to screen instead of two classes switching back and forth very rapidly. Looks like the re-write will solve the flickering problem.