1
我遇到了一個問題,將Daniel Shiffman的真棒拖入example到我的草圖中。我以前使用它,它是偉大的,但我試圖用一些「幻想」循環應用到多個對象(在這種情況下,文本),但無濟於事。一切正常工作除了對象不應該拖動時,他們應該。從邏輯上講,這是因爲Line
類中的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;
}
這裏是行類:
class Lines{
//class properties
float[] x; //array holding random values to be added to x for placement
float[] y; //array holding random values to be added to y for placement
float offsetX;
float offsetY;
String[] doc;
boolean dragging = false; //boolean for dragging
//construct
Lines(String[] tempDoc){
doc = tempDoc;
}
//fills x and y arrays
void populateArrays(){
x = new float[doc.length];
y = new float[doc.length];
//populates x and y arrays
for(int i = 0; i <= doc.length-1; i++){
x[i] = int(random(0-boundaryOverlap, width-boundaryOverlap));
y[i] = int(random(0, height-boundaryOverlap));
}
}
//draws text
void display(int i){
text(doc[i], x[i], y[i]); //draw text
//if(addition[i] != null) text(addition[i], x[i], y[i]+20);
}
void clicked(int i){
if(mouseX > x[i] &&
mouseX < x[i]+textWidth(doc[i]) &&
mouseY < y[i] &&
mouseY > y[i]-tSize){
dragging = true;
offsetX = x[i] - mouseX;
offsetY = y[i] - mouseY;
}
}
//updates text positions
void move(int i, boolean clicked){
//if mouseOver text hover gray
if(mouseX > x[i] &&
mouseX < x[i]+textWidth(doc[i]) &&
mouseY < y[i] &&
mouseY > y[i]-tSize){
fill(100); //gray text fill
if(dragging){
x[i] = mouseX + offsetX;
y[i] = mouseY + offsetY;
}
}
else{
fill(0); //if not text not mouseOver fill is black
dragging = false;
}
}
//delete
void delete(int i){
//if "delete" is pressed
if (keyPressed){
if(key == 8){
doc[i] = ""; // doc[line String that is being hovered over] is replaced with null
keyCode = 1;
}
}
}
}
我真的很感謝你的回答。你的代碼比我的代碼更乾淨,更高效。也就是說,我一直致力於擴展項目以添加功能(並最終將邏輯整合到文字遊戲中),並且無法解釋某些代碼。我不熟悉你正在使用的一些語法,如帶有四個參數的for循環(int),(Line l:lines)和其他各種東西。我很抱歉缺乏編程經驗,並想知道是否有辦法儘可能多地使用我的(壞)代碼來解決我的問題? – Brannon 2013-03-12 22:14:14
這是標準的java /處理語法,所以我可以肯定地解釋你不理解的部分。從Java 1.5開始(很久以前=現在= QU),有兩種方法可以循環。第一個是for(int i = 0; i
2013-03-13 12:28:26