简体   繁体   English

拖放无法正常运行processing.js

[英]drag and drop is not working properly processing.js

What I am trying to do is to make the white square to move around the canvas when the mouse is pressed with the mouse locations but it is not working. 我要做的是,当用鼠标位置按下鼠标但不起作用时,使白色方块在画布上移动。 I know that I am missing something and ask you to help me. 我知道我缺少一些东西,请您帮助我。 Here is my code: 这是我的代码:

Object o;

int[][] back =new int[3][3];
int pad = 10, bs=100;            //len=pad*(back.length+1)+bs*back.length; za dinamichno resaizvane na ekrana
boolean drag = false;


void setup() {
  size(400, 400);
  noStroke();
  o = new Object();
}

void draw() {

  rectt(0, 0, width, height, color(100));

  for (int row=0; row<back.length; row++)
    for (int coll=0; coll<back[row].length; coll++) {
      float x = pad+(pad+bs)*coll;
      float y = pad+(pad+bs)*row;

      rectt(x, y, bs, bs, color(150));
      if (mouseX >=x && mouseX<=x+width/x*coll+bs
        && mouseY>=y && mouseY<=y+height/y*row+bs) {
        rectt(x, y, bs, bs, color(255, 0, 0));
      }
    }
   o.show();
   //o.over();
}



void rectt(float x, float y, float w, float h, color c) {
  fill(c);
  rect(x, y, w, h);
}


void mousePressed() {
  o.drag();

}

and the class is here: 班级在这里:

class Object {
  float size = 50;
  float x;
  float y;
  //  boolean d = false;
  Object() {
    x = width -60;
    y = height -60;
  }

  void show() {
    fill(255);
    rect(x, y, size, size);
  }


  void drag() {
    if ( mouseX >= x && mouseX <= x+size && mouseY <= y+size && mouseY >= y && mousePressed ) {
      x = mouseX;
      y = mouseY;
    }
  }
}

In the future, please tell us exactly what your code does, and exactly what you mean when you say it isn't working. 将来,请准确告诉我们您的代码做什么,以及当您说它不起作用时的确切含义。

But let's run through your code and figure out what's going on. 但是,让我们遍历您的代码并弄清楚发生了什么。

First off, it's a pretty bad idea to name your class Object . 首先,给您的类Object命名是一个非常糟糕的主意。 It probably won't actually hurt anything, especially using Processing.js, but better safe than sorry. 它实际上可能不会造成任何伤害,尤其是使用Processing.js时,但比后悔要安全得多。 So I'm going to rename it to Rectangle . 因此,我将其重命名为Rectangle

After that, your main problem comes from the fact that you have two sets of x and y coordinates. 在这之后,你的主要问题来自于事实,你有两套 xy坐标。 The first come from your loop: 第一个来自您的循环:

float x = pad+(pad+bs)*coll;
float y = pad+(pad+bs)*row;

You use this first set of coordinates to draw your rectangles. 您可以使用第一组坐标来绘制矩形。 But then you have a second set of coordinates inside your Rectangle class: 但是,然后在Rectangle类中有了第二组坐标:

x = width -60;
y = height -60;

You use this second set in your dragging logic, but then you never use them for drawing your rectangles. 您在拖动逻辑中使用了第二个集合,但随后再也不会使用它们来绘制矩形。 More generally, you never seem to use the show() function at all. 更一般而言,您似乎根本没有使用过show()函数。 Where do you expect that rectangle to show up? 您希望该矩形显示在哪里?

Also, you only ever instantiate one Rectangle instance. 另外,您只能实例化一个Rectangle实例。 The rectangles you're drawing in the for loop don't have anything to do with the Rectangle that you've created. 您在for循环中绘制的Rectangle与您创建的Rectangle没有任何关系。

So to fix your problems, you need to do a few things. 因此,要解决问题,您需要做一些事情。

