一、条件
实例:
事件:玩家输入任意数字 动作:如果大于100,则显示“V我50!”,如果不足100,则显示“金钱不足!”。
(一)if语句
JASS转TS语法表达如下:
export default class Gold {
constructor() {
let t = CreateTrigger();
TriggerRegisterPlayerChatEvent(t, Player(0), "", true);
TriggerAddAction(t, Gold.action);
}
static action(this: void) {
if ((S2I(GetEventPlayerChatString()) = 100))
DisplayTextToPlayer(Player(0), 0, 0, "V我50疯狂星期四!");
if ((S2I(GetEventPlayerChatString()) < 100))
DisplayTextToPlayer(Player(0), 0, 0, "金钱不足!");
}
}
太阳编辑器框架语法表达:
import { MapPlayer } from "solar/w3ts/handles/player"
import { Trigger } from "solar/w3ts/handles/trigger"
export default class Gold {
constructor() {
let t = new Trigger()
t.registerPlayerChatEvent(MapPlayer.fromIndex(0), '', false)
t.addAction(this.action)
}
action(this: void) {
if (S2I(GetEventPlayerChatString()) = 100)
DisplayTextToPlayer(Player(0), 0, 0, 'V我50疯狂星期四!')
if (S2I(GetEventPlayerChatString()) < 100)
DisplayTextToPlayer(Player(0), 0, 0, '金钱不足!')
}
}
[info]知识点:使用太阳编辑器的registerPlayerChatEvent的第一个参数是MapPlayer类型,而这个类型需要从"solar/w3ts/handles/player"中导入。我们Ctrl+鼠标左键这个相对地址,查看player.ts文件会发现MapPlayer是Handle<player的子类。进一步查找会发现类似JASS转TS的Player(0)对应的是fromIndex。由于我们刚开始用太阳编辑器,代码还不太熟悉,相信将来能熟能生巧![/info]
(二)if……else语句
import { MapPlayer } from "solar/w3ts/handles/player"
import { Trigger } from "solar/w3ts/handles/trigger"
export default class Gold {
constructor() {
let t = new Trigger()
t.registerPlayerChatEvent(MapPlayer.fromIndex(0), '', false)
t.addAction(this.action)
}
action(this: void) {
if (S2I(GetEventPlayerChatString()) = 100)
DisplayTextToPlayer(Player(0), 0, 0, 'V我50疯狂星期四!')
else
DisplayTextToPlayer(Player(0), 0, 0, '金钱不足!')
}
}
二、循环
实例:
事件:玩家输入数字xh 动作:循环12次创建1个步兵单位。
(一)for循环
老规矩,我们先来看看JASS转TS语法表达如下:
export default class Gold {
constructor() {
let t = CreateTrigger();
TriggerRegisterPlayerChatEvent(t, Player(0), "xh", true);
TriggerAddAction(t, Gold.action);
}
static action(this: void) {
bj_forLoopAIndex = 1;
bj_forLoopAIndexEnd = 12;
while (true) {
if (bj_forLoopAIndex bj_forLoopAIndexEnd) break;
CreateNUnitsAtLoc(1, FourCC('hfoo'), Player(0), GetRectCenter(GetPlayableMapRect()), bj_UNIT_FACING);
bj_forLoopAIndex = bj_forLoopAIndex + 1;
}
}
}
太阳编辑器框架语法表达:
import { MapPlayer } from "solar/w3ts/handles/player"
import { Trigger } from "solar/w3ts/handles/trigger"
export default class Gold {
constructor() {
let t = new Trigger()
t.registerPlayerChatEvent(MapPlayer.fromIndex(0), 'xh', false)
t.addAction(this.action)
}
action(this: void) {
for (let i = 1; i < 13; i++) {
CreateNUnitsAtLoc(1, FourCC('hfoo'), Player(0), GetRectCenter(GetPlayableMapRect()), 270);
}
}
}
[info]知识点:从上面我们可以再一次感受到TypeScript 语言的简洁,2行代码实现了JASS语言6行代码的效果。温习TypeScript 循环语法可以访问菜鸟教程:TypeScript 循环 | 菜鸟教程 (runoob.com)[/info]
(二)while循环
while循环和for循环最大的不同就是条件语句的写法。当条件为 false 时,会跳过循环主体,直接执行紧接着 while 循环的下一条语句。
import { MapPlayer } from "solar/w3ts/handles/player"
import { Trigger } from "solar/w3ts/handles/trigger"
export default class Gold {
constructor() {
let t = new Trigger()
t.registerPlayerChatEvent(MapPlayer.fromIndex(0), 'xh', false)
t.addAction(this.action)
}
action(this: void) {
let i = 1;
while (i < 13) {
CreateNUnitsAtLoc(1, FourCC('hfoo'), Player(0), GetRectCenter(GetPlayableMapRect()), 270);
i++;
}
}
}
三、演示地图下载
(已过期)