简体   繁体   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?

So I'm learning about how to use classes and I've made this code (it's exactly 100 lines long.) using P5.JS which is just supposed to draw three rectangles and three circles, One of the circles is able to be moved with the arrow keys.所以我正在学习如何使用类,我已经使用 P5.JS 制作了这段代码(它正好有 100 行长。)它应该画三个矩形和三个圆,其中一个圆可以移动用箭头键。 and the other two follow that one around: Here's the part that doesn't work, when the follower circles move over the squares.其他两个跟随那个:这是不起作用的部分,当跟随者圆圈移动到正方形上时。 it's supposed to make them slow down as they move over it.它应该让他们在移动时放慢速度。 I've tried everything.我什么都试过了。

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;
    }
  }
}

Try only having one obstacle.尝试只设置一个障碍。 Here's my guess:这是我的猜测:

draw calls obstacle.drawObstacle three times, and if an enemy should be slowed down by obstacle1 or obstacle2 , it doesn't matter because enemy.s will be reset by obstacle3.drawObstacle draw 调用了三次obstacle3.drawObstacle ,如果一个敌人应该被obstacle.drawObstacle 1 或obstacle1 2 减速,没关系,因为敌人会被obstacle2 enemy.s重置

I highly recommend that each of your character and enemy class have a draw and a move method, and put everything about how enemies move within the enemy class.我强烈建议你的每个角色和敌人 class 都有一个drawmove方法,并将所有关于敌人如何移动的信息都放在敌人 class 中。 Maybe the obstacle class should have a draw and maybe a isCollidingWith method that enemy.move would call.也许障碍 class 应该有一个draw ,也许是一个敌人enemy.move会调用的isCollidingWith方法。

Keeping all your logic for an class within the class means that you can do variants much more easily.将 class 的所有逻辑保留在 class 中意味着您可以更轻松地进行变体。 For example, maybe later you'll want to have an enemy that's much slower but isn't hindered by obstacles at all.例如,也许稍后你会想要一个速度慢得多但完全不受障碍物阻碍的敌人。 Having all the logic within the class makes that easy.拥有 class 中的所有逻辑使这变得容易。 Putting the logic within the obstacle class would quickly become a nightmare if you have enough different types of enemies.如果你有足够多不同类型的敌人,将逻辑置于障碍 class 将很快成为一场噩梦。

Also, you could have a game object that holds everything about the game including character , enemies , and obstacles properties and draw and move methods that call the respective methods on character, enemy, and obstacle objects, and a destroy or quit method that clears its properties and such.此外,您可以拥有一个game object 包含游戏的所有内容,包括characterenemiesobstacles属性以及调用角色、敌人和障碍物对象的相应方法的drawmove方法,以及清除其对象的destroyquit方法属性之类的。 (In some languages this is important, because if you were to add a game property to each piece in the game, they'd have handles to each other and since (in some languages) garbage collection only reaps objects that have no references to them, they'll take up memory much longer than you want.) (在某些语言中,这很重要,因为如果您要为游戏中的每个部分添加game属性,它们将具有彼此的句柄,并且因为(在某些语言中)垃圾收集只获取没有引用它们的对象,它们占用 memory 的时间会比你想要的长得多。)

暂无
暂无

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

相关问题 此jQuery代码无法正常工作。 初学者在这里。 - This jQuery code won't work. Beginner Here. 初学者在这里。 我需要帮助更好地理解这个回调函数 - Beginner here. I need help understanding this callback function better 代码以错误的顺序执行并且它不是异步的(我认为......) - Code executing in wrong order and it isn't async (I think…) 我的代码有问题吗? 我认为一切都很好 - Is there any problem with my code? I think there's all good typeof 代码我觉得有些奇怪 - typeof code i think there is something weird JavaScript初学者,如果变量不匹配以执行某项操作 - JavaScript beginner, if a variable does not match to do something 有人可以解释为什么我的模式不起作用。 我认为这与我的 populateEpisodes function 有关,因为 getEpisodes 数组返回正常 - Can someone explain why my modal isn't working. I think it has to do with my populateEpisodes function because the getEpisodes array is returned fine 我在函数外提到的变量“x”有一些问题,我猜。 函数没有访问变量不知道为什么 - Variable "x" which I mention outside the function has some problem I guess . Function is not accessing the variable don't know why 这里是node.js的新功能。 如何正确地从subreddit的顶部获取JSON? - New to node.js here. How do I properly get the JSON from the top of a subreddit? 在JavaScript代码中的if块之外访问变量的更改值时出现问题 - Problem in accessing a variable's changed value outside of if block in javascript code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM