You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GameMaker 8.1中Unknown variable floor_tex错误的解决方法

问题:GameMaker 8.1运行3D代码时出现Unknown variable floor_tex错误

使用GameMaker 8.1.141(r11549)版本,跟随3D游戏制作教程编写代码后,测试游戏时触发Unknown variable floor_tex错误,错误截图如下:
Unknown variable floor_tex错误截图

当前代码如下:

direction -= (display_mouse_get_x() -(display_get_width()/2))/5
zto -= (display_mouse_get_y() -(display_get_height()/2))/2
display_mouse_set(display_get_width()/2,display_get_height()/2)

xto = lengthdir_x(100,direction)+x
yto = lengthdir_y(100,direction)+y

d3d_set_projection(x,y,z,xto,yto,zto,0,0,1)

draw_set_color(c_white)
d3d_draw_floor(0,0,0,room_width,room_height,0,background_get_texture(floor_tex),room_width/128,room_height/128)
d3d_draw_floor(0,0,100,room_width,room_height,100,background_get_texture(root_tex),room_width/128,room_height/128)

解决方案

错误原因

floor_tex和root_tex并非GameMaker内置变量,代码中直接调用但未定义,导致引擎无法识别这两个名称对应的资源或变量。

修复步骤

  1. 检查背景资源创建
    确认你已在GameMaker的资源面板中创建了名为floor_tex和root_tex的背景资源,注意名称大小写完全一致(GameMaker 8.1对资源名大小写敏感)。

  2. 直接使用背景资源ID
    若未创建对应名称的背景,可直接使用默认背景ID(如background0、background1)替换代码中的floor_tex和root_tex,示例:

    d3d_draw_floor(0,0,0,room_width,room_height,0,background_get_texture(background0),room_width/128,room_height/128)
    d3d_draw_floor(0,0,100,room_width,room_height,100,background_get_texture(background1),room_width/128,room_height/128)
    
  3. 显式声明变量(可选)
    若需要用自定义变量名指代背景资源,需在代码开头先声明并赋值:

    // 替换为你实际使用的背景资源ID
    var floor_tex = background_get_texture(background0);
    var root_tex = background_get_texture(background1);
    
    // 后续代码保持不变
    direction -= (display_mouse_get_x() -(display_get_width()/2))/5
    zto -= (display_mouse_get_y() -(display_get_height()/2))/2
    // ...其余代码
    

修正后完整代码示例

// 声明背景变量(或直接使用背景ID)
var floor_tex = background_get_texture(background0);
var root_tex = background_get_texture(background1);

direction -= (display_mouse_get_x() -(display_get_width()/2))/5
zto -= (display_mouse_get_y() -(display_get_height()/2))/2
display_mouse_set(display_get_width()/2,display_get_height()/2)

xto = lengthdir_x(100,direction)+x
yto = lengthdir_y(100,direction)+y

d3d_set_projection(x,y,z,xto,yto,zto,0,0,1)

draw_set_color(c_white)
d3d_draw_floor(0,0,0,room_width,room_height,0,floor_tex,room_width/128,room_height/128)
d3d_draw_floor(0,0,100,room_width,room_height,100,root_tex,room_width/128,room_height/128)

内容的提问来源于stack exchange,提问作者sagad ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 02:55:17