[size=0.76em]启动游戏SnailBait 的全局对象[size=0.76em][size=0.76em]如 清单 1 和 清单 3 所示,Snail Bait 只有两个全局对象:SnailBait 函数和 snailBait 对象。
[size=0.76em]清单 3 显示了启动游戏的 JavaScript。该清单的开头定义了三个SnailBait 方法的实现:animate()、start() 和initializeImages()。
清单 3. 启动
SnailBait.prototype = { ... // The 'this' variable in the animate() method is // the window object, so the method uses snailBait instead animate: function (now) { snailBait.fps = snailBait.calculateFps(now); snailBait.draw(now);requestNextAnimationFrame(snailBait.animate); }, start: function () {this.turnRight();
// Sets everything in motionthis.splashToast('Good Luck!', 2000); // "Good Luck" is displayed for 2 secondsrequestNextAnimationFrame(this.animate); }, initializeImages: function () {this.background.src = 'images/background_level_one_dark_red.png';this.runnerImage.src = 'images/runner.png'; this.background.onload = function (e) { // ...the 'this' variable is the window object, // so this function uses snailBait instead.
snailBait.start();}; },}; // End of SnailBait.prototype// Launch gamevar snailBait = new SnailBait(); // Note: By convention, the object
// reference starts with lowercase, but
// the function name starts with uppercasesnailBait.initializeImages();
|