Hi,
I've been trying to solve this for a few days and can't seem to so I figured I'd come and ask to see if anyone knew where I was going wrong and what I should be doing.
Basically I'm creating an array of Objects, which are balls that travel back and forth on the screen. I've added a random number feature in the code and when the random number generated equals 3 then I want to update the array of objects, expanding it by one and adding a new ball into the Array and then onto the screen.
My current main script looks like this:
int numBalls = 8; // variable to hold the initial size of array Ball[] balls = new Ball[numBalls]; // array to hold ball objects Ball addOneBall;
void setup() { // set up screen size(500, 500); smooth(); noStroke();
// for loop to fill array with balls for (int i = 0; i < balls.length; i++) { balls[i] = new Ball(); } }
void draw() { fill(255, 200); //white see-through background rect(0, 0, width, height); // draw rect instead of background so we can use alpha to give comet tail effect on balls
int addOne = int(random(100)); //generate a random number to use in a conditional in a moment println("random number is " + addOne); //print random number to terminal to monitor
//conditional statement if random number is three... if (addOne == 3) { int currentLength = balls.length; // Create and int of the value of the current length of the array println("Current length of balls array is " + currentLength); ///print value to terminal
balls = (Ball[]) expand(currentLength+1); //extend the current array by one addOneBall = new Ball(); // Create new Ball balls = splice(balls, addOneBall, currentLength); //splice addOneBall and balls array, placing addOneBall in the last position of the array }
// for (int i = 0; i < balls.length; i++) { balls[i].move(); balls[i].updateMe(); balls[i].display(); } }
my Class for the Ball looks like this:
class Ball { float x, y; float diam; float radius; float speed; int dir =1; int decision = int(random(-1, 1)); color shade;
int addABall = 1;
Ball() { x = random(width); y = random(height); diam = (random(10)+10); radius = diam/2; speed = random(5); }
void move() { x+=(speed * dir); if ((x > width) || (x< 0)) { dir *= -1; } }
When I initially tried to solve this problem I figured I'd have to reiterate the for loop in setup every time I wanted to lengthen the array. Then I realised that 1. this was a bad idea because its inefficient & I'd loose the location of the balls on screen and 2. a more sensible thing to do would be to simply extend the size of the Array by one and then stick a new ball object in the new empty slot. I try to do this in the main code with the following code inside the if statement in void loop:
int currentLength = balls.length; // Create an int of the value of the current length of the array println("Current length of balls array is " + currentLength); ///print value to terminal
balls = (Ball[]) expand(currentLength+1); //extend the current array by one addOneBall = new Ball(); // Create new Ball balls = splice(balls, addOneBall, currentLength); //splice addOneBall and balls array, placing addOneBall in the last position of the array
Despite the fact that I can see the file in the data folder in my sketch folder everytime I run my sketch I get a nullpoint exception. It doesn't make sense to me because my file structure is correct. I'm using processing on a Mac running Lion, if that helps. Here's my code, it's rough as I'm just getting this project off the ground:
// Set the Name if (wordNum == 0) { theName = named[stepper]; theNameFin = theName; cntr++; if (cntr == 10) { theName = theNameFin; wordNum = 1; cntr = 0; } } // Set the first word if (wordNum == 1) { first = wordOne[stepper]; firstFin = first; cntr++; println(cntr); if (cntr == 10) { first = firstFin; wordNum = 2; cntr = 0; } }
// set the second word if (wordNum == 2) { sec = wordTwo[stepper]; secFin = sec; cntr++; if (cntr == 10) { sec = secFin; wordNum = 3; cntr = 0; } } println(first + sec);
fin = messageFor.concat(first + sec); // first.concat(sec); // Concatanate all the strings into one final string textSize(20); textAlign(LEFT); text(fin, 20, 100); //Print the final string to the window
// Re set the stepper to 0 so I don't get a Null pointer exception error in case of -1 return stepper++; if (stepper>3) { stepper = 0; }
// This is a means of keeping the loop running for animation testing if (wordNum == 3) { int wee = int(random(0, 30)); println(wee); if(wee == 3) { wordNum =0; named = " "; first = " "; sec = " ";
I'm trying to make a little bit of code where I read values from two arrays. My aim is to concatenate values from each array into a string and then print the value to the applet window. In the applet window I want the text to appear to be continually changing. I'm hitting a bit of trouble. I keep getting a message saying that the compiler Can't convert object to String. Can anyone tell me how to combine objects in this way in processing? My code is below. Thanks for reading.
Hi All,
I wonder if anyone has come across this and found away round it. I'm in the beginning stages of a new project. AS part of this project I need to print strings to the screen via the text() function. My problem is that I need to print the strings to screen as the code runs. However I've noticed that in Processing the loop has to complete once before printing the String to the screen via text(); is there a way around this? To demonstrate what I mean I've put a simple script below.
PFont font; int counter = 0;
void setup() { size(500, 200); smooth(); }
void draw() { background(255, 0, 0); println("I am at the top of the loop"); delay(2000); font = loadFont("Arial-Black-24.vlw"); textFont(font); String s = "test Text"; println("I have been around the loop " + counter); text(s, width/2, height/2); delay(2000); counter++; }
I have a few questions about working with long numbers in processing and wondered if anyone could clarify for me?
I ran the following code to start with:
void setup() { size(800, 100); background(0); }
void draw() { int bigInt = 987654321 * 123456789; println("the value of the integer bigInt is "+bigInt); delay(1000);
PFont font; font = loadFont("ArialMT-20.vlw"); textFont(font, 20); String s =(""+bigInt); text(s, width/2, height/2);
long bigLong = 987654321 * 123456789; println("the value of the long bigLong is "+bigLong); }
For each the console prints: -67153019 and the same appears in the window. Surely the answer is: 121932631112635260. Can anyone tell me what's going on?
I started playing with printing out number values as I want to print a large changing calculated number out to the display window and I wanted to check if processing would truncate it. The above reading has confused me on route to my original task. Any clues welcome! I'm guessing this is some form of trunctuation, but I don't understand it.
I want processing to alter the position of the ends of the lines when the mouse is moved. though at the moment when the mouse is moved processing just draws new lines over the tops of the old ones. I've tried a few things to fix this. For instance moving the vert and hor variables into the ends of the bracketed for statements.
I'm at a bit of a loss here. I know it's got to do with the fact that processing loops through the code until it is killed and also that the lines are being drawn within a for statement. I'm thinking that what I need to do is put this for loop in its own function and find a way to pass values from it for i and m and then draw the line within void draw(). I tried doing this but got error messages. Which made me think I may be way off the mark with that line of thought.
Can someone help me see the forest instead of the trees?
I'm quite new to processing. More used to working with xCode and Objective-C. I noticed my laptop Fan comes on and my CPU usage goes up to 69% or more everytime I run the following sketch:
void setup() { size(1120, 800); x = width/2; y = height/2; background(255,30,30);
String arduinoPort = Serial.list()[0]; port = new Serial(this, arduinoPort, 9600); }
void draw() { smooth(); frameRate(30); float value = brightness((int)random(255)); fill(value);
//If data is available... if (port.available() >0) { //Read it and store it in the variable val... val = port.read(); velocity = val/20; // convert it to a value using the map function }
What I couldn't gather from this thread was if the high CPU usage would damage the machine? I'm guessing it's not good for it. Can anyone tell me? Also is there a way to manage CPU usage by the program from within the script?
I'm wondering if anyone can help me with this, I've been googling but can only find advice for PC users running windows 7.
I'm running a simple sketch that take's data from an Arduino via USB. At the start of the sketch I get the following warning in the console:
WARNING: RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
I've tried running a few different sketches that interface with the Arduino and they all produce this error.
That said, the sketch seems to run fine. Though I'm a bit worried that this incompatibility may produce future problems as I develop my project. Can anyone tell me how to remedy this problem on a Mac platform? Is there a simple way such as reinstall processing?