首页 > 解决方案 > 为什么具有相同参数的 2 个对象具有不同的行为?

问题描述

基本上,我是编程新手(和 stackoverflow)并试图学习 p5.js 的基础知识。我是尝试 Pong 并潜入如何创造不同难度级别的人之一,我发现 2 个本应具有相同半径的球实际上没有(事实弄乱了我的命中框)并使用相同的常数来改变 Y 轴的速度会导致 2 个完全不同的 Y 速度。我几乎可以肯定我知道问题出在哪里,但我对它发生的原因一无所知,无论如何我都会在这里留下一个完整的链接。

我做了第二个球来让对手的球拍跟随它的路径,我很确定问题要么在于获得球的 y 位置,要么在于我之后如何使用它,因为当我评论那些事情时,事情就像我会喜欢它。

class Ball {
   constructor(_x, _y, _radius, _xSpeed, _ySpeed, _color){
      this.x = _x;
      this.y = _y;
      this.radius = _radius;
      this.xSpeed = _xSpeed;
      this.ySpeed = _ySpeed;
      this.color = _color;
   }

   //this guy is one of my suspects
   get ballY() {
      return this.y += this.ySpeed;
   }

   show() {
      fill(this.color)
      circle(this.x, this.y, 2 * this.radius); 
      fill(255)
   }
   moveY() {
      this.y += this.ySpeed;
   }

//leaving it here for context
class Paddle {
  constructor(_x, _y, _length, _height, _tag) {
    this.x = _x
    this.y = _y
    this.length = _length
    this.height = _height
    this.tag = _tag
  }

  //with this guy also in the list
  get opSpeed() {
    return ball2.ballY - this.y - 45;
  }

  //or maybe here
   move() {
      if(this.tag == "opponent") {
      this.y += this.opSpeed;
     }
   }

我将不胜感激任何可以说明此事的人,谢谢。

标签: javascriptp5.js

解决方案


推荐阅读