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 & HelpPrograms › Moving text (newbie)
Page Index Toggle Pages: 1
Moving text (newbie) (Read 792 times)
Moving text (newbie)
Mar 4th, 2008, 4:58pm
 
hi,

all i need to know is how to take a simple piece of text and move it up and down the screen using two keys, such as A + Z. it seems simple enough however for a newbie who has never done processing before it's quite daunting. i'm a student and need help quickly, so if anyone can help me out i'd really appreciate it.

thanks
Re: Moving text (newbie)
Reply #1 - Mar 4th, 2008, 6:02pm
 
Make a class, called something like movingText.  The class will have 3 member variables; x,y for position and a String to hold the actual text.

See: http://processing.org/reference/class.html

Code:

class movingText {
float x, y;
String s;

movingText (float _x, float _y, String _text) {
//This function is the 'constructor'.
//It will get called when you create a new
//movingText object
x = _x; //Set x to the first argument
y = _y; //Set y to the second argument
s = _text; //Set our string to the 3rd argument

}
void drawText() {
//Place code to actually draw the text here.
// see : http://processing.org/reference/text_.html

}
// Now, two methods to change the x&y variables
void move_y(float amount){
y+=amount;
}

void move_x(float amount){
x+=amount;
}
}


Then you need the main program structure:
Code:


movingText myText; //Create an object "myText" using the new class

void setup(){
size(500,500,P3D);
myText=new movingText(250,250,"processing rules"); //call the constructor
}

void draw(){
myText.drawText(); // draw the text on every frame

}


void keyPressed(){

//This function is called when any key is pressed
// if A is pressed, call myText.moveY(-1); etc..

// see: http://processing.org/reference/keyPressed_.html
// and: http://processing.org/reference/key.html

}



You will need to create a font (using the Processing IDE) and add code for loading the font.  

Always use the reference section! : http://processing.org/reference/index_ext.html

The processing documentation is unbeatable, and loaded with examples.   Good luck!
Page Index Toggle Pages: 1