Java - 来自另一个类的方法,即使从该类的实例调用它也没有被调用

时间:2018-03-17 05:18:33

标签: java

我真的迷失在这里。我有两个涉及这个问题的课程。第一个类是Application类,第二个类是GameManager类。 GameManager类上有一个名为instantiate()的方法,我想从Application类调用它。

所以我在GameManager类中创建了Application类的实例。我从非常实际调用instantiate()方法,但没有任何反应。

它应该输出"Instantiating GameObject"到控制台,但它不会发生。从来没有。更令人困惑的是renderGame()类中的GameManager方法 DOES 被调用(出于测试目的,我输出了gameobjects GameManager的数量{ {1}}持有)。

以下是两个类:

申请类:

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();
    }
}

GameManager类:

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);
        }
    }
}

控制台在输出0时会输出1,因为我调用了instantiate()方法,该方法会增加控制台推出的整数变量。 What happens when I run it

PS:对不起,如果我的代码让你头疼,我真的不是一个高级程序员。

1 个答案:

答案 0 :(得分:1)

基本问题在这里......

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

当您致电startLoop时,会阻止当前Thread,直到isRunningfalse

我建议将manager.instantiate()移到startLoop()之上。我也可以考虑使用某种startend方法代替可以告诉经理Thread已启动或停止,但这只是我;)