我是Processing的新手,我试图通过组合一些内置示例来开始。我正在通过图片滚动来自Google新闻的RSS源。它似乎工作得很好但代码仅限于一次滚动一个标题。如果我想限制每个标题之间的空间怎么办?我想到了两种方法:首先是将headlines
转换成一个非常长的字符串。第二个是同时滚动两个headlines
。第二种方法的问题在于,如果我继续更大的图像或开始滚动较小的字符串(如股票报价),我需要有许多语句来滚动许多headlines
。有没有更好的方法,我没有想到?
/**
* Scrolling Rss Feeds
*
* Scroll headlines from Google News on top of an image
*/
PImage img; // Declare variable "a" of type PImage
float x; // horizontal location of headline
XML xml;
String[] headlines;
int index = 0;
void setup() {
size(640, 360);
x = width;
// The image file must be in the data folder of the current sketch
// to load successfully
img = loadImage("moonwalk.jpg"); // Load the image into the program
// The URL for the XML document
String url = "http://news.google.com/news?cf=all&hl=en&pz=1&ned=us&output=rss";
// Load the XML document
xml = loadXML(url);
// Grab the element we want
XML[] headers = xml.getChildren("channel/item/title");
headlines = new String[headers.length];
for (int i = 0; i < headers.length; i++) {
headlines[i] = headers[i].getContent();
}
}
void draw() {
// Displays the image at its actual size at point (0,0)
image(img, 0, 0);
textSize(32);
text(headlines[index], x, 90);
x = x - 3;
float w = textWidth(headlines[index]);
if (x < -w) {
x = width;
index = (index + 1) % headlines.length;
}
}