We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › function in draw() runs only once
Page Index Toggle Pages: 1
function in draw() runs only once? (Read 319 times)
function in draw() runs only once?
Feb 12th, 2009, 2:48am
 
Hi,

I have a function whose purpose(for now) is to:

1) display Album Art for background (disabled for debugging, for now)
2) Grow a bar up to a certain point, based on the length of the title.
3) "rubberband" back and forth just slightly before stabilizing at the title length.

Currently, the bar appears but does not grow past 4 pixels. It seems draw() keeps looping as it should, but executes the function only once? Here is the code:

Code:

String title = "Micawber's Moan"; //Initial Strings. This string is measured for bar length.
String artist = "Random"; //Ignore/unused for now.
String album = "Bad Joke EP"; //Ignore, unused for now.

boolean onOff = false; //rubberBand needs this, it's the first parameter.

PImage arts; //background
PFont a; //font of choice
int h = 20; //used for bar height.
float stringSize = 0; //used to measure how lengthy bar should be.
float rubber = 0.0; //used for "angle"
int down = 50; //used for "d"
int starter = 0; //used for "i" growth

void setup() {

arts = loadImage("bad joke ep.jpg");
size(arts.width,arts.height);
background(50); // gray background
a = loadFont("Futura-MediumItalic-12.vlw");
textFont(a); //this HAS to be used here or compiler error.
stringSize = textWidth(title); // finds the length of the String (Micawber's Moan) in pixels.

}

void draw() {

frameRate(60);
int ypos = arts.height - 80;
//y-pos scales to the size of the album art.


rubberBand(onOff, arts, starter, ypos, stringSize, h, rubber, down);

//We've sent the boolean state, the background image,
//the y-position that will be close to the bottom, and "i"
//the float length of the string for the bar,
//and h, the height of bar.
// "rubber" and "down" are special use stuff.

}

void mouseClicked() {

onOff = true; // if mouse gets clicked, the function operates.
}

void rubberBand (boolean clicked, PImage bg, int i, int tempY, float xLimit, int tempH, float angle, int d) {
//function recieves a bool, an Image for background, a Ypos for location,
//a float for width of bar(xLimit) , and a height of bar.
//angle and 'd' are for special purpose.

//maybe the ENTIRE FUNCTION runs only once? Irrelevent. We've narrowed it down, apparently functions within draw happen only once.
// Right

//problems with the portion below.
if(clicked == true && i < xLimit){
//Initial state, grows up to the width of the String.
background(10,100,10); //this appears as a medium Green status---never gets past here.
//refresh background.
fill(200); // Full Opacity for now.
rect(10, tempY, i * 6, tempH);
//a rectangle starting at left side, grows at the rate of i.
i += 4;// 4 because otherwise the damn thing grows way too slow---this only occurs once, should occur more.
angle = 0.0; // resets it for next time
d =50;//here too
println(i);


}
else if (i >= 50 && i< (xLimit+50))//once the box has grown to its full height the rubberband effect comes in
{
background(50,50,10); //this is a "yellow light" status--I have yet to see this status.
fill(200);
rect(10, tempY,sin(angle)*d + 300 , tempH);
i += 2;
angle += 0.4;
d--;
clicked = false;
}
else if( i >= xLimit && i < (xLimit + 100))
{

background(10,10,100);
fill(200, 50);
rect(1, tempY, 300 , tempH);
i++;
}
else
{
background(100,10,10);
i = 0;
}

}

Any thoughts? The rubberBand function is in a seperate tab, btw.
Re: function in draw() runs only once?
Reply #1 - Feb 12th, 2009, 11:33am
 
If draw() is looping, it doesn't execute the function only once: the println() result is repeated, showing multiple calls.

The problem is with the variable i in the function: that's not only a variable local to the function (so its value with vanish on exit), but also a parameter (its value is provided by draw() and will always be equal to 'starter' value).

In Processing/Java, parameters are always transmitted by value, never by reference. So incrementing i will never touch starter.
Just increment this global variable in the function...

Note: arrays and objects are also passed by value, but we pass actually a reference to the object, so while the reference is constant, any change to the object is reflected on the source!
Re: function in draw() runs only once?
Reply #2 - Feb 13th, 2009, 12:37am
 
Thank you, yes I am having some progress by simplifying the function and keeping things that iterate in the global space...

now I'm having boolean issues, though. Hmm.
Page Index Toggle Pages: 1