Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当封装变量可用时,引用全局变量_Javascript_Oop_Encapsulation - Fatal编程技术网

Javascript 当封装变量可用时,引用全局变量

Javascript 当封装变量可用时,引用全局变量,javascript,oop,encapsulation,Javascript,Oop,Encapsulation,在下面的例子中,“player”指的是全局变量,这打破了OOP的封装原则。无论我需要什么,从全局范围通过构造函数传递到类实例。我怎么能做到 若我创建父变量(某些字符),那个么代码将不起作用。也许这是一个裸体问题,但我不知道它是如何运作的 p、 当我把“敌人”中的“玩家”看成“这个”时,所有的结构都崩溃了 var Enemy = function(x, y, speed, player) { this.x = x; this.y = y; this.speed = speed; t

在下面的例子中,“player”指的是全局变量,这打破了OOP的封装原则。无论我需要什么,从全局范围通过构造函数传递到类实例。我怎么能做到

若我创建父变量(某些字符),那个么代码将不起作用。也许这是一个裸体问题,但我不知道它是如何运作的

p、 当我把“敌人”中的“玩家”看成“这个”时,所有的结构都崩溃了

var Enemy = function(x, y, speed, player) {
  this.x = x;
  this.y = y;
  this.speed = speed;
  this.sprite = 'images/enemy-bug.png';
  this.player = player;
};

Enemy.prototype.update = function(dt) {
  this.x += +(this.speed * dt);

  if (this.x > canvasWidth) {
    this.x = borgSpawn;
    this.speed = Math.floor(100 + Math.random() * 200);
  }

  if (
    player.x > this.x - enemySize &&
    player.x < this.x + enemySize &&
    player.y > this.y - enemySize &&
    player.y < this.y + enemySize
  ) {
    player.x = playerSpawnX;
    player.y = playerSpawnY;
  }
};

Enemy.prototype.render = function() {
  ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};

var enemyLocation = [60, 140, 220];

var allEnemies = enemyLocation.map(y => new Enemy(0, y, 300, player));

var Player = function(x, y) {
  this.x = x;
  this.y = y;
  this.sprite = 'images/char-boy.png';
};

Player.prototype.update = function() {
  if (this.y < 60) {
    this.x = playerSpawnX;
    this.y = playerSpawnY;
  }
};

Player.prototype.render = function() {
  ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};

Player.prototype.handleInput = function(key) {
  if (key === 'left' && this.x > 0) {
    this.x = this.x - horizontalStep;
  } else if (key === 'right' && this.x != stepZone) {
    this.x = this.x + horizontalStep;
  } else if (key === 'up') {
    this.y = this.y - verticalStep;
  } else if (key === 'down' && this.y != stepZone) {
    this.y = this.y + verticalStep;
  }
};

var player = new Player(playerSpawnX, playerSpawnY);

document.addEventListener('keyup', function(e) {
  var allowedKeys = {
    37: 'left',
    38: 'up',
    39: 'right',
    40: 'down'
  };

  player.handleInput(allowedKeys[e.keyCode]);
});
var敌人=功能(x、y、速度、玩家){
这个.x=x;
这个。y=y;
速度=速度;
this.sprite='images/敌方bug.png';
this.player=player;
};
defey.prototype.update=函数(dt){
this.x++(this.speed*dt);
如果(此.x>画布宽度){
这个.x=伯格斯潘;
this.speed=Math.floor(100+Math.random()*200);
}
如果(
player.x>这个.x-敌人化&&
player.x<此.x+敌人化&&
player.y>这个.y-敌人化&&
player.y<此.y+敌人化
) {
player.x=playerSpawnX;
player.y=playerSpawnY;
}
};
defey.prototype.render=函数(){
drawImage(Resources.get(this.sprite)、this.x、this.y);
};
var enemyLocation=[60140220];
var allEnemies=enemyLocation.map(y=>新敌人(0,y,300,玩家));
变量Player=函数(x,y){
这个.x=x;
这个。y=y;
this.sprite='images/char-boy.png';
};
Player.prototype.update=函数(){
如果(该.y<60){
this.x=playerSpawnX;
this.y=playerSpawnY;
}
};
Player.prototype.render=函数(){
drawImage(Resources.get(this.sprite)、this.x、this.y);
};
Player.prototype.handleInput=函数(键){
if(key=='left'&&this.x>0){
this.x=this.x-水平步长;
}else if(key==='right'&&this.x!=stepZone){
此.x=此.x+水平步长;
}否则,如果(键=='up'){
this.y=this.y-垂直步长;
}else if(key==='down'&&this.y!=stepZone){
this.y=this.y+垂直步长;
}
};
var player=新玩家(playerSpawnX、playerSpawnY);
文件。添加的文件列表器(“键控”,功能(e){
var allowedKeys={
37:"左",,
38:"向上",,
39:"对",,
40:‘向下’
};
player.handleInput(允许键[e.keyCode]);
});
无需将“玩家”参数传递给“敌人”对象。没有此引用,您的敌人.prototype.update函数将正常工作