简单的Javascript游戏:可能的对象数组错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了简单的Javascript游戏:可能的对象数组错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4420字,纯文字阅读大概需要7分钟。
内容图文
我正在构建一个小型的JavaScript游戏,但在查看教程和诸如此类的东西后,它只是不适合我.为了省去一些麻烦,这里是我认为可能出错的部分(实际问题在下面解释了一下).
它现在运行在一个非常基本的循环上,我有一个数组,用于在射击时保持玩家的螺栓:
var playerBolts=new Array(); //Holds all the bolt objects that the player shoots
setInterval(function(){
updateGame();
drawGame();
},25);
这是玩家射击时创建的螺栓对象.
function bolt(facing,playerX,playerY){ //The bolt object is shot from the player's current position
this.facingLeft=facing; //The direction at which the bolt moves, if left, true
this.x=playerX; //The x position of the bolt
this.y=playerY; //The y position of the bolt
if(facingLeft==true){
this.xSpeed=-3; //The horizontal speed at which the bolt is moving
}
else if (facingLeft==false){
this.xSpeed=3;
}
this.ySpeed=0; //The vertical speed at which the bolt is moving
this.W=3; //The width of the bolt's model
this.H=3; //The height of the bolt's model
this.color="red"; //The color of the bolt's model
this.update=update;
function update(){ //Updates the bolt's properties
this.x=this.x+this.xSpeed;
this.y=this.y+this.ySpeed;
}
this.draw=draw;
function draw(){ //Draws the bolt's model to the canvas
context.fillStyle=this.color;
context.fillRect(this.x, this.y, this.W, this.H);
}
}
当“玩家”射击时,来自玩家对象的shootBolt方法被调用:
function player(){ //The player object
this.facingLeft=true; //If the player's character is facing left, true
this.x=100; //The x position of the player
this.y=100; //The y position of the player
this.shootBolt=shootBolt;
function shootBolt(){ //Shoots a bolt, creating a new bolt object and adding it to the playerBolts array
playerBolts.push(bolt(this.facingLeft,this.x,this.y));
}
}
问题是下一个螺栓每次后续射击都会变快.你开火得越多,他们就越快.此外,如果快速射击,应该可以看到多个螺栓,但每次射击时,前一个都会消失.
现在游戏循环更新和绘制功能.我用了一个
function updateGame(){ //The main update phase
player1.update(); //Updates the player's properties
playerBolts.forEach(function(bolt){ //Updates all active bolts's properties
this.update();
});
}
function drawGame(){ //The main drawing phase
context.fillStyle="white";
context.fillRect(0,0,canvasW,canvasH); //Clears the canvas for the next frame
player1.draw(); //Draws the player
playerBolts.forEach(function(bolt){ //Draws all bolt's model to the canvas
this.draw();
});
}
所以…我认为这可能与我用数组“push”添加对象的方式有关,即“forEach”方法(虽然我也尝试了for循环).我不知道我做错了什么,而且我已经找到了消息来源,这应该是不行的?如果没有足够的信息,我总是可以发布整个事情(只有119个记录良好的行).
谢谢.
解决方法:
我怀疑你有这个问题.您正在通过调用以下方法构造bolt对象
bolt(this.facingLeft, this.x, this.y)
但是,在bolt函数内部,您使用它就好像它引用了一个新创建的螺栓.不幸的是,事实并非如此.尝试构建这样的螺栓:
new bolt(this.facingLeft, this.x, this.y)
如果你这样做,那么这个bolt内部引用了新创建的对象.
此外,这也可能是错误的:
playerBolts.forEach(function(bolt){ //Draws all bolt's model to the canvas
this.draw();
});
由于奇怪的原因,你的循环函数中的这个可能是也可能不是你的螺栓(见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach).为了安全起见,请尝试以下方法:
playerBolts.forEach(function(bolt){ //Draws all bolt's model to the canvas
bolt.draw();
});
顺便说一下,新问题非常普遍;我养成了构建函数的习惯,这样它们可以使用或不使用new.要做到这一点,不要使用它,只需让螺栓返回一个新对象而不是操纵它.因为JS这令人困惑,我发现这是一个更好的方法:
function bolt(facing,playerX,playerY){ //The bolt object is shot from the player's current position
var theBolt = {
facingLeft: facing, //The direction at which the bolt moves, if left, true,
x: playerX, //The x position of the bolt
y: playerY, //The y position of the bolt
xSpeed: facingLeft ? -3 : 3, //The horizontal speed at which the bolt is moving
ySpeed: 0, //The vertical speed at which the bolt is moving
W: 3, //The width of the bolt's model
H: 3, //The height of the bolt's model
color: 'red', //The color of the bolt's model
update: update,
draw: draw
};
function update(){ //Updates the bolt's properties
theBolt.x = theBolt.x + theBolt.xSpeed;
theBolt.y = theBolt.y + theBolt.ySpeed;
}
function draw(){ //Draws the bolt's model to the canvas
context.fillStyle = theBolt.color;
context.fillRect(theBolt.x, theBolt.y, theBolt.W, theBolt.H);
}
return theBolt;
}
内容总结
以上是互联网集市为您收集整理的简单的Javascript游戏:可能的对象数组错误全部内容,希望文章能够帮你解决简单的Javascript游戏:可能的对象数组错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。