我正在试验Processing,我为我的项目写了一个非常简单的舞台系统。我分为三个阶段:
0-持续2秒,灰屏
1-持续1秒,从黑色过渡到白色
2-持续2秒,从白色过渡到黑色

阶段1和2反复重复,有时在任一阶段的最后一帧和下一阶段的第一帧之间重复,屏幕闪烁(我想您会自己看到的)。

package ryby;

import java.awt.Color;

import processing.core.PApplet;
import processing.core.PImage;

public class Ryby extends PApplet {

    public static PApplet APPLET;
    public static final int X = 700;
    public static final int Y = 465;

    int counter;
    int state = 0;
    int substate = 0;

    StringBuilder title = new StringBuilder();

    @Override
    public void setup() {
        APPLET = this;
        frameRate(60);
        smooth();
        size(X, Y, JAVA2D);
        counter = millis();
    }

    @Override
    public void draw() {
        title.delete(0, title.length());
        title.append("Ryby - ").append(state).append(" ").append(substate)
                .append(" ").append(frameRate);
        frame.setTitle(title.toString());
        Color color = new Color(get(0, 0));
        System.out.println(color);
        switch (state) {
            case 0:
                intro();
                break;
        }

    }

    public static void main(String _args[]) {
        PApplet.main(new String[]{ryby.Ryby.class.getName()});
    }

    void intro() {
        System.out.println(substate);
        System.out.println(millis() - counter);
        switch (substate) {
            case 0:
                setSubState(2000, 1);
                break;
            case 1:
                background((millis() - counter) * 255 / 1000);
                setSubState(1000, 2);

                break;
            case 2:
                background(255 - (millis() - counter) * 255 / 2000);
                setSubState(2000, 1);

                break;
        }

    }

    void setState(int ms, int s) {
        if (millis() >= counter + ms) {
            state = s;
            counter = millis();
        }
    }

    void setSubState(int ms, int s) {

        if (millis() >= counter + ms) {
            substate = s;
            counter = millis();

        }

    }
}


我知道我在某个地方犯了一个愚蠢的错误,我只需要找出哪里:)

最佳答案

设置背景时似乎与除以整数有关。使它们浮动似乎可以解决问题……因此,在intro()中的switch(子状态){

代替

background((millis() - counter) * 255 / 1000);




background(255 - (millis() - counter) * 255 / 2000);




background((millis() - counter) * 255 / 1000.0);




background(255 - (millis() - counter) * 255 / 2000.0);

09-27 10:38