读书人

Torque2D MIT 学习札记(5) - 脚本语法

发布时间: 2013-03-04 17:22:12 作者: rapoo

Torque2D MIT 学习笔记(5) ---- 脚本语法(3)
状态控制

和其他编程语言一样,TorqueScript支持分支结构.

if, then, elseif(<boolean expression>) { pass logic}else { alternative logic} // 控制灯光的全局变量$lightsShouldBeOn = true;// 检查灯是否应该打开if($lightsShouldBeOn){ // 开灯 turnOnLights(); echo("Lights have been turned on");}else{ // 关灯 turnOffLights(); echo("Lights have been turned off");}// 控制灯光的全局变量$lightsShouldBeOn = true;// 检查灯是否应该打开if($lightsShouldBeOn) turnOnLights(); //开灯else turnOffLights(); // 关灯switch and switch$

If your code is using several cascading if-then-else statements based on a single value, you might want to use a switch statement instead. Switch statements are easier to manage and read. There are two types of switch statements, based on data type: numeric (switch) and string (switch$).

TorqueScript支持两种多分支结构

1: switch: 数字类型

switch(<numeric expression>) {   case value0:       statements;   case value1:       statements;   case value3:       statements;   default:       statements;}

PS: 没有break;

switch($ammoCount){   case 0:      echo("Out of ammo, time to reload");      reloadWeapon();   case 1:      echo("Almost out of ammo, warn user");      lowAmmoWarning();   case 100:      echo("Full ammo count");      playFullAmmoSound();   default:      doNothing();}

2: switch$: 字符串类型

switch$ (<string expression>) {   case "string value 0":       statements;   case "string value 1":       statements;...   case "string value N":       statements;   default:       statements;}

// Print out specialtiesswitch($userName){   case "Heather":      echo("Sniper");   case "Nikki":      echo("Demolition");   case "Mich":      echo("Meat shield");   default:      echo("Unknown user");}

循环

和C++一样,Torquescript支持两种循环,分别如下:

For循环for(expression0; expression1; expression2) { statement(s);}for(%count = 0; %count < 3; %count++) { echo(%count);}OUTPUT:012While循环

语法:

while(expression) {    statements;}

%countLimit = 0;while(%countLimit <= 5){ echo("Still in loop"); %count++;}echo("Loop was terminated");函数

Much of your TorqueScript experience will come down to calling existing functions and writing your own. Functions are a blocks of code that only execute when you call them by name. Basic functions in TorqueScript are defined as follows:

归根结底,Torquescript的代码大多都是要调用存在的引擎函数方法和自定义方法.函数方法是一个在需要时主动调用函数名才执行的代码块.

基本的脚本函数定义方式如下:

// function - 脚本函数的关键字// function_name - 函数名// arg0-N... - 参数表(任意个)// statements - 函数逻辑// return val - 返回值function function_name([arg0],...,[argn]) {    statements;    [return val;]}

例子:

function echoRepeat (%echoString, %repeatCount) {   for (%count = 0; %count < %repeatCount; %count++)   {      echo(%echoString);   }}
echoRepeat("hello!", 5);OUTPUT:"hello!""hello!""hello!""hello!""hello!"

如果定义了一个函数名已经存在的方法,Torquescript将覆盖老的函数.即使只是参数数量不同,老的方法仍然会给覆盖.

比如:

function OutputA( %text ){   echo( %text );}function OutputA( %text, %addon ){   echo( %text SPC "+" SPC %addon );}调用:    OutputA( "A output function" );   OutputA( "A output function", "AddOn Infomation" );输出:A output function + A output function + AddOn Infomation

// In TorqueScript%objectID = new ObjectType(Name) { [existing_field0 = InitialValue0;] ... [existing_fieldN = InitialValueN;] [dynamic_field0 = InitialValue0;] ... [dynamic_fieldN = InitialValueN;]};

语法解释:

%objectID - 保存对象句柄的变量,new是一个关键字,他告诉引擎创建一个ObjectType指定的对象实例.

ObjectType - 引擎或者脚本中的定义的任意一个SimObject的继承类或者子类.SimObject的继承对象统称为游戏世界对象.

Name (可选项) - 对象名字符串

existing_fieldN - 初始化已经存在的类成员变量,当然变量必须是导出到脚本的变量.

dynamic_fieldN - 最后是添加一些新的变量,这些变量有唯一性,只属于这里实例化的对象,被称为动态变量.

Handles vs Names 每一个游戏对象都可以通过两个参数来访问,他们分别是:

Handle - 当对象创建时分配的全局唯一的数字ID编号.

Name - 在对象创建时可选的指定对象名.

例子:

// In this example, Truck is the name of the objectnew SceneObject(Truck) {   position = "0 0";   size = "5 5";};

objHandle.function_name();objName.function_name();

new SceneObject(Truck) { position = "0 0"; size = "5 5";};// 将Truck对象的所有导出方法输出打印到控制台日志.Truck.dump();// 获取对象的数字ID编号$objID = Truck.getID();// 输出IDecho("Object ID: ", $objID);// 获取对象的位置%position = $objID.getPosition();// 输出位置echo("Object Position: ", %position);
上面的代码展示了如何通过对象句柄和对象名来调用对象的方法.另外,Torquescript还支持对象脚本辅助函数的创建.

// function - 函数定义关键字// ClassName::- 类名// function_name - 函数名// ... - 参数表// statements - 逻辑代码// %this- 传递调用对象的句柄参数// return val - 返回值function Classname::func_name(%this, [arg0],...,[argn]) { statements; [return val;]}

// 武士刀入鞘function Samurai::sheatheSword(%this){ echo("Katana sheathed");}

1042.sheatheSword();OUTPUT: "Katana sheathed"

这里需要注意,我们并没有传递任何参数给函数,%this参数是默认的,由引擎内部自动传递,不需要我们进行额外的传递.

读书人网 >移动开发

热点推荐