你如何在这段代码中的文本之间暂停?
How do you pause between text in this code?
我想在“很久以前”和之后的内容之间暂停一下,但我在尝试这样做时遇到了麻烦。我怎样才能让它发挥作用?我尝试过使用计时器,但我不太擅长这样做,因此很难实现。
这是我的代码:
var snd: textStory = new textStory();
var snd2: soundStory = new soundStory();
var myString: String = "Long ago, two races\nruled over Earth:\nHUMANS and MONSTERS.";
var myArray: Array = myString.split("");
snd2.play();
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event: Event): void {
if (myArray.length > 0) {
if (n == 1) {
tf.appendText(myArray.shift());
n = 0;
snd.play();
} else {
n++;
}
} else {
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
我想是这样的。这是一个 data-driven 解决方案,您可以在其中准备适当的数据输入,以便您的主循环(ENTER_FRAME 处理程序在您的情况下)不必考虑太多一次只处理一个数据条目。
var E:Array = [""];
// It is considered a good tone to name classes
// starting with uppercase: TextStory, SoundStory.
var snd: textStory = new textStory();
var snd2: soundStory = new soundStory();
var laString:String = "Long ago";
var myString:String = ", two races\nruled over Earth:\nHUMANS and MONSTERS.";
var myArray:Array = new Array;
// Here we form an Array that starts with "Long ago"
// then 20 empty entries to skip, then the rest.
myArray = myArray.concat(tocharArray(laString));
myArray = myArray.concat(emptySpaces(20));
myArray = myArray.concat(tocharArray(myString));
snd2.play();
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event: Event): void
{
if (myArray.length < 1)
{
removeEventListener(Event.ENTER_FRAME, frameLooper);
return;
}
var aChar:String = myArray.shift();
// Just skip a frame if there's an empty character.
if (aChar == "")
{
return;
}
tf.appendText(aChar);
// Make no typing sound on space characters... I guess?
if (aChar != " ")
{
snd.play();
}
}
// Returns an Array ["", "", "", ... , ""] of the given length.
function emptySpaces(length:int):Array
{
// This doubles E as many times as needed
// to allow us to make a slice long enough.
while (E.length < length)
{
E = E.concat(E);
}
// Returns a portion of E of the given length.
return E.slice(0, length);
}
// Converts the given String into an Array
// of characters and empty "" entries.
function tocharArray(value:String):Array
{
var result:Array = new Array;
for (var i:int = 0; i < value.length; i++)
{
// You can put more or less "" here to make the
// typing loop skip more than a single
// frame after typing a character.
result.push(value.charAt(i), "", "");
}
return result;
}
我想在“很久以前”和之后的内容之间暂停一下,但我在尝试这样做时遇到了麻烦。我怎样才能让它发挥作用?我尝试过使用计时器,但我不太擅长这样做,因此很难实现。
这是我的代码:
var snd: textStory = new textStory();
var snd2: soundStory = new soundStory();
var myString: String = "Long ago, two races\nruled over Earth:\nHUMANS and MONSTERS.";
var myArray: Array = myString.split("");
snd2.play();
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event: Event): void {
if (myArray.length > 0) {
if (n == 1) {
tf.appendText(myArray.shift());
n = 0;
snd.play();
} else {
n++;
}
} else {
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
我想是这样的。这是一个 data-driven 解决方案,您可以在其中准备适当的数据输入,以便您的主循环(ENTER_FRAME 处理程序在您的情况下)不必考虑太多一次只处理一个数据条目。
var E:Array = [""];
// It is considered a good tone to name classes
// starting with uppercase: TextStory, SoundStory.
var snd: textStory = new textStory();
var snd2: soundStory = new soundStory();
var laString:String = "Long ago";
var myString:String = ", two races\nruled over Earth:\nHUMANS and MONSTERS.";
var myArray:Array = new Array;
// Here we form an Array that starts with "Long ago"
// then 20 empty entries to skip, then the rest.
myArray = myArray.concat(tocharArray(laString));
myArray = myArray.concat(emptySpaces(20));
myArray = myArray.concat(tocharArray(myString));
snd2.play();
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event: Event): void
{
if (myArray.length < 1)
{
removeEventListener(Event.ENTER_FRAME, frameLooper);
return;
}
var aChar:String = myArray.shift();
// Just skip a frame if there's an empty character.
if (aChar == "")
{
return;
}
tf.appendText(aChar);
// Make no typing sound on space characters... I guess?
if (aChar != " ")
{
snd.play();
}
}
// Returns an Array ["", "", "", ... , ""] of the given length.
function emptySpaces(length:int):Array
{
// This doubles E as many times as needed
// to allow us to make a slice long enough.
while (E.length < length)
{
E = E.concat(E);
}
// Returns a portion of E of the given length.
return E.slice(0, length);
}
// Converts the given String into an Array
// of characters and empty "" entries.
function tocharArray(value:String):Array
{
var result:Array = new Array;
for (var i:int = 0; i < value.length; i++)
{
// You can put more or less "" here to make the
// typing loop skip more than a single
// frame after typing a character.
result.push(value.charAt(i), "", "");
}
return result;
}