首页 > 解决方案 > 如何使对象出现在鼠标按下并停留在屏幕上?

问题描述

(Java 新手)。所以我创建了以下脚本,我希望我的名为“站点”的对象仅在按下鼠标时出现,然后停留在屏幕上。然后在每次按下鼠标时,它都会创建一个新的“站点”。

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

有没有人可以帮助我,那就太好了!

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

谢谢 :)

标签: java

解决方案


摆脱这一行:

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

setup()您函数中的这一行添加了一个Site. 如果您不想Site在启动时添加,请删除此行。

您还应该尝试完成调试问题的过程。例如,使用一张纸和一支铅笔逐行执行代码,或者添加打印语句,或者使用处理编辑器附带的调试器。这将帮助您在未来解决此类问题。祝你好运!


推荐阅读