生成期望)预期{和EOF找到} Unity Javascript

时间:2017-01-30 13:50:02

标签: javascript jquery

第12行和第22行的语法错误似乎没有任何丢失/额外的括号,所以它让我感到困惑的是我的更新函数中导致这些错误的原因。理想情况下,我希望程序能够在重新加载8秒之前启动四次。



#pragma strict

 var Bullet : Transform;
 var Spawn : Transform;
 var amountOfShots = 4;
 
 function Update() {
 	if (Input.GetKeyDown("r") {
        Reload(); //Calls the reload function
    }

    if (Input.GetButtonDown("Fire1")) {
        //Fires on left click
        Shoot();
        amountOfShots --;
        //Calls shoot function and removes one shot from amountOfShots
    }
}
 
function Shoot() {
    var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
    pel.rigidbody.AddForce(transform.forward * 6000);
    //instantiates bullet at spawn point and adds a force to propel the bullet forward
     
    if (amountOfShots <= 0) {
        Debug.Log("No ammo left in magazine");
        //Tells player if there is not enough ammo to fire
    }     
}

function OnReload()
   "setTimeout(8000, 3000)"
//Waits 8 seconds while reloading

function Reload()
{
    amountOfShots = 4;
    //resets amount of shots to 4
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

第一个if语句中缺少右括号:

if(Input.GetKeyDown("r")

应该是

if(Input.GetKeyDown("r"))

答案 1 :(得分:1)

你忘记了第12行的结束括号,因此错误了。

if(Input.GetKeyDown("r"))

尝试使用通知用户语法错误的IDE。 Webstorm和Brackets是很好的。

由于

答案 2 :(得分:0)

函数不会遵循与ifelseforwhile内部有多少指令相同的步骤。

一个函数必须总是在它们的主体周围有大括号({})(即使它们包含一条指令)。所以这个:

function OnReload()
   "setTimeout(8000, 3000)"

应该是这样的:

function OnReload() {
   "setTimeout(8000, 3000)"; // a ; is optional here (in this case) but it's not a good habbit to ignore them.
}