Hans Ramos
Hans Ramos

Reputation: 23

Java - Method from another class not being called even if it was called from an instance of that class

I'm really lost here. I'm having two classes that are involved with this issue. The first class is the Application class and the second one is the GameManager class. The GameManager class has a method called instantiate() on it that I want to call from the Application class.

So I created an instance of the GameManager class inside the Application class. I called the instantiate() method from THAT VERY INSTANCE, but nothing happens.

It was supposed to output "Instantiating GameObject" to the console, but it doesn't happen. Not even once. What's even more confusing is the renderGame() method in the GameManager class DOES get called(and for testing purposes I had it output the amount of gameobjects that the GameManager holds).

Here are the two classes:

Application class:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;

public class Application implements Runnable {
    public static boolean isRunning;

    GameManager manager = new GameManager();//CREATE AN INSTANCE OF THE GAME MANAGER

    JFrame frame;
    BufferStrategy buffer;
    Graphics drawer;

    Thread appThread;

    public Application (JFrame frame, BufferStrategy buffer, Graphics drawer) {
        this.frame = frame;
        this.frame.setBackground(Color.WHITE);
        this.buffer = buffer;
        this.drawer = drawer;
    }

    public synchronized void startLoop () {
        appThread = new Thread (this);
        appThread.start();
        isRunning = true;
        GameLoop ();
    }

    public synchronized void endLoop () {
        isRunning = false;
        try {
            appThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    void GameLoop() {
        int targetFps = 60;
        double timePerTick = 1000000000 / targetFps;
        double delta = 0;
        long now;
        long lastTime = System.nanoTime();

        while(isRunning) {//THIS IS A LOOP THAT REPEATEDLY CALLS UPDATE AND RENDER METHODS
            now = System.nanoTime();
            delta += (now - lastTime) / timePerTick;
            lastTime = now;
            if(delta >= 1) {
                masterUpdate();
                masterRender();
                delta--;
            }
        }

        System.out.println("STOPPED");
    }

    void masterUpdate () {
        manager.updateGame();
    }

    void masterRender () {
        manager.renderGame(drawer);//THIS IS WHERE THE RENDER METHOD IS CALLED
        buffer.show();
        drawer.dispose();
    }

    @Override
    public void run() {
        startLoop();
        manager.instantiate();
    }
}

The GameManager class:

import java.awt.Graphics;

public class GameManager {

    public static int currentGameObjectAmount = 0;
    public static int maxGameObjects = 100;
    Player[] sceneObjects = new Player[maxGameObjects];

    public void instantiate () {
        System.out.println("Instantiating GameObject");
        sceneObjects[currentGameObjectAmount] = new Player(100, 100, 100);
        currentGameObjectAmount++;
    }

    public void updateGame () {

    }

    public void renderGame (Graphics drawer) {
        System.out.println(currentGameObjectAmount);
        for(int i = 0; i != maxGameObjects; i++) {
            sceneObjects[i].render(drawer);
        }
    }
}

The console outputs 0 when it should be outputting 1 since I called the instantiate() method which increments the integer variable that the console is pushing out. What happens when I run it

PS: Sorry if my code makes your head hurt, I'm not really an advanced programmer.

Upvotes: 2

Views: 614

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347214

The basic problem is here...

@Override
public void run() {
    startLoop();
    manager.instantiate();
}

When you call startLoop, this blocks the current Thread until isRunning is false

I would suggest moving manager.instantiate() above startLoop(). I might also consider having a start and end method some kind instead which can be used to tell the manager that the Thread has started or stopped, but that's just me ;)

Upvotes: 1

Related Questions