We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all!
First of all, i'm new to java so please forgive me for easy questions and my bad English (I'm Dutch).
I have a code with an array: robotStartX[], the x position of a robot. I would now like to add the value of a variable to one of the indexes of robotStartX[].
Why doesn't the following work?
int robotStartX[] = new int[4];
int value = 12;
robotStartX[1] += value;
println(robotStartX[1]);
Could someone please tell me how to do this?
Thanks!
Joris van Lanen
Answers
It prints out 12. I don't get why you're claiming it's not working? (:|
also note: array indexes start at 0 so robotStartX[1] is actually the second after robotStartX[0] (but perhaps you know this)
Oke thanks, I made a new simple one to test it out:
It indeed works fine, but for some reason it doesn't work with my other code. I'll just paste it here. There might me some Dutch words, sorry for that!
The robots should be going further to the right because the X is being raised, but it doesn't......
Thanks
tekenRobots() re-create the array of robots on each frame, the previous one is lost.
I think you want to define robotStartX as a global variable.
See also the What are setup() and draw()? article.
Thanks very much!!! It works now! I indeed only had to move the create array statement to a place where it's not being done over and over again. Thanks again, problem SOLVED! :)