I had a very strange bug recently. I named a String array "articles[]". Then in setup I loaded a txt file into it
articles=loadStrings("articles.txt");
Then I tried to access it from setup and draw like this:
println(articles);
It worked fine. But when I called println inside a function that was inside a class I got "null".
Then I changed the name of "articles[]" array and everything worked fine. What could cause this bug?
*UPDATE*
Sorry, I found it. Looks like I had a String named "articles" in my class, so when I called println inside of a that class I got values of a local undefined array bu not values of a global one))
I'm coding a lyrics generator. I want the user to set structure of text that will be generated (for example: noun, verb, adverb...). I have String arrays (adjectives, verbs...) to load my word lists. I also have a two-dimensional array
speechParts [(an array of strings to be loaded)] [wrd (position of current word in the list)]
The problem is: I have a function in a class (called "activate") that is supposed to set speechParts[0] to "adjectives". But it doesn't work speechParts remains the same as I initialized it in setup().
I have a file with a list of numbers I need to add. I load it using loadStrings(), and then I want to convert each line of String array to an int. I wonder why I get a NullPointerExeption error?
Hi, guys! I'm making a game with an array of objects that are falling from the top of the screen and you need to catch it with your mouse. I've made a boolean paused and controlled it with a key press. Everything worked fine, but when I put a condition "if(!paused){//draw the rectangles", then paused and resumed the game: A big amount of overlapped rectangles started falling from the top of the screen. What should I do? Where to put the condition? Maybe there's something with the timer?
Here's a simplified version of that game without all the features and images:
Head [] en;
Head [] po;
boolean gamePause = false;
//Timer
int t = 1000;
float speed = 1;
void setup() {
en = new Head[500];
po = new Head[500];
size(700, 600);
//Enemies
for (int i = 0; i < en.length; i++) {
en[i] = new Head();
}
for (int i = 0; i < po.length; i++) {
po[i] = new Head();
}
}
void draw() {
background(0);
redd();
greenn();
player();
}
void redd() {
int i = 0;
for (t=0; millis() > t; t=t+1950) {
en[i].appear(2);
i++;
}
}
void greenn() {
int i = 0;
for (t=2000; millis() > t; t=t+2150) {
po[i].appear(1);
i++;
}
}
void player() {
rect(mouseX, mouseY, 56, 143);
}
void keyPressed() {
gamePause = !gamePause;
}
//Class
class Head {
float yloc = -50;
float xloc = random(100, 680);
float sel;
float switch1 = 1;
void appear(float Tsel) {
sel = Tsel;
//Selector
if (sel == 1) {
fill(0, 255, 0);
}
else if (sel == 2) {
fill(255, 0, 0);
}
if (!gamePause) {
rect(xloc, yloc, 40, 57);
yloc=yloc+speed;
// 1 --- DAMAGE
if (mouseY<yloc+40 && mouseY+90>yloc && sel == 2 && xloc+30>mouseX && xloc < mouseX+30 || switch1 == 0) {
yloc++;
rect(xloc, yloc, 40, 57);
switch1 = 0;
}
// 1 --- FIX
if (mouseY<yloc+40 && mouseY+90>yloc && sel == 1 && xloc+30>mouseX && xloc < mouseX+30 || switch1 == 0) {