【问题标题】:Processing | My class isn't calling the other class加工 |我的班级没有打电话给其他班级
【发布时间】:2019-03-13 12:46:00
【问题描述】:
void setup(){} 

老实说,我不知道这个顶部是做什么的,最初我的类“东西”甚至没有出现,因为它一直说它不存在,所以有人做了那个 void setup 的东西并且它有效?我是新手,所以如果我触发了某人,我深表歉意。

class TarotGame1
{
  Stuff game = new Stuff();
}

我照常上课

class Stuff
{

Stuff 类的构造函数

  Stuff()
  {
     tarotCircle(); 
     tarotCircle();
     tarotSquare();
     tarotDiamond();
     tarotPacMan();
     rngSelect1();
     rngSelect2();
     rngSelect3();
     rngSelect4();
  }

  //Tarot for Circle
  void tarotCircle()
  {
    size(800,600);
    rectMode(CENTER);

    fill(#2c3e50);
    rect(width / 2, height / 2, 300, 400, 60);
    noFill();

    fill(#16a085);
    rect(width / 2, height / 2, 250, 350, 60);
    noFill();

    fill(#c0392b);
    ellipse(width /2, 250, 100, 100);
    noFill();


    textSize(32);
    fill(#c0392b);
    text("Circle", 360, 350); 
    noFill();
  }

  //Tarot for Square

  void tarotSquare()
  {
    rectMode(CENTER);

    fill(#2c3e50);
    rect(width / 2, height / 2, 300, 400, 60);
    noFill();

    fill(#16a085);
    rect(width / 2, height / 2, 250, 350, 60);
    noFill();

    fill(#c0392b);
    rect(width / 2, 225, 100, 100);
    noFill();


    textSize(32);
    fill(#c0392b);
    text("Rectangle", 330, 350); 
    noFill();
  }

  //Tarot for Diamond

  void tarotDiamond()
  {
    rectMode(CENTER);

    fill(#8e44ad);
    rect(width / 2, height / 2, 300, 400, 60);
    noFill();

    fill(#16a085);
    rect(width / 2, height / 2, 250, 350, 60);
    noFill();

    fill(#27ae60);
    beginShape(TRIANGLE_FAN);
    vertex(57.5 + 340, 50+ 200);
    vertex(57.5+340, 15+ 200); 
    vertex(92+ 340, 50+ 200); 
    vertex(57.5+ 340, 85+ 200); 
    vertex(22+ 340, 50+ 200); 
    vertex(57.5+ 340, 15+ 200); 
    endShape();
    noFill();

    textSize(32);
    fill(#c0392b);
    text("Diamond", 330, 350); 
    noFill();
  }

  //Tarot for Pac-Man

  void tarotPacMan()
  {
    rectMode(CENTER);

    fill(#2c3e50);
    rect(width / 2, height / 2, 300, 400, 60);
    noFill();

    fill(#16a085);
    rect(width / 2, height / 2, 250, 350, 60);
    noFill();

    fill(#f1c40f);
    arc(width / 2, 250, 80, 80, 0, PI+2.5, PIE);
    noFill();

    textSize(32);
    fill(#c0392b);
    text("Pac-Man", 330, 350); 
    noFill();
  }

  //Randomized Selection #1 (Barely Randomized Values)

  void rngSelect1()
  {
    rectMode(CENTER);

    fill(#c0392b);
    size(800,600); 

    fill(#f1c40f);
    arc(500, 200, 80, 80, 0, PI+2.5, PIE);
    noFill();

    fill(#27ae60);
    beginShape(TRIANGLE_FAN);
    vertex(57.5 + 340+ 150, 50+ 200);
    vertex(57.5+340, 15+ 200); 
    vertex(92+ 340+ 150, 50+ 200); 
    vertex(57.5+ 340, 85+ 200); 
    vertex(22+ 340, 50+ 200); 
    vertex(57.5+ 340+ 150, 15+ 200); 
    endShape();
    noFill();

    fill(#8e44ad  );
    rect(width / 2, 225, 100, 100);
    noFill();

    fill(#c0392b);
    ellipse(width /2, 150, 100, 100);
    noFill();

    textSize(32);
    fill(#c0392b);
    text("Randomized Selection", 330, 500); 
    noFill();
  }

  //Randomized Selection #2 (Somewhat Randomized Values)

  void rngSelect2()
  {
    rectMode(CENTER);

    fill(#c0392b);
    size(800,600); 

    fill(#f1c40f);
    arc(500, 124, 80, 80, 0, PI+2.5, PIE);
    noFill();

    fill(#27ae60);
    beginShape(TRIANGLE_FAN);
    vertex(57.5 + 340+ 75, 50+ 200);
    vertex(57.5+340, 35+ 200); 
    vertex(92+ 340+ 57, 50+ 203); 
    vertex(57.5+ 56, 85+ 53); 
    vertex(22+ 57, 50+ 24); 
    vertex(57.5+ 75+ 150, 15+ 200); 
    endShape();
    noFill();

    fill(#8e44ad  );
    rect(width / 2, 225, 100, 100);
    noFill();

    fill(#c0392b);
    ellipse(width /2, 143, 100, 100);
    noFill();

    textSize(32);
    fill(#c0392b);
    text("Randomized Selection", 330, 500); 
    noFill();
  }

  //Randomized Selection #3 (More Randomized Values)

  void rngSelect3()
  {
    rectMode(CENTER);

    fill(#c0392b);
    size(800,600); 

    fill(#f1c40f);
    arc(500, 300, 80, 80, 0, PI+2.5, PIE);
    noFill();

    fill(#27ae60);
    beginShape(TRIANGLE_FAN);
    vertex(57.5 + 340+ 75, 50+ 23);
    vertex(57.5+340, 35+ 127); 
    vertex(92+ 340+ 57, 50+ 721); 
    vertex(57.5+ 56, 85+ 126); 
    vertex(22+ 57, 50+ 234); 
    vertex(57.5+ 75+ 150, 15+ 34); 
    endShape();
    noFill();

    fill(#8e44ad  );
    rect(111, 341, 100, 100);
    noFill();

    fill(#c0392b);
    ellipse(231, 180, 100, 100);
    noFill();

    textSize(32);
    fill(#c0392b);
    text("Randomized Selection", 330, 500); 
    noFill();
  }

  //Randomized Selection #4 (Super Randomized Values)

  void rngSelect4()
  {
    rectMode(CENTER);

    fill(#c0392b);
    size(800,600); 

    fill(#f1c40f);
    arc(500, 300, 132, 221, 0, PI+2.5, PIE);
    noFill();

    fill(#27ae60);
    beginShape(TRIANGLE_FAN);
    vertex(57.5 + 12+ 75, 50+ 23);
    vertex(57.5+340, 351+ 127); 
    vertex(92+ 340+ 577, 50+ 12); 
    vertex(57.5+ 56, 85+ 136); 
    vertex(22+ 57, 50+ 234); 
    vertex(57.5+ 75+ 150, 15+ 13); 
    endShape();
    noFill();

    fill(#8e44ad  );
    rect(123, 461, 111, 425);
    noFill();

    fill(#c0392b);
    ellipse(231, 180, 123, 560);
    noFill();

    textSize(32);
    fill(#c0392b);
    text("Randomized Selection", 310, 550); 
    noFill();
  }

}

有什么想法吗?如果你把它放在处理中,我只会得到一个空白屏幕。

【问题讨论】:

  • 有一些“标准”方法(@​​987654325@、setup() 等)在应用程序的生命周期中的特定时间被调用 - 我鼓励您查看它们的内容是以及它们何时运行。
  • 在类似的注释中,我看不到您在哪里进行方法调用以使您的程序真正任何事情。你的setup() 方法没有做任何事情,我看不到你在哪里调用draw() 或类似的东西。正如我上面所说,您真的,真的需要了解这些方法的作用才能成功编写处理程序。
  • 啊,好吧!谢谢你的快速回复我的朋友:D
  • @shmosel 我看不出将这个标记为 Java 有什么害处——毕竟,它或多或少是 Java 编程语言的一个子集。

标签: processing


【解决方案1】:

默认情况下,Processing 不“知道”调用你的类方法。

就像在“标准”Java 中一样,某些方法具有“神奇”的名称。在 Java 中,该方法当然是 main 方法,按照惯例,它是应用程序的入口点。

在Processing中,其实有几种类似的方法。您提到的 setup 方法在程序开始时被调用一次。你可以把它想象成main 方法和类构造函数的交叉——你用它来做基本的设置工作(例如屏幕大小、你将在程序运行期间使用的实例变量等.).

setupmain 的区别当然是你不需要显式调用其他生命周期方法来让你的程序工作)。

另一个重要的方法是void draw()。环境每秒会自动调用几次。这是您调用大部分逻辑来渲染屏幕的地方;特别是,您预计会定期更改的任何内容都应该放在此处。

(但请注意:注意您在 draw 方法中所做的工作量,因为它被如此频繁地调用 - 否则您的 UI 将非常“滞后”并且似乎挂起)。

还有其他像这样的“魔术名称”方法来获取键盘输入、获取鼠标位置和点击等,但void setup()void draw() 是最重要的。

TL;DR您应该调用逻辑以在draw() 方法中呈现您的屏幕,以便处理“知道”该类的用途。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2012-05-17
    • 2019-04-26
    • 1970-01-01
    • 2018-05-20
    • 2020-05-23
    相关资源
    最近更新 更多