For loop 在处理过程中拖动对象

For loop 在处理过程中拖动对象,for-loop,draggable,drag,processing,offset,For Loop,Draggable,Drag,Processing,Offset,在我的素描中加入丹尼尔·希夫曼(Daniel Shiffman)的绝妙表现时,我遇到了一个问题。我以前使用过它,它非常棒,但是我尝试使用它进行循环,将其应用于多个对象(在本例中为文本),但没有任何效果。除了应该拖动的对象不拖动外,所有操作都正常。从逻辑上讲,这是因为行类中的offsetX和offsetY属性继续更新,从而强制对象保持静止。我确信有解决办法,但我想不出来。也许我盯着它看太久了。我真的很感谢你的帮助 String[] doc; //array of Strings from each

在我的素描中加入丹尼尔·希夫曼(Daniel Shiffman)的绝妙表现时,我遇到了一个问题。我以前使用过它,它非常棒,但是我尝试使用它进行循环,将其应用于多个对象(在本例中为文本),但没有任何效果。除了应该拖动的对象不拖动外,所有操作都正常。从逻辑上讲,这是因为
类中的offsetX和offsetY属性继续更新,从而强制对象保持静止。我确信有解决办法,但我想不出来。也许我盯着它看太久了。我真的很感谢你的帮助

String[] doc; //array of Strings from each text documents line
int tSize; //text size
float easing; //easing
int boundaryOverlap; //value for words to overlap edges by
PFont font; //font
Lines lines;
boolean clicked = false;

void setup(){
  size(displayWidth/2,displayHeight); 
  background(255);
  fill(0);

  boundaryOverlap = 20; //value for words to overlap edges by
  tSize = 32; //text size

  //loads and formats text
  doc = loadStrings("text.txt"); 
  font = loadFont("Times-Roman-48.vlw");
  textFont(font, tSize);

  //lines object
  lines = new Lines(doc);

  //populate xAddition and yAddition arrays
  lines.populateArrays();
}

void draw(){
  background(255);
  fill(0);

  //loops through each line in .txt
  for(int i = 0; i <= doc.length-1; i++){
    if(clicked) lines.clicked(i);
    lines.move(i, clicked); //update doc[i] positions //deletes 
    lines.display(i); //draws text for each line of text in text.txt
   }
}

void mousePressed(){
   clicked = true;
}

void mouseReleased(){
   clicked = false; 
   lines.dragging = false;
}
String[]单据//来自每个文本文档行的字符串数组
int tSize//文本大小
浮动宽松//缓和
内边界重叠//要重叠边的单词的值
字体//字体
线条;
布尔值=假;
无效设置(){
尺寸(显示宽度/2,显示高度);
背景(255);
填充(0);
boundaryOverlap=20;//用于重叠边的单词的值
tSize=32;//文本大小
//加载和格式化文本
doc=loadStrings(“text.txt”);
font=loadFont(“Times-Roman-48.vlw”);
textFont(字体,tSize);
//线条对象
行=新行(单据);
//填充xAddition和yAddition数组
line.populatearray();
}
作废提款(){
背景(255);
填充(0);
//循环遍历.txt中的每一行
对于(int i=0;i y[i]-tSize){
拖动=真;
offsetX=x[i]-mouseX;
offsetY=y[i]-mouseY;
}
}
//更新文本位置
无效移动(整数i,单击布尔值){
//如果鼠标悬停文本为灰色
if(mouseX>x[i]&&
mouseXy[i]-t大小){
填充(100);//灰色文本填充
如果(拖动){
x[i]=鼠标+偏移量x;
y[i]=鼠标+偏移;
}
}
否则{
填充(0);//如果不是文本,则填充为黑色
拖动=假;
}
}
//删除
作废删除(int i){
//如果按“删除”
如果(按键){
如果(键==8){
doc[i]=“”;//doc[悬停在上面的行字符串]替换为null
keyCode=1;
}
}
}
}

首先,不要使用vlw字体。当每个人都安装了Times New Roman时,没有理由使用位图图像文件。只需使用createFont(“Times New Roman”,48)。现在,您甚至可以更改文本大小,而不会让它看起来很糟糕,因为您只加载了字体,而不是位图图像

也就是说,这段代码并不是很好。。。但使用它时,问题在于:

  for(int i = 0; i <= doc.length-1; i++){
    if(clicked) lines.clicked(i);
    lines.move(i, clicked); //update doc[i] positions //deletes 
    lines.display(i); //draws text for each line of text in text.txt
  }

