We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › What gives!!!
Page Index Toggle Pages: 1
What gives!!! (Read 918 times)
What gives!!!
Sep 17th, 2007, 12:33am
 
Quote:


// Import traer physics engine
import traer.physics.*;
// Make variables
int firstballx, firstbally, secondballx, secondbally;
firstballx = int(firstBall.position().x());
firstbally = int(firstBall.position().y());
secondballx = int(secondBall.position().x());
secondbally = int(secondBall.position().y());
ParticleSystem sys = new ParticleSystem(0.0, 1.0);
Particle firstBall = sys.makeParticle(4.0, 150.0, 300.0, 1.0);
Particle secondBall = sys.makeParticle(4.0, 350.0, 300.0, 1.0);
Spring tether = sys.makeSpring(firstBall, secondBall, 4.0, 1.0, 80.0);
// Set up the program
void setup()
{
 size(500, 600);                                                              // Set size to 500x600
 fill(0, 0, 0);                                                               // Fill shapes with black
}
// Start main program
void draw()
{
 sys.tick();                                                                  // Advance physics engine clock by one second
 background(255, 255, 255);                                                   // Erase screen between frames
 line(firstballx, firstbally, secondballx, secondbally);                      // Create a tether
 ellipse(firstballx, firstbally, 40, 40);                                     // Make the first ball
 ellipse(secondballx, secondbally, 40, 40);                                   // Make the second ball
}




It gives me the following error:
Quote:
unexpected token: void


Referring to the underlined section of code.
Re: What gives!!!
Reply #1 - Sep 17th, 2007, 1:02am
 
Processing has 2 modes, one which just a single run through in which you can use functions/methods outside of functions, and the other in which you have setup and draw and so have multiple loops through, but in which you can't due to restrictions within Java.

So the reason you're getting the error, is that you're setting values from functions/methods outside of a function and so you can't use setup/draw method.

To fix it, you'll need to move he initialization of the values to within setup.

Code:
/ Import traer physics engine
import traer.physics.*;
// Make variables
int firstballx, firstbally, secondballx, secondbally;
ParticleSystem sys;
Particle firstBall;
Particle secondBall;
Spring tether;
// Set up the program
void setup()
{
size(500, 600); // Set size to 500x600

firstballx = int(firstBall.position().x());
firstbally = int(firstBall.position().y());
secondballx = int(secondBall.position().x());
secondbally = int(secondBall.position().y());

sys = new ParticleSystem(0.0, 1.0);
firstBall = sys.makeParticle(4.0, 150.0, 300.0, 1.0);
secondBall = sys.makeParticle(4.0, 350.0, 300.0, 1.0);
tether = sys.makeSpring(firstBall, secondBall, 4.0, 1.0, 80.0);

fill(0, 0, 0);
}
void draw()
{
sys.tick();
background(255, 255, 255);
line(firstballx, firstbally, secondballx, secondbally);
ellipse(firstballx, firstbally, 40, 40);
ellipse(secondballx, secondbally, 40, 40);
}
Re: What gives!!!
Reply #2 - Sep 17th, 2007, 3:33am
 
Actually, you'll need to make the particles before trying at access their positions. Otherwise, you'll get a null pointer exception for trying to dereference an uninitialized variable.
Re: What gives!!!
Reply #3 - Sep 22nd, 2007, 9:41pm
 
Uggg...
Quote:

// Import traer physics engine
import traer.physics.*;
// Make variables
int firstballx, firstbally, secondballx, secondbally;
ParticleSystem sys;
Particle firstBall;
Particle secondBall;
Spring tether;
// Set up the program
void setup()
{
 sys = new ParticleSystem(0.0, 0.0);
 firstBall = sys.makeParticle(4.0, 150.0, 240.0, 1.0);
 secondBall = sys.makeParticle(4.0, 350.0, 240.0, 1.0);
 firstballx = int(firstBall.position().x());
 firstbally = int(firstBall.position().y());
 secondballx = int(secondBall.position().x());
 secondbally = int(secondBall.position().y());
 tether = sys.makeSpring(firstBall, secondBall, 4.0, 1.0, 80.0);
 size(600, 480);                    
 fill(0, 0, 0);                    
 smooth();
}
// Start main program
void draw()
{
 sys.tick();                                              
 background(255, 255, 255);        
 line(firstballx, firstbally, secondballx, secondbally);
 ellipse(firstballx, firstbally, 40, 40);
 ellipse(secondballx, secondbally, 40, 40);
}



Why don't the balls attract?
Re: What gives!!!
Reply #4 - Sep 22nd, 2007, 10:50pm
 
You don't update the values of firstballx/y/etc you set their value once in setup, and then don't update their values from the particles.

What you need to do in draw is:

Code:
void draw()
{
sys.tick();
background(255, 255, 255);
firstballx = int(firstBall.position().x());
firstbally = int(firstBall.position().y());
secondballx = int(secondBall.position().x());
secondbally = int(secondBall.position().y());

line(firstballx, firstbally, secondballx, secondbally);
ellipse(firstballx, firstbally, 40, 40);
ellipse(secondballx, secondbally, 40, 40);
}
Re: What gives!!!
Reply #5 - Sep 28th, 2007, 2:45am
 
I cannot believe what is wrong with this code:
Quote:


import controlP5.*;
ControlP5 controlP5;
void setup()
{
 size(800, 600);
 controlP5 = new ControlP5(this);
 controlP5.addButton("Start", 10, 10, 10, 150, 75);
}
void draw()
{
 background(255, 255, 255);
}
void button(int theValue)
{
print("hi");
}





It doesn't say "hi" in the console when you click!
Re: What gives!!!
Reply #6 - Sep 28th, 2007, 3:09am
 
hi,
please replace the line

Code:

controlP5.addButton("Start", 10, 10, 10, 150, 75);


with
Code:

controlP5.addButton("button", 10, 10, 10, 150, 75);
controlP5.controller("button").setLabel("Start");


the concept here is, that each controller gets a name as identifier. the name is given when you add a controller to controlP5. here this would be controlP5.addButton, where "button" would be the identifier. furthermore the name is used to link a method in your program to the execution of a controller, meaning controlP5 checks if a method with the same name as your controller exists. in your case "Start" didnt match the method name "button", thats why you didnt see any results when clicking the button. with the code replacement that should be different. keep in mind, that label and name are different things. if you dont change the label of a controller with .setLabel, the label defaults to the name/identifier of a controller. hope this helps,
best,
andi
Page Index Toggle Pages: 1