Javascript函数在第一次被调用时起作用,但在第二次或之后的任何时间都不会起作用
下面的代码是我正在使用的代码,我正在尝试创建一个冲突系统,我计划在多个项目中使用该系统。但每次我尝试创建第二个实体来测试碰撞系统是否工作时,它只返回以下错误 ncaught TypeError:实体不是函数 在敌人的游戏中。html:103 更新后的game.html:110 代码段Javascript函数在第一次被调用时起作用,但在第二次或之后的任何时间都不会起作用,javascript,function,call,typeerror,Javascript,Function,Call,Typeerror,下面的代码是我正在使用的代码,我正在尝试创建一个冲突系统,我计划在多个项目中使用该系统。但每次我尝试创建第二个实体来测试碰撞系统是否工作时,它只返回以下错误 ncaught TypeError:实体不是函数 在敌人的游戏中。html:103 更新后的game.html:110 代码段 在实体函数的末尾,您将其重新指定为self对象,这意味着在您第一次调用实体之后。。。它将不再是一个函数,而是创建的第一个self对象。所以我认为只要去掉实体=自我;会解决眼前的问题。谢谢你,这确实解决了问题。事实上
在实体函数的末尾,您将其重新指定为self对象,这意味着在您第一次调用实体之后。。。它将不再是一个函数,而是创建的第一个self对象。所以我认为只要去掉实体=自我;会解决眼前的问题。谢谢你,这确实解决了问题。事实上,我在一段时间前就开始编写这段代码了,不幸的是,直到今天,我才花了一两周的时间写下这段代码。所以下一次我记得在做实验时添加注释。
<DOCTYPE html>
<html>
<canvas id="can" width="600" height="600" style="border:2px solid #000000"></canvas>
<script language "JavaScript">
var can = document.getElementById("can").getContext("2d");
can.font = '20px Arial';
var HEIGHT = 610;
var WIDTH = 610;
var startTime = Date.now();
var frames = 0;
var score = 0;
var entitylist = [];
var entity = function(id,type,x,y,xspd,yspd,width,height,color) {
var self = {
id:id,
type:type,
x:x,
y:y,
xspd:xspd,
yspd:yspd,
width:width,
height:height,
color:color
};
self.draw = function() {
can.save();
can.fillStyle = self.color;
can.fillRect(self.x - self.width / 2,self.y - self.height / 2,self.width,self.height);
can.restore();
}
self.position = function() {
self.x = self.x + self.xspd;
self.y = self.y + self.yspd
if(self.x <= self.width-10) {
self.x = self.width;
self.xspd = -self.xspd;
self.x = self.x + self.xspd;
}
if(self.x >= WIDTH - self.width) {
self.x = WIDTH - self.width;
self.xspd = -self.xspd;
self.x = self.x + self.xspd;
}
if(self.y <= self.height-10) {
self.y = self.height;
self.yspd = -self.yspd;
self.y = self.y + self.yspd;
}
if(self.y >= HEIGHT - self.height) {
self.y = HEIGHT - self.height;
self.yspd = -self.yspd;
self.y = self.y + self.yspd;
}
}
squareCollison = function(sqr1,sqr) {
return sqr1.x <= sqr.x + sqr.width
&&sqr.x <= sqr1.x + sqr1.width
&&sqr1.y <= sqr.y + sqr.height
&&sqr.y <= sqr1.y + sqr1.height;
}
collisonTest = function(sqr1,sqr) {
var rect1 = {
x:sqr1.x - sqr1.width / 2,
y:sqr1.y - sqr1.height / 2,
width:sqr1.width,
height:sqr1.height
}
var rect2 = {
x:sqr.x - sqr.width / 2,
y:sqr.y - sqr.height / 2,
width:sqr.width,
height:sqr.height
}
return squareCollison(rect1,rect2);
}
self.update = function() {
self.position();
self.draw();
console.log('entity update')
}
entity = self;
entitylist[id] = self;
return self;
}
enemy = function() {
var id = Math.random();
var type = 'enemy';
var x = Math.random() * WIDTH;
var y = Math.random() * HEIGHT;
var xspd = 5 + Math.random() * 5;
var yspd = 5 + Math.random() * 5;
var width = 12 + Math.random()*10;
var height = 10 + Math.random()*10;
var color = 'red'
entity(id,type,x,y,xspd,yspd,width,height,color);
}
updated = function() {
can.clearRect(0,0,WIDTH,HEIGHT)
frames++;
if(frames % 50 === 0)
enemy();
for(E in entitylist){
entitylist[E].update(entity);
var collid = collisonTest(E,entitylist[E]);
if(collid){
console.log('colliding')
}
}
}
setInterval (updated,100);
</script>
</html>