我一直在研究一個processing.js演示,其中主體圍繞一箇中心點運行。我正在嘗試添加一個偵聽器,用於檢查每個更新是否按下s
鍵,如果是,則調用一個方法以指定的因子減小每個物體軌道的大小。如何在Processing.js中的update()中監聽鍵盤輸入
該示例是jsfiddle上的here。
基本上我已經把代碼中的update()方法來監聽按鍵:
void update()
{
//detect input
if(keyPressed)
{
debugger; //debug
if(key == 's' || key == 'S')
{
//shrink orbit
ShrinkOrbit(planets,.9);
}
}
}
它調用ShrinkOrbit(ArrayList <OrbitingBody> orbs, float reductionFactor)
方法:
/*
* Reduces size of orbit for specified group of orbiting bodies
*/
void ShrinkOrbit(ArrayList<OrbitalBody> orbs, float reductionFactor)
{
for(OrbitalBody b:orbs)
{
b.x *= reductionFactor;
b.y *= reductionFactor
}
}
然而,目前不僅不調試斷點不觸發,但我發現在Chrome開發者工具中出現了一些錯誤:
Uncaught SyntaxError: Unexpected reserved word
Uncaught TypeError: Cannot read property 'reason' of null Actions.js:333
(anonymous function) Actions.js:333
(anonymous function) moo-clientcide-1.3.js:212
Array.implement.each moo-clientcide-1.3.js:329
(anonymous function) moo-clientcide-1.3.js:212
Class.JSLintValidate Actions.js:330
wrapper.extend.$owner moo-clientcide-1.3.js:3798
Class.jsLint Actions.js:277
wrapper.extend.$owner moo-clientcide-1.3.js:3798
(anonymous function) moo-clientcide-1.3.js:1027
defn
我錯誤地認爲update()
是一種全局方法,相當於setup
,可以在任何草圖中調用該方法嗎?我曾看到它在另一個正常運行的jsFiddle中使用,這是我從中得出結論的地方。我的目標是Processing.js 1.4.1,如果有任何幫助。