View Full Version : Flash RPG Character Dialogue help.
jessejburton
04-12-2007, 06:41 PM
Hey All,
Just wondering if anyone has any ideas as to the best way to handle character (main and npc) dialogue for a flash RPG that I am working on. I have a function that opens and closes the dialogue box. And another function to display the text called
displayText(speech){}
speech is an array of text that I pass based on the character that I am talking to. What I need to be able to do is have it display the first element until the space key is pressed, then the second and so forth. In another language I would use a FOR NEXT loop however I can't seem to figure out how to use NEXT in flash. It always seems to display the last element after running through each elements, never pausing to be read.
here is an example of what I have tried. Any assistance would be very much appreciated.
//Function to display dialogue - speech is an array
display = function(speech){
for(i = 0; i < speech.length; i++){
dialogueBox.speech.text = speech[i];
if(Key.isDown(Key.SPACE)){
next i;
else{}
}
}
Thanks for the help!
'Childe Roland to the Dark Tower Came'
Welcome aboard,
Your not too far off. In AS you just count numerically through the array, so if you want the next message, just increment your counter:
//Function to display dialogue - speech is an array
display = function(speech){
for(i = 0; i < speech.length; i++){
dialogueBox.speech.text = speech[i];
if(Key.isDown(Key.SPACE)){
i++
else{}
}
}
Oh, and you would probably want to throw an evaluation somewhere in there to be sure that i<speech.length.
jessejburton
04-12-2007, 07:20 PM
Hey Noct,
Thanks for the reply I really appreciate it. I tried the code and It still seems to be looping through right to the end. I am wondering of there is someway of limiting the loop within the for statement?
for(i = 0; i < speech.length; 'something to limit i')
not sure?
I have confirmed that speech.length is giving me the proper number as well thanks for that tip.
Hey, no problem, glad to help.
Ya know, I didn't evaluate your script earlier, I was just mentioning how to increment through an array. (it errors out as is)
Anyways, see if this helps at all:
var speechArray = new Array("First Message", "Message 2", "Last Message");
i = 0;
function displayText() {
onEnterFrame = function () {
dialogueBox.speech.text = speechArray[i];
if (Key.isDown(Key.SPACE)) {
if (i<speechArray.length-1) {
i++;
} else {
i = 0;
}
}
};
}
displayText();
The only issue here being that if you hold down the space bar it will continue to cycle through all the messages. You could use a keyboard listener to get better results.
Like, this is even better IMO:
var speechArray = new Array("First Message", "Message 2", "Last Message");
var i:Number = 0;
function displayText() {
dialogueBox.speech.text = speechArray[i];
}
//
keyListener = new Object();
keyListener.onKeyDown = function():Void {
if (Key.isDown(Key.SPACE)) {
displayText()
if (i<speechArray.length-1) {
i++;
} else {
i = 0;
}
}
};
Key.addListener(keyListener);
jessejburton
04-12-2007, 08:40 PM
Hey Noct,
Thanks a ton that seems to be working. I wonder if there will be preformance issues with it continuously looping like that? Do you think so?
I will definately try using a key listener rather than the onEnterFrame. That is a great idea. Thanks for the help I really appreciate it.
Jesse
'Childe Roland to the Dark Tower Came'
jessejburton
04-12-2007, 08:41 PM
That looks really good as well. I will definately work with that. The only thing I can see is that I want to be able to pass the array in based on which character the person is talking to so I will ahve to make some adjustments but atleast I have somewhere to start now.
jessejburton
04-12-2007, 08:46 PM
Ok now I have had a chance to play around with the code you gave me and see exaclty how it works. It is very nice! I cane definately use this. Thanks so much. This has been on my brain all week. Now I can sleep! and move on to the next function in the game. This is my first time on this forum but I am really glad I signed up. It is great to get and give advice!
jessejburton
04-12-2007, 09:22 PM
Hey,
So here is the updated code: This as is functions correctly except for the break.... I am not sure if that is the correct way to end a function. It appears to me that it is trying to read both arrays. I am wondering if I put the speak(Array) inside of an if statement based on a variable and change the variable at the end of the array?? Hey I may have just answered my own question!
//Set Character speech
var DrPeepoo = new Array("Welcome to the village of Dr. Peepoo", "Unfortunately for you, you are not welcome here.", "Fortunately for us, we are hungry cannibals.", "All you have to do is let us eat you, and it will solve all of our problems.");
var JimJungle = new Array("I.....", "You......", "AAAAAAHHHHHHHHHH");
//Set variable for dialogue array element.
var i:Number = 0;
/*Function to display dialogue - Assitance from NOCT on actionscript.org
Thanks for the help NOCT*/
function displayText(speech) {
dialogueBox.text = speech[i];
}
function speak(speech) {
//Cycle through the dialogue with the SPACE key - Assitance from NOCT
keyListener = new Object();
keyListener.onKeyDown = function():Void {
if (Key.isDown(Key.SPACE)) {
displayText(speech)
if (i < speech.length-1) {
i++;
trace(i);
} else {
break;
}
}
};
Key.addListener(keyListener);
}
//Dr Peepoo's first speech.
speak(DrPeepoo);
//Jim's Response
speak(JimJungle);
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.