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 & HelpSyntax Questions › A basic level problem
Page Index Toggle Pages: 1
A basic level problem (Read 260 times)
A basic level problem
Apr 6th, 2008, 8:18am
 
UPDATED: Problem solved

I'm to create simple program where a ball is bouncing on the screen. Each corner should have a color and each time the ball hits a wall it should change to that sides color.

Very simpley yet I'm manage to get stuck

Right now the color of the ball only change at the moment it tuched the wall. But I want the ball to remain in the color of the wall. If I remover the corners I get the function I want. But then.. I have no corners. The problem lies in the "fill" I have to use to specify a color on the wall.

Is there anyway to draw the corners outside the "void draw?" Or are there any other solotion???

//----------------------------------- Gobal Settings
float PosX, PosY, Value1, Value2;
float xmover, ymover;
int ballsizex, ballsizey;

color bg = color(45,44,42,150);          
color sg = color(163,219,107,100);        
color dg = color(61,83,52,150);          
color bl = color(51,65,83,150);          
color re = color(140,35,24,150);          

void setup()
{
 size(400,300);
 PosX = 0;
 PosY = 0;
 Value1 = width/40;
 Value2 = height/30;
 xmover = 1;
 ymover = 2;
 ballsizex = width/20;
 ballsizey = height/15;

}
void draw()
{

 background(bg);
 smooth();
 noStroke();
 
 //----------------------------------- Ball

 ellipse(PosX+(Value1 + ballsizex), PosY+(Value2 + ballsizex), ballsizex, ballsizey);
 PosX = PosX + xmover;
 PosY = PosY + ymover;

 //----------------------------------- Bounce
 if(PosX  < 0  ||  PosX > width - (ballsizex*3)) {
   xmover = -xmover;
 }
 if(PosY  < 0  || PosY > height - (ballsizex*3)) {
   ymover = -ymover;
 }


//----------------------------------- Corners
 fill(sg);
 rect(0, ballsizex, ballsizex, height);
 fill(re);
 rect(0, 0, width, ballsizex);
 fill(bl);
 rect(width - ballsizex, 0 , width - ballsizex, height);
 fill(dg);
 rect(0, height - ballsizex, width, height - ballsizex);
 fill(0);

 //----------------------------------- Color change

 if (PosX == width - (ballsizex*3)) {
   fill(bl);
 }
 if (PosY == height - (ballsizex*3)) {
   fill(dg);
 }
 if(PosX == 0) {
   fill(sg);
 }
 if(PosY == 0) {
   fill(re);
 }

}

EDIT: Problem solved by using mover in the color script:

 if ((PosX > width - (ballsizex*5))  &&  xmover<0) {
   fill(bl);
 }
 if ((PosY > height - (ballsizex*5)) && ymover<0) {
   fill(dg);
 }
 if((PosX < (0+(ballsizex*2))) && xmover>0) {
   fill(sg);
 }
 if((PosY < (0+(ballsizex*2))) && ymover>0) {
   fill(re);
 }

Leave the answer here is my classmates happends to pass by
Page Index Toggle Pages: 1