简体   繁体   English

ECMAScript6中类中的碰撞函数

[英]Collision function inside a class in ECMAScript6

I was wondering if anyone could help me, I am trying to create a collision function inside a class Ball. 我想知道是否有人可以帮助我,我正在尝试在类Ball中创建碰撞功能。 I am stuck as I don't know how to refer to a second ball object inside the Ball class. 我被卡住了,因为我不知道如何引用Ball类中的第二个球对象。

Any help would be very appreciated best regards, Van 任何帮助将非常感谢最好的问候,范

class Ball {

  constructor(bX, bY, bRadius, bcolor, dX, dY) {
    this.bX = bX;
    this.bY = bY;
    this.bRadius = bRadius;
    this.bcolor = bcolor;
    this.dX = dX;
    this.dY = dY;
  }

  drawBall() {
    //
  }

  moveBall() {
    //
  }

  bounce() {
    //
  }

  collisions(Ball ball2) { //don't know how to refer to Ball & ball2
    var deltaX = this.bX - ball2.bX;
    var deltaY = this.bY - ball2.bY;
    // ....
    if (sqDistance <= sqRadius) {
      alert("going to hit!");
  }
};

ES6 does not support type of parameters. ES6不支持参数类型。

So your code should just omit type collisions(ball2) { 所以你的代码应该省略类型collisions(ball2) {

Look at TypeScript when you need strict type check. 需要严格类型检查时,请查看TypeScript。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM