Minecraft Fabric开发ClickGUI报错:cannot find symbol 求助
Minecraft Fabric模组自定义ClickGUI编译错误解决
问题背景
开发Minecraft Fabric模组的自定义ClickGUI时,参照Fabric渲染指南编写代码后出现编译错误,目标是渲染出带颜色渐变与纹理的四边形效果。
原代码
package org.sgx.nihao.ui.screen.clickgui; import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.render.*; import net.minecraft.util.Identifier; import org.joml.Matrix4f; public class ClickGUI { public void onInitializeClient(){ HudRenderCallback.EVENT.register((drawContext, tickDelta) -> { Matrix4f positionMatrix = drawContext.getMatrices().peek().getPositionMatrix(); Tessellator tessellator = Tessellator.getInstance(); BufferBuilder buffer = tessellator.getBuffer(); buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE); buffer.vertex(positionMatrix, 20, 20, 0).color(1f, 1f, 1f, 1f).texture(0f, 0f).next(); buffer.vertex(positionMatrix, 20, 60, 0).color(1f, 0f, 0f, 1f).texture(0f, 1f).next(); buffer.vertex(positionMatrix, 60, 60, 0).color(0f, 1f, 0f, 1f).texture(1f, 1f).next(); buffer.vertex(positionMatrix, 60, 20, 0).color(0f, 0f, 1f, 1f).texture(1f, 0f).next(); RenderSystem.setShader(GameRenderer::getPositionColorTexProgram); RenderSystem.setShaderTexture(0, new Identifier("nihao_mod", "icon.png")); RenderSystem.setShaderColor(1f, 1f, 1f, 1f); tessellator.draw(); }); } }
编译错误信息
*path*\ClickGUI.java:12: error: cannot find symbol Matrix4f positionMatrix = drawContext.getMatrices().peek().getPositionMatrix(); ^ symbol: method getMatrices() location: variable drawContext of type MatrixStack
解决方案
错误原因是HudRenderCallback的第一个参数类型为MatrixStack,而非包含getMatrices()方法的DrawContext,直接调用peek()即可获取矩阵。同时需要调整渲染API的调用顺序,确保Shader和纹理在构建顶点缓冲区前设置:
修正后的代码
package org.sgx.nihao.ui.screen.clickgui; import com.mojang.blaze3d.systems.RenderSystem; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.render.*; import net.minecraft.util.Identifier; import org.joml.Matrix4f; public class ClickGUI { public void onInitializeClient(){ HudRenderCallback.EVENT.register((matrixStack, tickDelta) -> { // 直接从MatrixStack获取位置矩阵 Matrix4f positionMatrix = matrixStack.peek().getPositionMatrix(); // 先设置Shader与纹理 RenderSystem.setShader(GameRenderer::getPositionColorTexProgram); RenderSystem.setShaderTexture(0, new Identifier("nihao_mod", "icon.png")); RenderSystem.setShaderColor(1f, 1f, 1f, 1f); Tessellator tessellator = Tessellator.getInstance(); BufferBuilder buffer = tessellator.getBuffer(); buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR_TEXTURE); // 添加四边形顶点(位置、颜色、纹理坐标) buffer.vertex(positionMatrix, 20, 20, 0).color(1f, 1f, 1f, 1f).texture(0f, 0f).next(); buffer.vertex(positionMatrix, 20, 60, 0).color(1f, 0f, 0f, 1f).texture(0f, 1f).next(); buffer.vertex(positionMatrix, 60, 60, 0).color(0f, 1f, 0f, 1f).texture(1f, 1f).next(); buffer.vertex(positionMatrix, 60, 20, 0).color(0f, 0f, 1f, 1f).texture(1f, 0f).next(); tessellator.draw(); }); } }
关键修改点
- 移除
getMatrices()调用:直接通过matrixStack.peek().getPositionMatrix()获取位置矩阵 - 调整渲染顺序:先设置Shader、纹理和颜色,再开始构建顶点缓冲区,确保渲染状态正确
- 参数重命名:将原
drawContext改为matrixStack,更贴合实际类型,提升代码可读性
修改后编译即可通过,运行时会在屏幕(20,20)位置渲染一个40x40的四边形,四个顶点分别为白、红、绿、蓝颜色渐变,并叠加指定纹理。
内容的提问来源于stack exchange,提问作者Yooshyasha
相关产品推荐
相关产品推荐

