HTML5 - 启动共享相同变量的游戏对象
HTML5 - Initiate Game Objects that Share Same Variable
我只是想知道启动共享相同变量的相同对象的最佳方法是什么,除了它们的位置。
我基本上是在处理我的HTML5游戏的场景,我已经建立了一个可以打开和关闭的路灯柱。所有灯将具有相同的变量,例如图像,尺寸,开/关功能。唯一不同的是位置 x 和 y。
我已经在一个变量函数中构建了我的灯(我认为它们被称为"var = {"),在我的实际游戏DrawFunction中,我调用了"LampPost.draw();"。
有可能做这样的事情吗?
LampPost(0,0);
LampPost(100, 0);
LampPost(200, 0);
等。。。然后可能将每个启动的 Lamp 放在一个数组中?
这是灯的片段代码:
var LampPost = {
lamp_xsprite : 0,
lamp_ysprite : 0,
light_xsprite : 0,
lightysprite : 0,
x : 440,
y : 320,
//Flicker effects
lightFlicker : 0,
seconds_Off : 0,
seconds_On : 0,
randomLength_Off : 500,
randomLength_On : 150,
draw: function(x, y) {
this.x = x;
this.y = y;
ctxPropsOver.setTransform(1, 0, 0, 1, -Map.x + gameWidth/2, -Map.y + gameHeight/2);
ctxPropsOver.rotate(Math.PI / -25);
ctxPropsOver.clearRect(0, 0, 1000, 1000);
ctxPropsOver.globalAlpha = this.lightFlicker;
ctxPropsOver.drawImage(imgLights, 0, 36, 500, 463, -60 + this.x, -190 + this.y, 500, 463);
ctxPropsOver.globalAlpha = 1;
ctxPropsOver.drawImage(imgLights, 0, 0, 210, 36, 0 + this.x, 0 + this.y, 210, 36);
this.flicker();
}
};
有两种常见的方法可以实现您的目标:工厂函数或带有原型的构造函数。 工厂函数相当容易理解,你基本上把你现在拥有的代码,作为 return 语句扔到函数中。
function LampPost(x, y) {
var lampPost = {
lamp_xsprite: 0,
lamp_ysprite: 0,
light_xsprite: 0,
lightysprite: 0,
x: x, //Reference x argument here
y: y, //Reference y argument here
//Flicker effects
lightFlicker: 0,
seconds_Off: 0,
seconds_On: 0,
randomLength_Off: 500,
randomLength_On: 150,
draw: function(x, y) { /* Cut for readability */ }
};
globalLampPostArray.push(lampPost);
return lampPost;
}
然而,这样做的缺点是为每个制作的 LampPost 副本创建一个新的绘制函数(大概是闪烁函数)。 允许每个函数只有一个副本的另一种方法是将构造函数与原型一起使用。
function LampPost(x, y) {
//Make sure LampPost is called as a constructor
if(!this instanceof LampPost) {
return new LampPost(x, y);
}
this.lamp_xsprite = 0;
this.lamp_ysprite = 0;
this.light_xsprite = 0;
this.lightysprite = 0;
this.x = x; //Reference x argument here
this.y = y; //Reference y argument here
//Flicker effects
this.lightFlicker = 0;
this.seconds_Off = 0;
this.seconds_On = 0;
this.randomLength_Off = 500;
this.randomLength_On = 150;
globalLampPostArray.push(this);
}
LampPost.prototype.draw = function(x, y) {
/* Draw function code */
}
LampPost.prototype.flicker = function() {
/* Flicker code */
}
现在,灯柱的每个副本都将引用相同的绘制和闪烁函数,这也具有能够重新定义所有或仅针对一个实例的行为的好处。 此版本的开头有一个检查,以确保它被调用为构造函数。 为了使用原型属性,必须使用 new 关键字调用函数。 这只会确保对 LampPost 的所有调用都将返回 LampPost 的新实例。
另请注意,在每个函数的末尾,我将每个实例推送到数组中以供将来参考,因为这也是您问题的一部分。 这使您可以执行以下操作:
function drawLampPosts() {
for (var i = 0, len = globalLampPostArray.length; i < n; i++) {
globalLampPostArray[i].draw();
};
}
虽然,你也可以有一个数组,其中包含所有可绘制的游戏对象(而不仅仅是一个用于灯柱),并且只需在一个循环中完成。
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 设置变量时破坏游戏代码
- Javascript中非常基本的骰子游戏-尝试登录'获胜'变量
- Sails.js路由,url中有变量.ex/游戏/(此处ID)
- 如何防止javascript和html5游戏中的变量注入
- 对于Javascript游戏引擎,重用全局变量是否比使用局部变量更有效?
- 在制作多人游戏时,我是否依赖于浏览器不提供变量给玩家?
- 无法在基于javascript的游戏中初始化全局变量
- JavaScript-我该如何修复这个石头/纸/剪刀游戏?不能在if/else-if/else语句中使用两个变量
- 我需要从我的游戏中返回一个变量
- PHP 变量,用于游戏的 Javascript 代码中
- HTML5 - 启动共享相同变量的游戏对象
- 网页游戏-保存JavaScript变量到MySQL