"Custom" side scrolling text - easy to use
Posted: Sun Apr 22, 2007 6:30 pm
Since novices code isn't as efficient as it could be, and now sparky is posting his memory destroying version of the same code, I wrote up a cleaner piece of code that uses minimal CPU + RAM, has less than half the code, and has a much better way of calling the function to draw.
the code:
To use the code:
1. Copy the above code to global code
2. Create a "draw actor" event on a text actor with script editor action:
3. Use the function writeText() whenever you want to type out text. EG, in a mousedown event on an actor:
The first parameter of the function is the name of the text actor you wish to draw to. Make sure that all the text actors you wish to use typing on have the drawText(); call in their "Draw Actor" event.
You can adjust the typing speed and the character appended to the text by modifying the 2 #DEFINE statements at the top of my code. If you wanted to make it play a sound as it types its 1 extra line of code. If you cant see where that line would go then post here and I'll help.
the code:
- Code: Select all
char buf[512];
int pos, ptimer;
Actor *tgt;
#define TYPE_SPEED 5
#define APPEND_CHAR "_"
void drawText()
{
if (ptimer < TYPE_SPEED) { ptimer++; return; }
ptimer = 0;
if (pos<0)
if (pos==-1) { pos = -2; strcpy(tgt->text, buf); return; }
else if (pos==-2) { pos = -1; strcpy(tgt->text, buf); strcat(tgt->text, APPEND_CHAR); return; }
else return;
strncpy(tgt->text, buf, pos);
strncat(tgt->text, APPEND_CHAR, 1);
pos++;
if (pos > strlen(buf)) pos = -1;
}
void writeText(Actor *ent, char *txt)
{
sprintf(ent->text, " ");
sprintf(buf, txt);
tgt = ent;
pos = 1;
ptimer = 0;
}
To use the code:
1. Copy the above code to global code
2. Create a "draw actor" event on a text actor with script editor action:
- Code: Select all
drawText();
3. Use the function writeText() whenever you want to type out text. EG, in a mousedown event on an actor:
- Code: Select all
writeText(&mytextactor, "hello world");
The first parameter of the function is the name of the text actor you wish to draw to. Make sure that all the text actors you wish to use typing on have the drawText(); call in their "Draw Actor" event.
You can adjust the typing speed and the character appended to the text by modifying the 2 #DEFINE statements at the top of my code. If you wanted to make it play a sound as it types its 1 extra line of code. If you cant see where that line would go then post here and I'll help.