【问题标题】:drag and drop is not working properly processing.js拖放无法正常工作 processing.js
【发布时间】:2016-06-12 15:59:26
【问题描述】:

我想要做的是当鼠标按下鼠标位置但它不起作用时,让白色方块在画布上移动。我知道我遗漏了一些东西,请您帮助我。这是我的代码:

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

}

课程在这里:

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

【问题讨论】:

    标签: javascript processing.js


    【解决方案1】:

    以后,请告诉我们您的代码究竟做了什么,以及当您说它不工作时的确切含义。

    但是让我们运行一下您的代码并弄清楚发生了什么。

    首先,将您的班级命名为Object 是一个非常糟糕的主意。它可能实际上不会伤害任何东西,尤其是使用 Processing.js,但比抱歉更安全。所以我要把它重命名为Rectangle

    在那之后,您的主要问题来自于您有 两组xy 坐标。第一个来自您的循环:

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

    您使用第一组坐标来绘制矩形。但是你在 Rectangle 类中有第二组坐标:

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

    您在拖动逻辑中使用了第二组,但您从不使用它们来绘制矩形。更一般地说,您似乎根本没有使用过show() 函数。您希望该矩形出现在哪里?

    此外,您只能实例化一个 Rectangle 实例。您在 for 循环中绘制的矩形与您创建的 Rectangle 没有任何关系。

    因此,要解决您的问题,您需要做一些事情。

    第 1 步:为要在屏幕上绘制的每个矩形创建一个 Rectangle 实例。换句话说,您需要创建一个 ArrayList 来保存 9 个 Rectangle 实例,而不是一个。

    把它放在你的草图的顶部:

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

    你永远不会使用你的 back 变量,所以你可以摆脱它。

    把它放在你的 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);
        }
      }
    

    此代码循环遍历坐标并在该位置创建Rectangle 的实例,然后将其添加到ArrayList。您还必须将参数添加到 Rectangle 构造函数。

    第 2 步:然后您可以缩短您的 draw() 函数以简单地遍历 ArrayList 中的 Rectangle 实例并绘制它们:

    void draw() {
    
      background(100);
    
      for (Rectangle r : rectangles) {
        r.show();
      }
    }
    

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

    看看每个Rectangle 如何知道如何根据其位置以及鼠标是否在其中绘制自己。我们所有的逻辑都在这个类中,而不是被分成两个地方。

    第 4 步:然后您可以在 if 语句中添加拖动逻辑。如果光标在Rectangle 内并且正在按下鼠标,那么您知道用户正在拖动Rectangle

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

    请注意,我是在常规处理中执行此操作的,而不是 Processing.js,因此您可能需要进行一些小的调整,例如使用 mouseIsPressed 而不是 mousePressed。但基本步骤是相同的​​:您需要将您的逻辑移动到您的 Rectangle 类中,然后您需要使用该类中的变量来绘制每个矩形。

    如果您在某个特定步骤上遇到困难,请在更新后的代码中发布另一个问题,并准确描述您希望代码做什么、它会做什么以及这两件事有何不同。祝你好运。

    【讨论】:

      【解决方案2】:

      你可以在这里找到答案:https://processing.org/examples/mousefunctions.html

      但我会记住你不能在对象中使用鼠标事件。 mouse-click-on-object

      【讨论】:

        猜你喜欢
        • 2021-10-31
        • 2013-05-28
        • 1970-01-01
        • 2019-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-20
        • 1970-01-01
        相关资源
        最近更新 更多