Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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_Collision Detection_P5.js - Fatal编程技术网

Javascript 直线碰撞

Javascript 直线碰撞,javascript,collision-detection,p5.js,Javascript,Collision Detection,P5.js,我正在用Javascript编写一个游戏,其中有一个玩家试图收集硬币。目前两者都是不同维度的rects(),我正在尝试合并一个函数,当用户收到硬币时,它会提醒用户。目前,这是我的播放器和硬币碰撞检测代码 isTouching(player) { return player.x + player.width > this.x && this.x + this.width > player.x &&

我正在用Javascript编写一个游戏,其中有一个玩家试图收集硬币。目前两者都是不同维度的rects(),我正在尝试合并一个函数,当用户收到硬币时,它会提醒用户。目前,这是我的播放器和硬币碰撞检测代码

isTouching(player) {

        return player.x + player.width > this.x &&
               this.x + this.width > player.x &&
               player.y + player.height > this.y &&
               this.y + this.height > player.y;

    }
然而,当我循环我的硬币数组并检查碰撞时,什么都没有发生。为什么会这样?以下是我与此相关的代码:

for (let x = 0; x < 5; x ++) { coins[x].collisions(); }
我的硬币和玩家都有自己的等级,我的硬币存储在一个数组中

let player;
let coins = [];
player =    new Player();
for (let x = 0; x < 5; x ++) { coins.push(new Coin()); }

只需阅读所有代码。我能够让硬币警报工作,以下是您需要更改的内容

在game.engine.js中,更改函数设置。在这里,我更新了您的循环,问题是您的随机硬币x和y需要传递给您的硬币类实例

function setup() {

  // Apply classes to empty variables
  console.log("Creating player...");
  player =    new Player();

  console.log("Creating world...");
  world  =    new World();

  console.log("Creating coins...");
  for (let i = 0; i < number_of_coins; i++) { coins.push(new Coin(coin_cords_X[i], coin_cords_Y[i])); }

  console.log("Creating controller...");
  controller = new Controller();

  // Draw canvas with set size
  console.log("Creating game screen...");
  createCanvas(1250, 750);

}
完成这两项工作后,它应该可以正常工作

我附加修改后的程序压缩。

编程的首要规则之一:不要重新发明轮子。如果那里有适合你需要的图书馆,请使用它。如果你将此作为一种学习体验,这是很公平的。为了节省我们通过你的代码,在其他地方,考虑提供一个在这里演示了问题抱歉,这是一个项目,我正在试图减少使用的图书馆,这意味着我需要编码我自己的功能。尽管如此,我还是尝试使用一个在以前的项目(bmoren的p52d)中为我工作过的库,但这也不起作用。我已经编辑了我的原始帖子并加入了我的新代码。这很有效,非常感谢!没问题。祝你的项目伙伴好运
if (this.touched()) {
        
     alert("you got a coin!");
        
}
        
touched() {
        
     return collideRectRect(this.x, this.y, this.width, this.height, player.x, player.y, player.width, player.height);
        
}
function setup() {

  // Apply classes to empty variables
  console.log("Creating player...");
  player =    new Player();

  console.log("Creating world...");
  world  =    new World();

  console.log("Creating coins...");
  for (let i = 0; i < number_of_coins; i++) { coins.push(new Coin(coin_cords_X[i], coin_cords_Y[i])); }

  console.log("Creating controller...");
  controller = new Controller();

  // Draw canvas with set size
  console.log("Creating game screen...");
  createCanvas(1250, 750);

}
class Coin {

    // Setup player attributes
    x;
    y;
    width;
    height;

    constructor(x, y) {

        this.x = x;
        this.y = y;
        this.width = 30;
        this.height = 30;

    }

    show(x) {
        fill(player.color);

        rect(this.x, this.y, this.width, this.height);

    }

  // rest of the methods will be as is
}