2016-01-10 181 views
1

所以這是我的總代碼,所以我從flickr中獲取圖像將其存儲在數組中然後在繪製循環這些圖像。然後我想讓它們動畫到100,100點。看到我的評論更詳細的解釋。動畫圖像,但圖像不移動

int imageIndex; 
XML xml; 
String tag_mode = "all"; 
String words[]; 
PImage[] displayImages; 
int amount = 500; 
int counter = 0; 
PrintWriter output; 
int j = 0; 
int x =50; 
int y = 50; 
String labelNaam = "Medium"; 

void setup() { 
    size(500, 500); 
    String lines[] = loadStrings("text.txt"); 
    words = split(lines[0], " "); 
    displayImages = new PImage[amount]; 
    output = createWriter("positions.txt"); 


    for (int k = 0; k<words.length; k++) { 

    int randomX = int(random(2)); 
    if (randomX == 1) { 
     x = displayWidth; 
    } else { 
     x = -500; 
    } 
    int randomX2 = int(random(2, 4)); 
    if (randomX2 == 2) { 
     y = displayHeight; 
    } else { 
     y = -500; 
    } 

    String query = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=MY_API_KEY&tags="+ words[k] + "&sort=relevance&extras=url_o&tag_mode="+ tag_mode +"format=rest"; 
    xml = loadXML(query); 

    XML[] children = xml.getChildren("photos"); 
    if (children.length > 0) { 
     XML[] childPhoto = children[0].getChildren("photo"); 
    // println(childPhoto); 

     if (childPhoto.length > 0) { 
      for (int i = 0; i < 1; i++) { 
       String id = childPhoto[i].getString("id"); 
       String title = childPhoto[i].getString("title"); 
       String user = childPhoto[i].getString("owner"); 
       String url = "https://www.flickr.com/photos/"+user+"/"+id; 

       String query2 ="https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=MY_APIKEY&photo_id="+id+"&format=rest"; 
       xml= loadXML(query2); 
       XML[] children2 = xml.getChildren("sizes"); 

        if (children2.length > 0) { 
        XML[] childSize2 = children2[0].getChildren("size"); 
        if (childSize2.length >0) { 
         println(counter); 
         if (counter <= words.length) { 
           String labelNaam = "Medium"; 
           String label = childSize2[5].getString("label"); 
           String source = childSize2[5].getString("source"); 
           displayImages[counter] = loadImage(source, "jpg"); 
           counter++; 
           } 
          } 
         } 
        } 
      } 
     } 
    } 

    textAlign(CENTER, CENTER); 
    smooth(); 
} 

void draw() { 
    if (j > words.length-1) { 
     j = 0; 
    } 

    if (displayImages[j] != null) { 

    println(x + "|||||"+ y); 
    image(displayImages[j], x, y); 
    j++; 
    } 
    if (x < 100) { 
     x+=100; 
    } else { 
     x-=100; 
    } 

    if (y < 100) { 
    y+=100; 
    } else { 
    y-=100; 
    } 
    delay(1000); 
} 

回答

0

你設置圖像的每一次draw()窗口的邊界之外的位置被稱爲。然後,您將圖像移動10個像素。但是隨後你只是在下一次將圖像移回到窗外的位置,所以增量運動實際上並沒有做任何事情。

取而代之,您應該只在草圖開始處初始化圖像的位置一次

int x; 
int y; 
PImage image; 
int j =0; 
int amount = 500; 

void setup() { 
    size(500, 500); 
    image = loadImage("cat1.jpg"); 

    int randomX = int(random(2)); 
    if (randomX == 1) { 
    x = width + 500; 
    } else { 
    x = -500; 
    } 
    int randomX2 = int(random(2, 4)); 
    if (randomX2 == 2) { 
    y = height + 500; 
    } else { 
    y = -500; 
    } 
} 

void draw() { 

    background(255); 

    if (x < 100) { 
    x+=10; 
    } else { 
    x-=10; 
    } 
    if (y < 100) { 
    y+=10; 
    } else { 
    y-=10; 
    } 

    println(x + ", " + y); 

    image(image, x, y, 25, 25); 
} 
+0

謝謝你的回答!但我不希望所有圖像都有相同的起點。所以這就是爲什麼我把位置畫出來,所以每個圖像都有不同的起點。現在每個後續的圖像被放置在100,100點附近,但是我希望所有的圖像都移動到100,100點,而動畫不會使靜態接近100,100。我會怎麼做?我編輯了我原來的帖子,所以你可以看到我的完整代碼,那麼它可能會更清晰一些。 – FutureCake