Tarek Al Zihad Tarek Al Zihad - 2 months ago 6
Java Question

assets.init() - Can somebody explain how this works?

I was following a 2d Java game programming from youtube. There is a line where a method was called in another method like objects.

public class Assets {

private static final int width = 32, height = 32;

public static BufferedImage player, dirt, grass, stone, tree;

public static void init(){
SpriteSheet sheet = new SpriteSheet(ImageLoader.loadImage("/textures/sheet.png"));

player = sheet.crop(0, 0, width, height);
dirt = sheet.crop(width, 0, 32, 32);
grass = sheet.crop(width*2, 0, width, height);
stone = sheet.crop(0, height, width, height);


}

}

private void init(){

display = new Display(title, width, height);
Assets.init();

gameState = new GameState();
State.setState(gameState);

}


Can Someone explain how "assets.init()" works? Thanks.

Answer

If you have a static method you can call it without creating an instance of the class. Assets has static init() and you can call it anywhere in this way: Assets.init(); Another init() method in your code does not belong to any class so you will need to create one which will include it.

Comments