簡體   English   中英

初學者在這里。 代碼不服從。 我認為這個問題與訪問 class 中的變量有關嗎?

[英]Beginner here. Code isn't obeying. I think the problem has something to do with accessing a variable that's within a class?

所以我正在學習如何使用類,我已經使用 P5.JS 制作了這段代碼(它正好有 100 行長。)它應該畫三個矩形和三個圓,其中一個圓可以移動用箭頭鍵。 其他兩個跟隨那個:這是不起作用的部分,當跟隨者圓圈移動到正方形上時。 它應該讓他們在移動時放慢速度。 我什么都試過了。

function setup() {
  createCanvas(windowWidth, windowHeight);
  noStroke();
  character1 = new character(250, 200, 30, 4);
  follower1 = new enemy(100, 20, 30, 2);
  follower2 = new enemy(200, 20, 30, 2);
  obstacle1 = new obstacle(200, 200, 100, 50, follower1.x, follower1.y, follower1.s);
  obstacle2 = new obstacle(300, 300, 50, 25, follower1.x, follower1.y, follower1.s);
  obstacle3 = new obstacle(200, 400, 25, 50, follower1.x, follower1.y, follower1.s);
}

function draw() {
  background(0);
  character1.drawCharacter();
  follower1.drawEnemy();
  follower2.drawEnemy();
  obstacle2.drawObstacle();
  obstacle1.drawObstacle();
  obstacle3.drawObstacle();
}

class character {
  constructor(x, y, d, s) {
    this.x = x;
    this.y = y;
    this.d = d;
    this.s = s;
  }
  drawCharacter() {
    fill(255);
    ellipse(character1.x, character1.y, character1.d);
    if (keyIsDown(UP_ARROW)) {
      character1.y -= character1.s;
    }
    if (keyIsDown(DOWN_ARROW)) {
      character1.y += character1.s;
    }
    if (keyIsDown(LEFT_ARROW)) {
      character1.x -= character1.s;
    }
    if (keyIsDown(RIGHT_ARROW)) {
      character1.x += character1.s;
    }
  }
}

class enemy {
  constructor(x, y, d, s) {
    this.x = x;
    this.y = y;
    this.d = d;
    this.s = s;
  }

  drawEnemy() {
    fill(255, 0, 0);
    ellipse(this.x, this.y, this.d);
    if (this.x < character1.x - this.d) {
      this.x += this.s;
    }
    if (this.x > character1.x + this.d) {
      this.x -= this.s;
    }
    if (this.y < character1.y - this.d) {
      this.y += this.s;
    }
    if (this.y > character1.y + this.d) {
      this.y -= this.s;
    }
  }
}

class obstacle {
  constructor(x, y, w, h, enemyX, enemyY, enemyS) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    enemyX = enemyX;
    enemyY = enemyY;
    enemyS = enemyS;
  }
  drawObstacle(enemyX, enemyY, enemyS) {
    fill(255);
    rectMode(CENTER);
    rect(this.x, this.y, this.w, this.h);
    if (
      this.x - this.w / 2 - 10 < enemyX &&
      this.x + this.w / 2 + 10 > enemyX &&
      this.y - this.h / 2 - 10 < enemyY &&
      this.y + this.h / 2 + 10 > enemyY
    ) {
      enemyS = 0.5;
    } else {
      enemyS = 2.5;
    }
  }
}

嘗試只設置一個障礙。 這是我的猜測:

draw 調用了三次obstacle3.drawObstacle ,如果一個敵人應該被obstacle.drawObstacle 1 或obstacle1 2 減速,沒關系,因為敵人會被obstacle2 enemy.s重置

我強烈建議你的每個角色和敵人 class 都有一個drawmove方法,並將所有關於敵人如何移動的信息都放在敵人 class 中。 也許障礙 class 應該有一個draw ,也許是一個敵人enemy.move會調用的isCollidingWith方法。

將 class 的所有邏輯保留在 class 中意味着您可以更輕松地進行變體。 例如,也許稍后你會想要一個速度慢得多但完全不受障礙物阻礙的敵人。 擁有 class 中的所有邏輯使這變得容易。 如果你有足夠多不同類型的敵人,將邏輯置於障礙 class 將很快成為一場噩夢。

此外,您可以擁有一個game object 包含游戲的所有內容,包括characterenemiesobstacles屬性以及調用角色、敵人和障礙物對象的相應方法的drawmove方法,以及清除其對象的destroyquit方法屬性之類的。 (在某些語言中,這很重要,因為如果您要為游戲中的每個部分添加game屬性,它們將具有彼此的句柄,並且因為(在某些語言中)垃圾收集只獲取沒有引用它們的對象,它們占用 memory 的時間會比你想要的長得多。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM