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

跟随格斗游戏教程时遇sprite_index未设置错误求助

解决GameMaker中Var <unknown_object>.sprite_index not set.错误

错误场景

你遇到的错误提示Var <unknown_object>.sprite_index not set.,出现在这段代码中:

if (sprite_index != sprKill_foward)
{
    sprite_index = sprKill_foward;
    image_index = 0;
}

对象的状态管理代码如下:

switch (state)
{
    case 0:  //< idle
        UpdateMoveIdle();
        break;
    case 1: //< move fwd
        UpdateMoveFwd();
        break;
    case 2: //< move back
        UpdateMoveBack();
        break;
    default:
        show_debug_message("Error! Unknown state: " + string(state) + " Maybe you added a state but haven't updated the Update method.");
        break;
}

if (nextState != state)
{
    //change the state
    state = nextState;
            
    //execute the start function for the new state
    switch (state)
{
        case 0:  //< idle
            StartIdle();
            break;
        case 1: //< move fwd
            StartMoveFwd();
            break;
        case 2: //< move back
            StartMoveBack();
            break;
        default:
            show_debug_message("Error! Unknown state: " + string(state) + " Maybe you added a state but haven't updated the Start method.");
            break;
    }
}

排查与解决步骤

1. 检查对象的初始精灵设置

GameMaker中,若对象未在编辑器中指定默认精灵,且未在Create事件中初始化sprite_index,第一次访问该变量时就会触发此错误。

  • 解决:
    • 打开对象编辑器,在Sprite属性下拉菜单中选择一个默认精灵(比如 idle 状态的精灵);
    • 或者在对象的Create事件中添加初始化代码:sprite_index = sprIdle;(替换为你项目中实际的 idle 精灵名称)。

2. 验证sprKill_foward资源的正确性

错误也可能是因为目标精灵资源不存在或拼写错误:

  • 打开资源管理器,搜索sprKill_foward,确认该精灵确实存在;
  • 检查代码中的拼写是否和资源名完全一致(注意是否把forward写成了foward,这类拼写错误很常见)。

3. 确认状态切换时的精灵初始化逻辑

从你的状态代码来看,当前只处理了0(idle)、1(move fwd)、2(move back)三种状态。如果sprKill_foward对应一个新的Kill状态:

  • 你需要在状态管理的switch分支中新增Kill状态的case(比如case 3);
  • 同时在Start函数的switch中添加StartKill()分支,在StartKill()里初始化sprite_index = sprKill_foward;,避免在未初始化的情况下直接判断sprite_index。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 19:50:26