这是一个脚本,它根据预定义的名称列表请求并验证用户名。
我从教程中复制了整个脚本,但结果什么都没有,我什至无法理解为什么没有错误!我通过首先复制脚本然后理解它来学习,但不幸的是结果是没有错误!我是编程新手,所以请尝试解释一下
第 1 帧中的脚本:
var myGreeter:Greeter = new Greeter();
mainText.text = myGreeter.sayHello("")
一个名为 greeter 的动作脚本文件中的脚本:
package
{
public class Greeter
{
/**
* Defines the names that should receive a proper greeting.
*/
public static var validNames:Array = ["Sammy", "Frank", "Dean"];
/**
* Builds a greeting string using the given name.
*/
public function sayHello(userName:String = ""):String
{
var greeting:String;
if (userName == "")
{
greeting = "Hello. Please type your user name, and then press the Enter key.";
}
else if (validName(userName))
{
greeting = "Hello, " + userName + ".";
}
else
{
greeting = "Sorry, " + userName + ", you are not on the list.";
}
return greeting;
}
/**
* Checks whether a name is in the validNames list.
*/
public static function validName(inputName:String = ""):Boolean
{
if (validNames.indexOf(inputName) > -1)
{
return true;
}
else
{
return false;
}
}
}
}