How do I trigger an animation?
in
Programming Questions
•
3 years ago
Hello, I'm trying to make a sketch where a lightning bolt hits the ground and, as soon as it does, a particle is emitted from where the bolt hit. I can get the particle to appear, but I can't get it to animate. Instead, every single frame of the animation appears simultaneously. What am I doing wrong? Here is the code (the first chunk is what goes in the main tab; press the SPACE BAR to activate the lightning):
LightningBolt bolt;
void setup(){
size(640, 480);
smooth();
background(0);
}
void draw(){
noStroke();
fill(0,20);
rect(0,0,width,height);
}
void keyTyped(){
if(key==' ')
{
bolt = new LightningBolt();
bolt.drawBolt();
}
}
++++++++++++++++++++++++++++++++
class LightningBolt{
float lineWidth0;
float angle;
float x1,y1;
float x2,y2;
float straightJump;
float straightJumpMax;
float straightJumpMin;
float lineWidth;
LightningBolt(){
float randomX = random(0+(width/4), width-(width/4));
this.x1 = randomX;
this.x2 = randomX;
this.y1 = 0;
this.y2 = 0;
this.lineWidth0 = random(8,10); //starting thickness of bolt
this.angle = 0;
this.straightJumpMin = 1;
this.straightJumpMax = 10;
}
void drawBolt()
{
while((y2 < height) && ((x2 > 0) && (x2 < width)))
{
angle = randomSign()*random(PI/20, PI/5);
straightJump = random(straightJumpMin, straightJumpMax);
x2 = x1-straightJump * cos(angle - HALF_PI);
y2 = y1-straightJump * sin(angle - HALF_PI);
lineWidth = map(y2, height, 0, 1, lineWidth0);
stroke(255);
strokeWeight(lineWidth);
line(x1,y1,x2,y2);
x1=x2;
y1=y2;
if (y1 >= height){
fill(255);
noStroke();
Circle circle = new Circle(x1, height);
circle.drawCircle();
}
}
}
int randomSign()
{
float num = random(-1,1);
if(num == 0)
return -1;
else
return int(num/abs(num));
}
}
+++++++++++++++++++++++++++++++++
class Circle{
float x, y;
int radius;
float xSpeed;
float ySpeed;
float angle;
float speed;
float accel = .98;
Circle(float xCoord, float yCoord){
this.x = xCoord;
this.y = yCoord;
this.radius = 10;
this.angle = random(255.0, 285.0);
this.speed = random(12.0, 20.0);
this.xSpeed = speed * cos(radians(angle));
this.ySpeed = speed * sin(radians(angle));
}
void drawCircle(){
for (int i = height; i > 0; i--) {
y += ySpeed + 0.5 * accel;
ySpeed += accel;
x+= xSpeed;
ellipse(x, y, radius, radius);
}
}
}
LightningBolt bolt;
void setup(){
size(640, 480);
smooth();
background(0);
}
void draw(){
noStroke();
fill(0,20);
rect(0,0,width,height);
}
void keyTyped(){
if(key==' ')
{
bolt = new LightningBolt();
bolt.drawBolt();
}
}
++++++++++++++++++++++++++++++++
class LightningBolt{
float lineWidth0;
float angle;
float x1,y1;
float x2,y2;
float straightJump;
float straightJumpMax;
float straightJumpMin;
float lineWidth;
LightningBolt(){
float randomX = random(0+(width/4), width-(width/4));
this.x1 = randomX;
this.x2 = randomX;
this.y1 = 0;
this.y2 = 0;
this.lineWidth0 = random(8,10); //starting thickness of bolt
this.angle = 0;
this.straightJumpMin = 1;
this.straightJumpMax = 10;
}
void drawBolt()
{
while((y2 < height) && ((x2 > 0) && (x2 < width)))
{
angle = randomSign()*random(PI/20, PI/5);
straightJump = random(straightJumpMin, straightJumpMax);
x2 = x1-straightJump * cos(angle - HALF_PI);
y2 = y1-straightJump * sin(angle - HALF_PI);
lineWidth = map(y2, height, 0, 1, lineWidth0);
stroke(255);
strokeWeight(lineWidth);
line(x1,y1,x2,y2);
x1=x2;
y1=y2;
if (y1 >= height){
fill(255);
noStroke();
Circle circle = new Circle(x1, height);
circle.drawCircle();
}
}
}
int randomSign()
{
float num = random(-1,1);
if(num == 0)
return -1;
else
return int(num/abs(num));
}
}
+++++++++++++++++++++++++++++++++
class Circle{
float x, y;
int radius;
float xSpeed;
float ySpeed;
float angle;
float speed;
float accel = .98;
Circle(float xCoord, float yCoord){
this.x = xCoord;
this.y = yCoord;
this.radius = 10;
this.angle = random(255.0, 285.0);
this.speed = random(12.0, 20.0);
this.xSpeed = speed * cos(radians(angle));
this.ySpeed = speed * sin(radians(angle));
}
void drawCircle(){
for (int i = height; i > 0; i--) {
y += ySpeed + 0.5 * accel;
ySpeed += accel;
x+= xSpeed;
ellipse(x, y, radius, radius);
}
}
}
1