Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

processing - Change object by pressing button while showing animation

I'm trying to add animation in my code. What I have so far is an object that can be changed by pressing a button. So every time you press the button, the object changes (it is a tree and I'm changing its branches). Is it possible to add some kind of animation like snow? The problem with that is that I have to put it inside the draw method so it will be called automatically and make us think that it is animation. Thus, I also have to add the background / button and everything all the time. But I can't do that with my main object (tree) as I want to change it only when you press the button.

Is there any solution to that?

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To persist some objects while refreshing others, you either:

  1. Refresh only part of the screen. Like, draw a shape (rect or whatever) with background colour erasing only part of screen

  2. Conditionally draw selected objects. Use flags to selective draw what you need, every draw, and use background() to clear the whole screen every draw cycle.

  3. Use layers. Erase one layer and not other as you need, display all them in draw. This is usually done with PGraphics objects. Search processing + layers to see samples. Here and/or in processing forum.

EDIT: Here some simple examples of each approach:

1.

/**
 * A very simple example of erasing just part of the screen to 
 * selective persist draws
 **/


void setup() {
  size(400, 400);
  background(0);
  noStroke();
}

void draw() {
  fill(0);
  rect(0, 0, width/2, height);

  fill(120);
  ellipse(width/4, frameCount%width, 100, 100);
}

void mouseMoved() {
  fill(255);
  ellipse(mouseX, mouseY, 10, 10);
}

2.

 /**
 * A very simple example of conditionally draw stuf 
 * to selective persist draws
 **/

ArrayList <PVector> points = new ArrayList <PVector>();
boolean showBalls = true; // any key to toogle

void setup() {
  size(400, 400);
  background(0);
  noStroke();
}

void draw() {
  background(0);
  fill(30);
  rect(frameCount%width, 100, 200, 200);

  fill(120);
  ellipse(width/2, frameCount%width, 150, 150);

  fill(255);
  if (showBalls) {
    for (PVector p : points) {
      ellipse(p.x, p.y, 10, 10);
    }
  }
  if (points.size() > 500) {
    points.clear();
  }
}

void mouseMoved() {
  ellipse(mouseX, mouseY, 10, 10);
  points.add(new PVector(mouseX, mouseY));
}

void keyPressed() {
  showBalls = !showBalls;
}

3.

/**
 * A very simple example of using PGraphics as layers 
 * to selective persist draws
 **/

PGraphics layer;

void setup() {
  size(400, 400);
  layer = createGraphics(width, height);
  layer.beginDraw();
  layer.fill(255);
  layer.endDraw();
  background(0);
  noStroke();
}

void draw() {
  background(0);
  fill(30);
  rect(frameCount%width, 100, 200, 200);

  fill(120);
  ellipse(width/2, frameCount%width, 150, 150);
  image(layer, 0, 0);
}

void mouseMoved() {

  layer.beginDraw();
  layer.ellipse(mouseX, mouseY, 10, 10);
  layer.endDraw();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...