查看: 1444|回复: 0

冒险岛脚本编写基础教程八

  • TA的每日心情
    开心
    2023-4-29 03:45
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    初级会员

    枫币
    391
    威望
    0
    贡献
    15
    在线时间
    35 小时
    注册时间
    2019-3-9
    QQ
    发表于 2022-9-5 22:35:05 | 显示全部楼层 |阅读模式
    第三课: 学习使用变量
    在你学习编写时,你一定有发现一些以 var 起头的东西出现在脚本开头,那就
    是变量。简单来说,变量是用来「取代」东西的。你可以用你命名的变量来
    「取代」数字或文字等。一般来说,你会想要用变量来缩短脚本,即使只是几
    个字而已。范例:
    var gl = 4000313;
    function start() {
    cm.sendOk("哈啰,你带来 #v" + gl + "#了吗?"); // Calls the variable
    gl, and the information it is replacing
    cm.dispose();
    }
    As you can see from the example, the item id 4000313 is being replaced with the variable "gl". Even though it's only a minor difference, I shortend the script by 1 character. Now imagine having hundreds of places on a script where 4000313 was replaced by "gl". It adds up on how much space you save by using variables. That was an example on how to use a variable for a number. Here is one on how to use a variable for a string, or text sentence.
    var yes = "太好了,你带齐了黄金枫叶!";
    var no = "抱歉,你身上的黄金枫叶不够。";
    var status;
    function start() {
    status = -1;
    action(1, 0, 0);
    }
    function action(mode, type, selection) {
    if (mode == 1) {
    status++;
    }else{
    status--;
    }
    if (status == 0) {
    cm.sendSimple("#L0# 我有 10 个黄金枫叶 #l \r\n #L1# 我有 20 个黄金
    枫叶 #l");
    }else if (status == 1) {
    if (selection == 0) {
    if (cm.haveItem(4000313, 10)) {
    cm.sendOk(yes); // 使用变量「yes」,并显示你设定给他的内容
    cm.dispose();
    } else {
    cm.sendOk(no); // 使用变量「no」,并显示你设定给他的内容
    cm.dispose();
    }
    } else if (selection == 1) {
    if (cm.haveItem(4000313, 20)) {
    cm.sendOk(yes); // 使用变量「yes」,并显示你设定给他的内容
    cm.dispose();
    } else {
    cm.sendOk(no); // 使用变量「no」,并显示你设定给他的内容
    cm.dispose();
    }
    }
    }
    }

    如你清楚地看见,使用这些变量省下了很大的空间。我只用了 111 个字就打 完了脚本,还包含开头的变量,而不用打长长的 202 个字。未来如果要编辑 脚本,有使用变量的话就更加简单了。

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    枫叶物语即使倒闭也绝对不会变质
    Copyright © 2001-2013 Discuz Team. Powered by Discuz! X3.5 |网站地图 | 本站已运行: