2012-11-22 31 views
0

我有这个程序wrritten了起来,它工作正常,但茎和花的生长速度不同。我怎样才能使这两个以相同的速度增长?我一直在为此奋斗了一两天,我知道这看起来很简单,但它对我来说是一种痛苦:P使两个对象以相同的速度增长?

+0

很难马上回答这个问题。代码太多,运行查看问题并不容易,问题描述有点模糊。你能给我们一个更简洁的问题片段吗?也许删除不影响结果的项目?这样做的时候你甚至可能会自己发现问题。 – BoppreH

回答

1

您无法轻松同步这两个动画的原因是因为这些茎基于增长在frameCount和花的增长基础上,不是基于frameCount或frameRate的比例。我已经修改了你的花类,速度非常快,说明其中的问题是:

事情之前被称为Flower.display()方法:

scale(-min((float)(frameCount)/250, 1), min((float)(frameCount)/250, 1)); 

这类似于你如何缩放茎。

Flower.grow()方法:

factor = maxFactor; 
sizes = maxSize; 

这是草率的,但快。只是强迫花朵以最大尺寸进行绘制,而上面的修改会缩放它。

下面是完整的源代码,意见和注释掉的代码:

Stem myStem; 
Circle circles[]; 
Flower flowers = new Flower(); 
float scaleFactor=0.5; 

void setup() { 
    size(floor(400*scaleFactor), floor(800*scaleFactor)); 
    myStem = new Stem(200, 800); 
    flowers = new Flower (0, 0); 
    //moved this to setup, no need to recreate each frame 
    circles = new Circle[6]; 
    circles[0] = new Circle(0, -40, 50, 50); 
    circles[1] = new Circle(0, -40, 50, 50); 
    circles[2] = new Circle(0, -40, 50, 50); 
    circles[3] = new Circle(0, -40, 50, 50); 
    circles[4] = new Circle(0, -40, 50, 50); 
    circles[5] = new Circle(0, 0, 50, 50); 
    // also smooth only needs to be called once 
    // unless ther is a noSmooth() somewhere 
    smooth(); 
    } 

    void draw() { 

    float grow = 0; 
    //translate(myStem.initalloX, myStem.initalloY); 
    myStem.drawStem(); 
    //set centre point 
    translate(myStem.initalloX, ((frameCount>250)?myStem.initalloY-  500:myStem.initalloY-(2*frameCount))); 
    if (frameCount>10) { 
    flowers.grow(); 
    flowers.display(); 
    } 
    } 

    class Stem { 
    int initalloX=200; 
    int initalloY=800; 

    Stem(int tempInitalloX, int tempInitalloY) { 
     initalloX = tempInitalloX; 
     initalloY = tempInitalloY; 
    } 

void drawStem() { 
    background(#0DBADB); 
    scale(scaleFactor, scaleFactor); 
    stroke (12, 149, 11); 
    fill (12, 149, 11); 
    strokeWeight(10); 
    line(initalloX, initalloY, initalloX, ((frameCount>250)?initalloY-500:initalloY-(2*frameCount))); 
    //stem1 
    if (frameCount>101) { 
     noStroke(); 
     translate(initalloX, initalloY-200); 
     scale(min((float)(frameCount-100)/100, 1), min((float)(frameCount-100)/100, 1)); 
     beginShape(); 
     vertex(0, 0); 
     bezierVertex(-40, -5, -30, -40, -80, -20); 
     bezierVertex(-47, -16, -52, 8, 0, 0); 
     endShape(CLOSE); 
     scale(1/min((float)(frameCount-100)/100, 1), 1/min((float)(frameCount-100)/100, 1)); 
     translate(-initalloX, -(initalloY-200)); 
     noStroke(); 
    } 
    //stem2 
    if (frameCount>151) { 
    // noStroke(); 
    translate(initalloX, initalloY-300); 
    scale(-min((float)(frameCount-150)/150, 1), min((float)(frameCount-150)/150, 1)); 
    beginShape(); 
    vertex(0, 0); 
    bezierVertex(-40, -5, -30, -40, -80, -20); 
    bezierVertex(-47, -16, -52, 8, 0, 0); 
    endShape(CLOSE); 
    scale(-1/min((float)(frameCount-150)/150, 1), 1/min((float)(frameCount-150)/150, 1)); 
    translate(-initalloX, -(initalloY-300)); 
    } 
    } 
} 

class Circle { 

    int c1 = 0; 
    int c2 = -40; 
    float c3 = 50; 
    float c4 = 50; 

    Circle(int tc1, int tc2, float tc3, float tc4) { 
    c1 = tc1; 
    c2 = tc2; 
    c3 = tc3; 
    c4 = tc4; 
    } 
    } 

class Flower { 

float centerX; 
float centerY; 
float posX; 
float posY; 
float maxSize = 51; 
float maxFactor = 40; 
float sizes = 0; 
float factor = 0; 
float speed = 0.17; 

Flower() { 
} 
Flower(float _centerX, float _centerY) 
{ 
    centerX = _centerX; 
    centerY = _centerY; 
} 
void setCenter(float x, float y) 
{ 
    centerX = x; 
    centerY = y; 
} 
void display() 
{ 
    // added line below 
    scale(-min((float)(frameCount)/250, 1), min((float)(frameCount)/250, 1)); 
    for (int i = -18; i < 360; i+=72) 
    { 
     posX = centerX + cos(radians(i)) * factor; 
     posY = centerY + sin(radians(i)) * factor; 
    noStroke(); 
    fill(170, 14, 24); // blue 
    ellipse(posX, posY, sizes, sizes); 
    } 
    fill(14, 17, 170); 
    ellipse(centerX, centerY, sizes , sizes); 
} 
void grow() 
{ 
// factor = (factor < maxFactor)? factor + speed: maxFactor; 
// sizes = (sizes < maxSize)? sizes + speed*1.3 : maxSize; 
    // modified line below 
    factor = maxFactor; 
    sizes = maxSize; 
} 
}// end of Flower 
相关问题