正确分配每个值的循环
For loop where each value is distributed correctly
我有一个 for 循环,我想在其中跨时间线分配一定数量的相同长度的单词。
我有一些变量可以告诉我每个文本的持续时间、单词出现的开始位置和结束位置。
var wordArray = [];
for (t = 0; t < words.length; t++) {
//This generates the textbox
wordArray[wordArray.length] = currentComp.layers.addText(words[t]);
if (t == 0) {
//This makes the first textbox appear in the start
wordArray[wordArray.length - 1].inPoint = start;
//This makes the first textbox have the length with the value that divides it by the number of words
wordArray[wordArray.length - 1].outPoint = wordArray[wordArray.length - 1].inPoint + textTime;
//Here I am trying to make them all evenly distributed by multiplying the divided duration for the number of words that are already existing
} else if (t < words.length - 2) {
wordArray[wordArray.length - 1].inPoint = start + textTime * (wordArray.length - 1);
wordArray[wordArray.length - 1].outPoint = end - textTime * (wordArray.length - 1);
} else {
//This is here to make the last word stop at the end of the text duration.
wordArray[wordArray.length - 1].inPoint = start + textTime * (wordArray.length - 1);
wordArray[wordArray.length - 1].outPoint = wordArray[wordArray.length - 1].inPoint + textTime;
}
}
错误出现在最后,如最后的 image.The 单词(以白色突出显示)的长度比前一个单词的长度短,倒数第二个单词的长度比其他所有单词的长度都长单词。
我知道这不是语法问题,而是我似乎无法弄清楚的数学问题。提前致谢
编辑:找到答案并更新语法以匹配它。
我更新了语法以匹配正确的代码。
事实证明问题是每个单词的间距计算不正确。
我通过计算每个部分的开始和结束来让它工作:
var startDurationString = timeCalc[0].split(":");
var finalStartDurationString = startDurationString[1] + ":" + startDurationString[2] + ":" + startDurationString[3];
var startDuration = currentFormatToTime(finalStartDurationString, currentComp.frameRate);
var endDurationString = timeCalc[1].split(":");
var finalEndDurationString = endDurationString[1] + ":" + endDurationString[2] + ":" + endDurationString[3];
var endDuration = currentFormatToTime(finalEndDurationString, currentComp.frameRate);
然后,我用开始和结束的差除以单词的长度。
durationForEachText = (endDuration-startDuration) / numberOfWords.length;
希望对大家有所帮助。
我有一个 for 循环,我想在其中跨时间线分配一定数量的相同长度的单词。
我有一些变量可以告诉我每个文本的持续时间、单词出现的开始位置和结束位置。
var wordArray = [];
for (t = 0; t < words.length; t++) {
//This generates the textbox
wordArray[wordArray.length] = currentComp.layers.addText(words[t]);
if (t == 0) {
//This makes the first textbox appear in the start
wordArray[wordArray.length - 1].inPoint = start;
//This makes the first textbox have the length with the value that divides it by the number of words
wordArray[wordArray.length - 1].outPoint = wordArray[wordArray.length - 1].inPoint + textTime;
//Here I am trying to make them all evenly distributed by multiplying the divided duration for the number of words that are already existing
} else if (t < words.length - 2) {
wordArray[wordArray.length - 1].inPoint = start + textTime * (wordArray.length - 1);
wordArray[wordArray.length - 1].outPoint = end - textTime * (wordArray.length - 1);
} else {
//This is here to make the last word stop at the end of the text duration.
wordArray[wordArray.length - 1].inPoint = start + textTime * (wordArray.length - 1);
wordArray[wordArray.length - 1].outPoint = wordArray[wordArray.length - 1].inPoint + textTime;
}
}
错误出现在最后,如最后的 image.The 单词(以白色突出显示)的长度比前一个单词的长度短,倒数第二个单词的长度比其他所有单词的长度都长单词。
我知道这不是语法问题,而是我似乎无法弄清楚的数学问题。提前致谢
编辑:找到答案并更新语法以匹配它。
我更新了语法以匹配正确的代码。
事实证明问题是每个单词的间距计算不正确。
我通过计算每个部分的开始和结束来让它工作:
var startDurationString = timeCalc[0].split(":");
var finalStartDurationString = startDurationString[1] + ":" + startDurationString[2] + ":" + startDurationString[3];
var startDuration = currentFormatToTime(finalStartDurationString, currentComp.frameRate);
var endDurationString = timeCalc[1].split(":");
var finalEndDurationString = endDurationString[1] + ":" + endDurationString[2] + ":" + endDurationString[3];
var endDuration = currentFormatToTime(finalEndDurationString, currentComp.frameRate);
然后,我用开始和结束的差除以单词的长度。
durationForEachText = (endDuration-startDuration) / numberOfWords.length;
希望对大家有所帮助。