Skip to content

支持在场景(Scene)配置文件中直接引用 / 内嵌 JavaScript 文件 #953

@Yunease

Description

@Yunease

目前场景文件只能使用json形式的动画,无法直接关联或执行 JavaScript 逻辑。

希望引擎支持:在场景文件内部引用外部 .js 脚本 / 内嵌脚本逻辑,让场景可以直接绑定行为、初始化逻辑、生命周期函数等。支持原生js的循环和判断,不需要写冗长的=when或goto

使用案例:
为玩家设置好感度,提前在js中设置屏幕右上角的好感度贴图:

function onSceneLoad() {
    // 提前创建好感度UI面板(固定在屏幕右上角)
    let uiContainer = createUIContainer();
    uiContainer.setPosition("top-right"); // 右上角对齐
    uiContainer.setSize(180, 50);

    // 2. 加载好感度图标贴图(心形/图标素材)
    let loveIcon = createSprite("images/ui/heart.png");
    loveIcon.setPosition(10, 10);
    uiContainer.addChild(loveIcon);

    // 3. 创建好感度数字文本
    let loveText = createText("好感度: 0");
    loveText.setPosition(50, 15);
    loveText.setFontSize(16);
    uiContainer.addChild(loveText);

    // 4. 把UI存入场景,方便后续更新
    scene.ui.loveText = loveText;
}

// 5. 外部修改好感度时自动调用(剧情选项/事件触发)
function updateLoveValue(newValue) {
    gameData.love = newValue;
    scene.ui.loveText.setText("好感度: " + newValue);

    // 根据数值切换心形贴图(满/空/半)
    if (newValue >= 100) {
        scene.ui.loveIcon.setTexture("images/ui/heart_full.png");
    } else if (newValue >= 50) {
        scene.ui.loveIcon.setTexture("images/ui/heart_half.png");
    }
}

配置区分:全局脚本 和 场景脚本
为适配 AVG 游戏开发,建议做两层脚本作用域隔离:

全局脚本:在 config.json 中声明,游戏全程加载、全局生效,用于实现通用逻辑(如全局好感度 UI、 多结局特殊效果、 根据讲话角色切换高光等 )。
场景脚本:在单个场景文件中声明,仅当前场景加载、场景销毁时自动卸载,用于实现该场景专属剧情、分支、临时逻辑。

importScript: ./scripts/scene/date_logic.js -id=xxx ;仅该约会场景使用
changebg: xxx.png;
vilstia: 台词;
useScript: -id=xxx ;在此处使用

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions