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 › moving a ball
Page Index Toggle Pages: 1
moving a ball (Read 1197 times)
moving a ball
Dec 26th, 2009, 3:46am
 
Hi,

Im trying to program a game with moving balls in the screen. I´ve already make a ball moving and bouncing in the screen. I want to introduce more balls at the same time. I know its better to make it with objects but I want to start doing it with an array.

I´ve made the same program I used with a ball, but I,ve defined an array of balls, but when it comes to the setup function it have an error an displays: "unexpected token: void"

This is the code:

Code:
int[] x=new int [3];
x[0]=150;
x[1]=30;
x[2]=150;
int[] y=new int [3];
y[0]=150;
y[1]=350;
y[2]=350;
int r=10;
int[] incx=new int [3];
incx[0]=1;
incx[1]=1;
incx[2]=1;
int[] incy=new int [3];
incy[0]=1;
incy[1]=1;
incy[2]=1;
int i;

void setup(){
size(700,700);
background(0);
stroke(255);
smooth();
frameRate(100);
}




void draw() {
background(0);
for (i=0;i<=2;i++){
ellipse(x[i],y[i],r,r);
if ((x[i]>699)||(x[i]<1)){
incx[i]=incx[i]*(-1);
}
if ((y[i]>699)||(y[i]<1)){
incy[i]=incy[i]*(-1);
}
x[i]=x[i]+incx[i];
y[i]=y[i]+incy[i];
}
}
Re: moving a ball
Reply #1 - Dec 26th, 2009, 4:02am
 
Move all the lines like x[0]=150; in setup().
Re: moving a ball
Reply #2 - Dec 26th, 2009, 12:23pm
 
Thank you, I've made it and it works, but I don't understand the reason because for example I make int r=10; outside the setup. I declare and inicialize outside, and what I did with the array was the same, I think it should work.

I also don't understand why my balls bounce between them, they're not programmed to do that, they should bounce only if their coordenates pass some values.
Re: moving a ball
Reply #3 - Dec 27th, 2009, 2:56am
 
> because for example I make int r=10; outside the setup. I declare and inicialize outside, and what I did with the array was the same, I think it should work.

no, that's different

int r = 10;

is declaring AND initialising. this is ok.

but this:

int[] x=new int [3];
x[0]=150;

is declaring on the first line and initialising on the second. that second line isn't allowed outside of a method so it must be inside setup().
Re: moving a ball
Reply #4 - Dec 27th, 2009, 7:14am
 
There's also another benefit to initialising your variables in a method.  When you want to reset your game (e.g. to start a new game after 'game over'...) you simply call this method to set all the variables back to their starting values.  I gather from a post by PhilHo that you shouldn't simply call setup again; so you'd need to create a separate method and call it from setup().   For example:

Code:
int foo;
int[] bar = new int[3];

void setup() {
 size(300,200);
 setGameStartValues():
}

void setGameStartValues() {
 foo = 3;
 for(int i=0;i<3;i++) {
   bar[i] = 100 + i*50;
 }
 
}


void draw() {
 // do stuff...
 if(gameOver) {
   setGameStartValues();
 }
}

Page Index Toggle Pages: 1