jeanhochu
YaBB Newbies
Offline
Posts: 5
Question about making the eruption effect
Dec 9th , 2008, 6:52am
Hi. I want to make the eruption effect, that if the ball of y location goes to the ground, to split into half and bounce. I can make the ball to get smaller and bounce, but when i tried to make another ball to make it split, it goes out of array index. But I don't know how to solve. Could you give any help or idea? Thank you. I will post the code. For the part that keep falling out of array, i put // so that it won't show any error if you played it. You should click mouse for make balls. int numballs=10000; float[] locx=new float[numballs]; float[] locy=new float[numballs]; float[] ma=new float[numballs]; float[] speedx=new float[numballs]; float[] speedy=new float[numballs]; int ball_length; int ball_lengthp; Balls[] ball=new Balls[numballs]; boolean play=false; float t,tt; void setup() { t=0; tt=0.2; size(500,500); background(0); } void draw(){ t+=tt; fill(0,20); rectMode(CORNERS); rect(width,height,0,0); if(play){ ball_length++; ball[ball_length] = new Balls(mouseX,mouseY,random(-.5,.5),1,10); } for (int i = 1; i < ball_length+1+ball_lengthp; i++) { if(ball[i].y>height-100){ ball[i] = new Balls(ball[i].x,ball[i].y,ball[i].xspeed/2,ball[i].yspeed*-1,ball[i].ma/2); //ball_lengthp++; //ball[ball_length+ball_lengthp] = new Balls(ball[i].x,ball[i].y,-ball[i].xspeed/2,ball[i].yspeed*-1,ball[i].ma/2); } ball[i].move(); ball[i].display(); } } void mousePressed(){ play=true; } void mouseReleased(){ play=false; } class Balls{ float x=250; float y=250; float ma; float xspeed; float yspeed; Balls(float xloc,float yloc,float speedx,float speedy,float mass) { x=xloc; y =yloc; xspeed = speedx; yspeed = speedy; ma=mass; } void move() { yspeed+=1; x+=xspeed; y +=yspeed; if(x>width-100){ xspeed*=-1; } } void display() { fill(255); noStroke(); ellipse(x,y,ma,ma); } }