如何使用mousePress显示对象并保留在屏幕上?

时间:2018-05-03 18:36:58

标签: java

(java新手)。所以我创建了以下脚本,我希望我的对象名为" site"仅在按下鼠标时出现然后停留在屏幕上。然后,每按一下鼠标就会创建一个新的"网站"。

现在它做的是它在开头创建一个站点,并在按下鼠标时添加一个站点。但我一开始并不想要那个网站,我只是想点击它。

有人能帮助我,那会很棒!

/*preload = "factory_12.png";*/
/*preload = "sign.png";*/
/*preload = "simple_truck.png";*/

Lorry lorry;

PImage concretePlant;
PFont aFont;
int xCoord;
int yCoord;
ArrayList<Site> sites;
int siteSize = 30;

void setup() // What is called once at the beginning
{
  size (500, 500);

  concretePlant = loadImage("factory_12.png");
  aFont = createFont("IndustrialRevolution-Regular", 12);
  textFont(aFont);

  xCoord = int(width/2);
  yCoord = int(height/2);

  //Creating empty Array List where store sites objects
  sites = new ArrayList<Site>();

  //Adding first site
  sites.add(new Site(random(width), random(height), siteSize));

  //storing lorries
  lorry = new Lorry(xCoord, yCoord);
}

void draw() // Draw the background and concrete plant
{
  background (235, 247, 255); //light blue background, not in draw as it wouldn't allow the site image to stay
  image(concretePlant, xCoord, yCoord, 60, 60);
  fill(1);
  text("Concrete Plant", xCoord-20, yCoord+70);

  //Calling the sites
  for (int i = sites.size () - 1; i>=0; i--) {
    Site site = sites.get(i);
    site.displaySites();
  }

  //calling the lorry functions
    lorry.updateLorry();
}

void mousePressed() {
  sites.add(new Site(mouseX, mouseY, siteSize));
}

class Site

{
  float x,y;
  float size;
  PImage picture;

  Site (float xin, float yin, float sin)
  {
    x = xin;
    y = yin;
    size = sin;
    picture = loadImage("sign.png");
  }

  void displaySites()
  {
    image(picture, x, y, 60, 60);
  }
}

class Lorry
{
  PVector location;
  PVector concretePlant;
  PVector velocity;
  PImage mixer;
  boolean changeDirection;
  int siteNumber = 0;
  Site destination;

  Lorry(float xCoord, float yCoord)
  {
    concretePlant = new PVector(xCoord, yCoord); //Initial start point
    location = new PVector(xCoord, yCoord); //Initial start point
    velocity = new PVector(2, 2);
    mixer = loadImage("simple_truck.png");
    destination = sites.get(siteNumber);
    changeDirection = false;
  }

  void displayLorry()
  {
    image(mixer, location.x, location.y, 30, 30);
  }

  void Move()
  {
    float xdir = destination.x - location.x;
    float ydir = destination.y - location.y;
    PVector dir = new PVector (xdir, ydir);
    dir.normalize();

    location.add(dir);
    print("going");
  }

  void reachDestination()
  {
    //println(destination,location);
    //if ((destination.x == location.x) && (destination.y == location.y)) {
    if (dist(destination.x, destination.y, location.x, location.y) < 1) {

      if (siteNumber <sites.size() -1) {
        siteNumber++; // siteNumber = siteNumber + 1;
        destination = sites.get(siteNumber);
        changeDirection = true;
      } else {
        println("reached final site");
      }
      println("reachedDestination");
    }
  }

  void updateLorry()
  {
    displayLorry();
    Move();
    reachDestination();
  }
}

谢谢:)

1 个答案:

答案 0 :(得分:0)

摆脱这条线:

//Adding first site
sites.add(new Site(random(width), random(height), siteSize));

setup()功能中的这一行会添加Site。如果您不想在启动时添加Site,请删除此行。

您还应该尝试完成debugging your problem的过程。例如,使用一张纸和一支铅笔逐行遍历代码,或添加打印语句,或使用Processing编辑器附带的调试器。这将有助于您在将来找出这样的问题。祝你好运!