java 如何使用gdx BitmapFont更改字体大小

pn9klfpd  于 2023-10-14  发布在  Java
关注(0)|答案(1)|浏览(109)

我用Java创建了一个字体BitmapFont
private BitmapFont font = new BitmapFont(Gdx.files.internal("HARLOWSI.TTF"));
现在我想改变字体的大小
我尝试了出现在BitmapFont方法中的方法font.setScale(scale);
它不起作用,所以我在网上寻找解决方案,我被告知尝试font.getData().setScale(scale);
但这也不起作用。
我也试过这个方法,但是在parameter.size行上出现了一个错误。标记大小错误,此标记后应为VariableDeclaratorID

package com.mygdx.game;

import java.lang.reflect.Parameter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;

public class Points {
    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("HARLOWSI.TTF"));
    FreeTypeFontParameter parameter = new FreeTypeFontParameter();
    parameter.size = 12; //here's where I get an error
    private String name;
    private int quantity;
    private Texture icon;
    private BitmapFont font = new BitmapFont();
    
    public Points(String name, int quantity, Texture icon) {
        super();
        this.name = name;
        this.quantity = quantity;
        this.icon = icon;
        this.font = font;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public Texture getIcon() {
        return icon;
    }
    public void setIcon(Texture icon) {
        this.icon = icon;
    }
    
    public void add(int added) {
        this.setQuantity(this.quantity+added);
    }
    public void substract(int substracted) {
        this.add(-substracted);
    }
    
    public void show(SpriteBatch batch) {
        batch.draw(this.icon, 10, 890-this.icon.getHeight());
        this.font.draw(batch, "Points : " + String.valueOf(this.quantity) , 20+this.icon.getWidth(),890-this.icon.getHeight());
    }
    
    public void show(SpriteBatch batch,int x, int y) {
        batch.draw(this.icon, x, y);
        this.font.draw(batch, "Points : " + String.valueOf(this.quantity) , 20+this.icon.getWidth(),890-this.icon.getHeight());
    }
    
}
cqoc49vn

cqoc49vn1#

一种方法是使用FreeTypeFontGeneratordocumentation,它允许您生成特定大小的BitmapFont
这样,您就可以生成BitmapFont的多个示例,每个示例对应您在游戏中需要的大小。
在你的情况下,问题来自你这样做

parameter.size = 12;

在构造函数(或方法)之外,除了在那里声明变量之外做任何事情都是语法错误。
将该行移到构造函数中;

public Points(String name, int quantity, Texture icon) {
    super();
    this.name = name;
    this.quantity = quantity;
    this.icon = icon;
    this.font = font;
    
    parameter.size = 12;
}

相关问题