Javascript 基于js的2d游戏中的具体问题

Javascript 基于js的2d游戏中的具体问题,javascript,this,Javascript,This,我用JavaScript制作了一个2d游戏(我知道,有更好的语言专门用于2d游戏,但现在我用的是js)。已经有一个10x10的2d地图,插入玩家和敌人的图像,使用箭头键行走,我对战斗系统有问题。这是它认为错误的地方 function Character(n,h1,so,sa,x,y,t) { this.name=n; this.hpmax=h1; this.hp=800; this.sila_obrony=so; //defence this.sila_ataku=sa; //attack

我用JavaScript制作了一个2d游戏(我知道,有更好的语言专门用于2d游戏,但现在我用的是js)。已经有一个10x10的2d地图,插入玩家和敌人的图像,使用箭头键行走,我对战斗系统有问题。这是它认为错误的地方

function Character(n,h1,so,sa,x,y,t)  
{
this.name=n;
this.hpmax=h1;
this.hp=800;
this.sila_obrony=so; //defence
this.sila_ataku=sa; //attack
this.x=x;
this.y=y;
this.type=t; //player(pl), golem(gl), troll(tr), cyklop(ck), procarz(pr), boss(bs)

this.atak = function(enemy)
{
  if(((enemy.x==this.x)||(enemy.x==this.x-1)||(enemy.x==this.x+1))&&
  ((enemy.y==this.y)||(enemy.y==this.y-1)||(enemy.y==this.y+1)))    
  {
    alert('<span style="color:red">'+this.name+" Atakuje "+enemy.name+"</span><br>");
    var min = this.sila_ataku-(this.sila_ataku/5);
    var max = this.sila_ataku+(this.sila_ataku/5);
    var damage=Math.floor(Math.random() * (max - min + 1) ) + min-enemy.sila_obrony;    
    enemy.hp-=damage;
    alert(enemy.hp);
  }else 
  {
    alert("You are not around");
    alert("Player's x: "+this.x+" Player's y: "+this.y+" Enemy's x: "+enemy.x+" Enemy's y: "+enemy.y);
  }
}

this.insert = function() //inserting character on 2d map
{
  var character = document.createElement("img");

  if(typeof this.exp !== 'undefined') character.className = "character"; //only player has exp field
  else character.className = "enemy";

  character.addEventListener('click', function() {Player.atak(this);}, false);
  character.style.left = this.x*60+"px"; //this code counts how move character's image on map
  character.style.top = this.y*50-50+"px";
  if (this.type=="pl") {character.src="img/character.png"; character.id = "pl";}
  if (this.type=="gl") {character.src="img/golem.png"; character.id = "gl";}
  if (this.type=="tr") {character.src="img/troll.png"; character.id = "tr";}
  if (this.type=="ck") {character.src="img/cyklop.png"; character.id = "ck";}
  if (this.type=="pr") {character.src="img/procarz.png"; character.id = "pr";}
  if (this.type=="bs") {character.src="img/boss.png"; character.id = "bs";}

  document.getElementById("map").appendChild(character);
}
}
函数字符(n,h1,so,sa,x,y,t) { this.name=n; 该参数为.hpmax=h1; 这是hp=800; this.sila_obrony=so;//防御 this.sila_ataku=sa;//攻击 这个.x=x; 这个。y=y; this.type=t;//玩家(pl)、傀儡(gl)、巨魔(tr)、cyklop(ck)、procarz(pr)、boss(bs) this.atak=功能(敌人) { if(((敌方.x==this.x)| |(敌方.x==this.x-1)| |(敌方.x==this.x+1))&& ((敌方.y==this.y)| |(敌方.y==this.y-1)| |(敌方.y==this.y+1))) { 警报(“”+此.name+“Atakuje”+敌人.name+“
”); var min=this.sila_ataku-(this.sila_ataku/5); var max=this.sila_ataku+(this.sila_ataku/5); var伤害=数学地板(数学随机()*(最大-最小+1))+min-defect.sila_obrony; 敌人。hp-=伤害; 警报(敌方.hp); }否则 { 警惕(“你不在附近”); 警报(“玩家的x:+这个.x+”玩家的y:+这个.y+“敌人的x:+敌人的x:+敌人的x:+敌人的y:+敌人的y:”); } } this.insert=function()//在二维地图上插入字符 { var character=document.createElement(“img”); if(typeof this.exp!=“undefined”)character.className=“character”;//只有玩家有exp字段 else character.className=“敌人”; character.addEventListener('click',function(){Player.atak(this);},false); character.style.left=this.x*60+“px”;//此代码计算如何在地图上移动角色的图像 character.style.top=this.y*50-50+“px”; if(this.type==“pl”){character.src=“img/character.png”;character.id=“pl”} if(this.type==“gl”){character.src=“img/golem.png”;character.id=“gl”} if(this.type==“tr”){character.src=“img/troll.png”;character.id=“tr”} if(this.type==“ck”){character.src=“img/cyklop.png”;character.id=“ck”} if(this.type==“pr”){character.src=“img/procarz.png”;character.id=“pr”} if(this.type==“bs”){character.src=“img/boss.png”;character.id=“bs”} document.getElementById(“map”).appendChild(字符); } } 这就是我收到的

我原以为战斗会成功,但有“你们不在身边”的警报和来自宇宙的敌人的呼救声。此外,当我点击自己时,我会发出警报“玩家的x:2玩家的y:2敌人的x:135敌人的y:258”,但在这种情况下,玩家和敌人是同一个对象

我认为解决方案“就在眼前”,但我可以自己找到


如果有人能解释为什么会有奇怪的x和y值,我将不胜感激

我知道,有更好的语言专门用于2d游戏,但现在我在js中
->我打赌js的效率实际上要高得多(至少对于较小的游戏)。不要注意WebVR和其他很酷的东西…
我知道,有更好的语言专门用于2d游戏,但现在我在js中
->我打赌js的生产率实际上要高得多(至少对于较小的游戏)。不要注意WebVR和其他很酷的东西。。。