Step 1: Create one instance of Rectangle for each rectangle you want to draw to the screen. 步骤1:为要绘制到屏幕上的每个矩形创建一个Rectangle实例。 In other words, you need to create an ArrayList that holds 9 Rectangle instances, not one. 换句话说,您需要创建一个包含9个Rectangle实例而不是一个的ArrayList

Put this at the top of your sketch: 将其放在草图的顶部:

ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();

You never use your back variable, so you can get rid of it. 您永远不会使用back变量,因此可以摆脱它。

Put this inside your setup() function: 将其放入您的setup()函数中:

  for (int row=0; row<back.length; row++) {
    for (int coll=0; coll<back[row].length; coll++) {
      float x = pad+(pad+bs)*coll;
      float y = pad+(pad+bs)*row;

      Rectangle rectangle = new Rectangle(x, y);
      rectangles.add(rectangle);
    }
  }

This code loops through the coordinates and creates an instance of Rectangle at that position, and then adds it to the ArrayList . 此代码循环遍历坐标,并在该位置创建Rectangle的实例,然后将其添加到ArrayList You'll also have to add the parameters to the Rectangle constructor. 您还必须将参数添加到Rectangle构造函数中。

Step 2: You can then shorten your draw() function to simply loop over the Rectangle instances in the ArrayList and draw them: 步骤2:然后,您可以缩短您的draw()函数,以简单地遍历ArrayListRectangle实例并绘制它们:

void draw() {

  background(100);

  for (Rectangle r : rectangles) {
    r.show();
  }
}

Step 3: Modify your show() function to include your logic for coloring the Rectangle based on the mouse position: 步骤3:修改show()函数,使其包含根据鼠标位置为Rectangle着色的逻辑:

  void show() {
    if (mouseX >=x && mouseX<=x+size && mouseY>=y && mouseY<=y+size) {
      //mouse is inside this Rectangle
      rectt(x, y, size, size, color(255, 0, 0));
    } else {
      rectt(x, y, size, size, color(150));
    }
  }

See how each Rectangle knows how to draw itself based on its position and whether the mouse is inside it. 了解每个Rectangle如何根据其位置以及鼠标是否在其中来绘制自身。 All of our logic is inside this class instead of being split into two places. 我们所有的逻辑都在此类内,而不是分成两个地方。

Step 4: You can then add the dragging logic inside that if statement. 步骤4:然后可以在if语句中添加拖动逻辑。 If the cursor is inside the Rectangle and the mouse is being pressed, then you know the user is dragging that Rectangle : 如果光标在Rectangle并且按下了鼠标,则您知道用户正在拖动Rectangle

//mouse is inside this Rectangle
if (mousePressed) {
    x = mouseX - size/2;
    y = mouseY - size/2;
}

Please note that I did this in regular Processing, not Processing.js, so you might have to make a few small adjustments like using mouseIsPressed instead of mousePressed . 请注意,我是在常规Processing(而不是Processing.js)中执行此操作的,因此您可能必须进行一些小的调整,例如使用mouseIsPressed而不是mousePressed But the basic steps are the same: you need to move your logic inside your Rectangle class, and then you need to use the variables inside that class to draw each rectangle. 但是基本步骤是相同的​​:您需要在Rectangle类内移动逻辑,然后需要使用该类内的变量来绘制每个矩形。

If you get stuck on a specific step then please post another question with your updated code and a description of exactly what you expect your code to do, what it does instead, and how those two things are different. 如果您被困在特定的步骤上,那么请发布另一个问题,其中包含更新的代码以及对您期望代码执行的操作,执行的操作以及这两项有何不同的描述。 Good luck. 祝好运。

You can find the answered in here: https://processing.org/examples/mousefunctions.html 您可以在以下位置找到答案: https : //processing.org/examples/mousefunctions.html

But I will remember you that you can't use mouse event in the Object. 但我会记住您,您不能在对象中使用鼠标事件。 mouse-click-on-object 鼠标单击对象

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM