显示动画时按按钮更改对象 [英] Change object by pressing button while showing animation

查看:77
本文介绍了显示动画时按按钮更改对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的代码中添加动画.到目前为止,我拥有的是一个可以通过按下按钮来更改的对象.所以每次你按下按钮时,对象都会改变(它是一棵树,我正在改变它的树枝).是否可以添加某种动画,如雪?问题是我必须把它放在 draw 方法中,这样它才会被自动调用并让我们认为它是动画.因此,我还必须一直添加背景/按钮和所有内容.但是我不能用我的主要对象(树)来做,因为我只想在你按下按钮时改变它.

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.

有什么解决办法吗?

提前致谢

推荐答案

要保持某些对象同时刷新其他对象,您可以:

To persist some objects while refreshing others, you either:

  1. 只刷新屏幕的一部分.比如,绘制一个形状(矩形或其他),背景颜色只擦除屏幕的一部分

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

有条件地绘制选定的对象.使用标志选择性地绘制您需要的内容,每次绘制,并使用 background() 在每个绘制周期清除整个屏幕.

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.

使用图层.根据需要擦除一个图层而不是其他图层,在绘图中显示所有图层.这通常通过 PGraphics 对象完成.搜索处理 + 层以查看示例.在此处和/或在处理论坛中.

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.

以下是每种方法的一些简单示例:

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();
}

这篇关于显示动画时按按钮更改对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