Javascript-使用同一参数创建多个对象
我试图在射击游戏中创造多个“子弹” 出于某种原因,我只能创建一个,我假设这是因为我没有正确地创建多个bullet对象 下面是我用来制作拍摄功能的代码。有人能给我指出正确的方向,告诉我如何在onclick上重新创建多个子弹吗Javascript-使用同一参数创建多个对象,javascript,object,canvas,onclick,draw,Javascript,Object,Canvas,Onclick,Draw,我试图在射击游戏中创造多个“子弹” 出于某种原因,我只能创建一个,我假设这是因为我没有正确地创建多个bullet对象 下面是我用来制作拍摄功能的代码。有人能给我指出正确的方向,告诉我如何在onclick上重新创建多个子弹吗 bullet = { x: null, y: null, width: 10, height: 10, direction: null, update: function(){ if(this.direct
bullet = {
x: null,
y: null,
width: 10,
height: 10,
direction: null,
update: function(){
if(this.direction == null){
if(lastKeyPress == null){
lastKeyPress = up;
}
this.direction = lastKeyPress;
}
if(this.direction == up){ this.y -=7; }
if(this.direction == down){ this.y +=7; }
if(this.direction == left){ this.x -=7; }
if(this.direction == right){ this.x +=7; }
},
draw: function() {
if(this.x == null){
this.x = player.x + (player.width/4);
}
if(this.y == null){
this.y = player.y + (player.height/4);
}
cContext.fillRect(this.x, this.y, this.width, this.height);
}
}
function main(){
canvas = document.getElementById("mainCanvas");
cContext = canvas.getContext("2d");
keystate = {};
document.addEventListener("keydown", function(evt) {
keystate[evt.keyCode] = true;
});
document.addEventListener("keyup", function(evt) {
delete keystate[evt.keyCode];
});
document.addEventListener("click", function(evt) {
bullets[bulletNum] = bullet;
bullets[bulletNum].draw();
bulletNum++;
});
init();
var loop = function(){
update();
draw();
window.requestAnimationFrame(loop, canvas);
}
window.requestAnimationFrame(loop, canvas);
}
function update() {
for (i = 0; i < bullets.length; i++) {
bullets[i].update();
}
player.update();
ai.update();
}
function draw() {
cContext.clearRect(0, 0, WIDTH, HEIGHT);
cContext.save();
for (i = 0; i < bullets.length; i++) {
bullets[i].draw();
}
player.draw();
ai.draw();
cContext.restore();
}
bullet={
x:null,
y:空,
宽度:10,
身高:10,
方向:空,
更新:函数(){
如果(this.direction==null){
如果(lastKeyPress==null){
lastKeyPress=向上;
}
this.direction=最后一次按键;
}
如果(this.direction==up){this.y-=7;}
如果(this.direction==down){this.y+=7;}
如果(this.direction==left){this.x-=7;}
如果(this.direction==right){this.x+=7;}
},
绘图:函数(){
if(this.x==null){
这个.x=player.x+(player.width/4);
}
if(this.y==null){
this.y=player.y+(player.height/4);
}
cContext.fillRect(this.x,this.y,this.width,this.height);
}
}
函数main(){
canvas=document.getElementById(“mainCanvas”);
cContext=canvas.getContext(“2d”);
keystate={};
文件.添加的EventListener(“向下键控”,功能(evt){
keystate[evt.keyCode]=真;
});
文件.附录列表(“键控”,功能(evt){
删除keystate[evt.keyCode];
});
文档.添加的EventListener(“单击”,函数(evt){
子弹[bulletNum]=子弹;
项目符号[bulletNum].draw();
bulletNum++;
});
init();
var循环=函数(){
更新();
draw();
requestAnimationFrame(循环,画布);
}
requestAnimationFrame(循环,画布);
}
函数更新(){
对于(i=0;i
问题是,一旦你射出一颗子弹,你就不能再射出了
我知道这里有很多代码,任何帮助都会很棒。您想使用原型模式:
var Bullet = function() {
this.x = null;
this.y = null;
this.width = 10;
this.height = 10;
this.direction = null;
};
Bullet.prototype.update = function() {...};
Bullet.prototype.draw = function() {...};
var bullet = new Bullet();
在javascript中定义对象的另一种方法是使用prototype,例如:
职能人员(姓名){
this.name=名称;
}
Person.prototype.sayHello=函数(){
var res=“你好,我是”+this.name;
返回res;
}
var jhon=新人(“jhon”);
var rose=新人(“rose”);
document.getElementById('jhon').innerHTML=jhon.sayHello();
document.getElementById('rose').innerHTML=rose.sayHello()代码>
查看原型模式:项目符号[bulletNum]=项目符号代码>。。。总是引用相同的object@charlietflbulletNum
实际上是未定义的@乔恩,你从来不会在任何地方制造不同的子弹。请不要发布变量未定义的问题。ai
也未定义。它们不是未定义的,我没有发布所有代码,只是发布了所需的部分。或者var newBullet=Object.create(bullet)本着保持OP代码不变的精神,sameObject.create(bullet)很不错,但是更老的浏览器(IE@JuanMendesObject.create(bullet)
如果bullet
的属性是Object@PaulS.以与执行Bullet.prototype.some={};var b=new Bullet()完全相同的方式
prototype的样式对于在prototype上共享对象没有影响