【问题标题】:How to collide using OOP JavaScript?如何使用 OOP JavaScript 进行碰撞?
【发布时间】:2018-11-03 19:36:48
【问题描述】:

我正在为我的项目使用 OOP javascript 制作游戏(射击游戏)。我已经移动(玩家)并使敌人消失在容器的边缘。现在,我想与射手(玩家)和敌人发生碰撞并显示消息“游戏结束”。这是我的enemy.js 和shooter.js 代码。另外,我的container.js。

container.js

class Container{
    constructor(){
        this.containerElement = document.getElementsByClassName("container")[0];
    }
    initialization(){
        this.shooterElement = document.getElementsByClassName("shooter")[0];
        let containerElement = document.getElementsByClassName("container")[0];
        this.backgroundElement = document.getElementsByClassName("background")[0];
        this.background = new Background(this.backgroundElement);
        this.shooter = new Shooter(this.shooterElement);
        document.addEventListener('keydown', (e)=>this.shooter.buttonGotPressed(e));
        let enemyElement = document.getElementsByClassName("enemy")[0];
        this.enemies = [];
        setInterval(function(){
            var top = Math.floor(Math.random() * 50) + 1;
            var left = Math.floor(Math.random() * 550) + 1;
            this.enemy = new Enemy({parentElement: containerElement, leftPosition: left, topPosition: top});
        },400)
        this.enemies.push(this.enemy); 
    }
}
let container = new Container();
container.initialization();

enemy.js

class Enemy{
     constructor({parentElement,leftPosition, topPosition }){
        let enemyElement = document.createElement("div");
        this.enemyElement = enemyElement;
        this.leftPosition = leftPosition;
        this.topPosition = topPosition;
        this.containerHeight = 600;
        this.y = 15;
        this.height = 40;
        this.width = 50;
        this.enemyElement.style.left = this.leftPosition + 'px';
        this.enemyElement.style.top= this.topPosition + 'px'; 
        this.enemyElement.style.height = this.height + 'px';
        this.enemyElement.style.width = this.width + 'px';
        this.enemyElement.style.position = "absolute";
        this.enemyElement.style.background="url(images/enemy3.png)";   
        console.log(enemyElement)
        parentElement.appendChild(this.enemyElement);
        this.containerCollision = this.containerCollision.bind(this);
        setInterval(this.containerCollision, 100);
    }
    containerCollision(){
        if(this.topPosition >= this.containerHeight - this.width ){
            this.enemyElement.style.display="none";
        }
        this.topPosition = this.topPosition + this.y;
        this.enemyElement.style.top = this.topPosition + 'px';    
    }
}

shooter.js(这是播放器)

class Shooter {
  constructor(shooterElement){
        this.shooterElement = shooterElement;
        this.shooterleftPosition = 300;
        this.shootertopPosition = 555;
        this.containerWidth = 600;
        this.width = 30;
        this.height = 40;
        this.x = 5;
        this.y = 1;
        this.shooterElement.style.left = this.shooterleftPosition + 'px';
        this.shooterElement.style.top = this.shootertopPosition + 'px';
        this.buttonGotPressed = this.buttonGotPressed.bind(this);
    }
    buttonGotPressed(e){
        console.log(e);
        if(e.key == "ArrowLeft"){
            console.log(e.key);
            if(this.shooterleftPosition <= 0){
                this.shooterleftPosition = 0;
            }
            else
            {
            this.shooterleftPosition = this.shooterleftPosition - this.x;                
            }
            console.log(this.shooterleftPosition)
            this.shooterElement.style.left = this.shooterleftPosition +'px'; 
        }
        if(e.key == "ArrowRight"){
            this.shooterleftPosition = this.shooterleftPosition + this.x;
            this.shooterElement.style.left = this.shooterleftPosition +'px';
            if(this.shooterleftPosition >= this.containerWidth - this.width){
                this.shooterleftPosition = this.containerWidth - this.width;
                this.shooterElement.style.left = this.leftPosition + 'px';
            }
        }
    }
}
     Now, How do I detect and collide shooter(player) and enemy and display the message/alert "Game Over". If the enemy touches the shooter(player). Here is pic too.

How do i know enemy touched the player and display game over in this pic

我试图检测玩家和敌人的碰撞

 collidingShooter(){
            if (this.enemies.push(this.enemy)>1) 
            {
(
                 ([this.enemies][this.enemy].top <= this.shootertopPosition + 50) &&
            ([this.enemies][this.enemy].top >= this.shootertopPosition) &&
             ([this.enemies][this.enemy].left >= this.shooterleftPosition) &&
            ([this.enemies][this.enemy].left <= this.shooterleftPosition+ 50) 

)
            console.log("hitttt");
            this.enemies.splice(this.enemy,1);
           // this.shooterElement.splice(1);
            }

         }

【问题讨论】:

  • 嗨,Vatsat,您可以添加您目前尝试过的内容吗?
  • 好的,你有什么问题?
  • 我添加了到目前为止我尝试过的内容。 :)
  • 我的问题是玩家和敌人没有碰撞。正如您在图片和代码中看到的那样......玩家(射手)和敌人没有碰撞检测。
  • 你试过了,它给你一个错误或者它不起作用?也许你应该花点时间想想哪里出了问题。

标签: javascript oop collision-detection


【解决方案1】:

我相信你在这里搞砸了 首先你以错误的方式使用数组:

[this.enemies][this.enemy].top

它的作用: 使用一个元素创建新数组(即 this.enemies 数组),现在它将 this.enemy 转换为 string = 'object Object' 并尝试在这个新数组中找到一个键,它可能返回 undefined 并且现在它尝试获取未定义的顶部。 您可能想要做的是:

this.enemies[this.enemies.indexOf(this.enemy)].top

但即使它没有 oop 意义。 我会做什么:

function checkCollision() {
  for (let i = 0; i < this.enemies.length; i++) {
    if (areColliding(this.shooter, this.enemies[i])) {
      console.log('hit')
    }
  }
}

function areColliding(o1, o2) {
  if (o1.top <= o2.top + o2.height &&
    o1.top >= o2.top &&
    o1.left <= o2.left + o2.width &&
    o1.left >= o2.left) {
    return true
  }
  return false
}

有比 aabb 更好的碰撞算法,即使您想以这种方式执行此操作,如果您有数千个敌人,您的游戏也会变得非常慢。有一种算法可以将游戏世界分割成更小的矩形并检查它们内部的碰撞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2017-06-09
    • 2012-09-15
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多