for(int i=0;我真的很感谢你的回答。你的代码比我的更干净、更高效。也就是说,我一直在努力扩展项目以添加功能(并最终将逻辑融入文字游戏)我在解释一些代码时遇到了困难。我不熟悉您正在使用的一些语法,例如带有四个参数的for循环(int)、for(第l行:行)和其他各种东西。我为我缺乏编程经验而道歉,我想知道是否有办法使用我的(坏的)代码来解决我的问题尽可能地编写代码?这是标准的java/处理语法,因此我当然可以解释您不理解的部分。对于java 1.5(这是很久以前的事了,现在=D),有两种方法可以循环。第一种是for(int I=0;I
  for(int i = 0; i <= doc.length-1; i++){
    if(clicked) lines.clicked(i);
    lines.move(i, clicked); //update doc[i] positions //deletes 
    lines.display(i); //draws text for each line of text in text.txt
  }
LineCollection lines;
float textSize;

void setup(){
  size(400,400);
  // fix the text size, reference a real font
  textSize = 32; 
  textFont(createFont("Times New Roman", textSize));
  // parse strings, construct Lines container
  String[] textValues = new String[]{"lol","cat"};
  lines = new LineCollection(textValues);
  // Do not loop! only update when events warrant,
  // based on redraw() calls  
  noLoop();
}
// fall through drawing
void draw() { background(255); lines.draw(); }
// fall through event handling
void mouseMoved() { lines.mouseMoved(mouseX,mouseY); redraw(); }
void mousePressed() { lines.mousePressed(mouseX,mouseY); redraw(); }
void mouseDragged() { lines.mouseDragged(mouseX,mouseY); redraw(); }
void mouseReleased() { lines.mouseReleased(mouseX,mouseY); redraw(); }


/**
 * A collection of lines. This is *only* a collecton,
 * it is simply responsible for passing along events.
 */
class LineCollection {
  Line[] lines;
  int boundaryOverlap = 20;

  // construct
  LineCollection(String[] strings){
    lines = new Line[strings.length];
    int x, y;
    for(int i=0, last=strings.length; i<last; i++) {
      x = (int) random(0, width);
      y = (int) random(0, height);
      lines[i] = new Line(strings[i], x, y);   
    }
  }

  // fall through drawing   
  void draw() {

    // since we don't care about counting elements
    // in our "lines" container, we use the "foreach"
    // version of the for loop. This is identical to
    // "for(int i=0; i<lines.size(); i++) {
    //    Line l = lines[i];
    //    [... use l here ...]
    //  }"
    // except we don't have to unpack our list manually.

    for(Line l: lines) { l.draw(); }
  }

  // fall through event handling
  void mouseMoved(int mx, int my) { for(Line l: lines) { l.mouseMoved(mx,my); }} 
  void mousePressed(int mx, int my) { for(Line l: lines) { l.mousePressed(mx,my); }} 
  void mouseDragged(int mx, int my) { for(Line l: lines) { l.mouseDragged(mx,my); }}
  void mouseReleased(int mx, int my) { for(Line l: lines) { l.mouseReleased(mx,my); }}
}

/**
 * Individual lines
 */
class Line {
  String s;
  float x, y, w, h;
  boolean active;
  color fillColor = 0;
  int cx, cy, ox=0, oy=0;

  public Line(String _s, int _x, int _y) {
    s = _s;
    x = _x;
    y = _y;
    w = textWidth(s);
    h = textSize;
  }

  void draw() {
    fill(fillColor);
    text(s,ox+x,oy+y+h);
  }

  boolean over(int mx, int my) {
    return (x <= mx && mx <= x+w && y <= my && my <= y+h);
  }

  // Mouse moved: is the cursor over this line?
  // if so, change the fill color
  void mouseMoved(int mx, int my) {
    active = over(mx,my);
    fillColor = (active ? color(155,155,0) : 0);
  }

  // Mouse pressed: are we active? then
  // mark where we started clicking, so 
  // we can do offset computation on
  // mouse dragging.
  void mousePressed(int mx, int my) {
    if(active) {
      cx = mx;
      cy = my;
      ox = 0;
      oy = 0; 
    }
  }

  // Mouse click-dragged: if we're active,
  // change the draw offset, based on the
  // distance between where we initially
  // clicked, and where the mouse is now.
  void mouseDragged(int mx, int my) {
    if(active) {
      ox = mx-cx;
      oy = my-cy;
    }
  }

  // Mouse released: if we're active,
  // commit the offset to this line's
  // position. Also, regardless of
  // whether we're active, now we're not.  
  void mouseReleased(int mx, int my) {
    if(active) {
      x += mx-cx;
      y += my-cy;
      ox = 0;
      oy = 0;
    }
    active = false;
  }
}