首页 > 解决方案 > 加工 | 我的班级没有打电话给其他班级

问题描述

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

}

任何想法这里的问题是什么?如果你把它放在处理中,我只会得到一个空白屏幕。

标签: processing

解决方案


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

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

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

setup(当然,和之间的区别在于main您不需要显式调用其他生命周期方法来使您的程序工作)。

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

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

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

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


推荐阅读