简体   繁体   English

迭代数组中的3个项目

[英]Iterate through 3 items in array

I have the following array: 我有以下数组:

var text = [
    "RISE AND SHINE, JONATHON! HMM, ", 
    "THAT'S A MOUTHFUL, HOW ABOUT ", 
    "I JUST CALL YOU JOHN? MY ", 
    "NAME IS PROFESSOR ", 
    "PHIL. I BUILT YOU FROM ", 
    "SCRATCH. I WANT TO TELL YOU ",
    "A LITTLE STORY BEFORE WE START."
];

I then loop through the array: 然后我遍历数组:

for (var i = 0; i < 3; i++) {
    this.font.draw(text[i]);
}

This obviously outputs: 这明显输出:

RISE AND SHINE, JONATHON! HMM,
THAT'S A MOUTHFUL, HOW ABOUT 
I JUST CALL YOU JOHN? MY

What I need help with is creating a function that will iterate through the next 3 lines to then display: 我需要帮助的是创建一个函数,它将迭代接下来的3行然后显示:

NAME IS PROFESSOR
PHIL. I BUILT YOU FROM
SCRATCH. I WANT TO TELL YOU

..and so forth. ......等等。

This is for the chat bubble code I'm writing for a JavaScript/HTML5 game where my chat dialogue only accommodates 3 lines of text at any given time. 这是我为JavaScript / HTML5游戏编写的聊天气泡代码,我的聊天对话在任何给定时间只能容纳3行文本。

Once the dialogue has reached the last sentence. 一旦对话达到最后一句话。 I would like it to continue from the beginning. 我希望它从一开始就能继续下去。

I think you need a generic method, Probably this fiddle should help. 我认为你需要一个通用的方法,可能这个小提琴应该有所帮助。
Here's the JS Method. 这是JS方法。

var text = [
    "RISE AND SHINE, JONATHON! HMM, ",
    "THAT'S A MOUTHFUL, HOW ABOUT ",
    "I JUST CALL YOU JOHN? MY ",
    "NAME IS PROFESSOR ",
    "PHIL. I BUILT YOU FROM ",
    "SCRATCH. I WANT TO TELL YOU ",
    "A LITTLE STORY BEFORE WE START."
];
var len=3;
for(var i=0;i<text.length;i+=len)
{
  for(var j=0;j<len && (i+j)<text.length;j++)
  {
      $('#text').html($('#text').html()+text[i+j]+'<br/>');
  }
  $('#text').html($('#text').html()+'<br/><br/>');
}

If I understand correctly what you could do is backup your array and splice it till all lines have been read. 如果我正确理解你可以做的是备份你的阵列并splice它直到读完所有行。 In other words: 换一种说法:

var textOut = text,
    len = text.length / 3,
    i = 0;
for ( ; i < len; i++ ) {
  setTimeout(function() {
    console.log( textOut.splice(0, 3).join(' ') );
  }, i*1000 );
}

The above will log 3 lines every second until all lines are read. 以上将每秒记录3行,直到读取所有行。
By the way you don't need the spaces at the end of each string since you can use join with spaces. 顺便说一句,你不需要的空间在每个字符串的结尾,因为你可以使用join有空格。

Demo: http://jsbin.com/atenib/9/edit 演示: http //jsbin.com/atenib/9/edit

I'm not quite sure what the point of this is, but you could just create a simple function: 我不太清楚这是什么意思,但你可以创建一个简单的函数:

function display_text(start, end) {
    for (var i = start; i < end; i++) {
        this.font.draw(text[i]);
    }
}

then you would call it 3 times: 然后你会称它为3次:

display_text(0, 2)
display_text(3, 5)
display_text(6, 6)

If I'm imagining your chat bubbles correctly something like this may work for you: 如果我正确想象你的聊天气泡,这样的事情对你有用:

    var text = [
    "RISE AND SHINE, JONATHON! HMM, ", 
    "THAT'S A MOUTHFUL, HOW ABOUT ", 
    "I JUST CALL YOU JOHN? MY ", 
    "NAME IS PROFESSOR ", 
    "PHIL. I BUILT YOU FROM ", 
    "SCRATCH. I WANT TO TELL YOU ",
    "A LITTLE STORY BEFORE WE START."
];

var max_lines = 3;

var length = text.length;

num_bubbles = length/max_lines; 
if (!(length%max_lines)){
    num_bubbles = num_bubbles + 1;
}


var print_bubble = function(i,text){
    console.log("***start of text bubble****");
    for(j=i*max_lines; j<length && j<(i+1)*max_lines; ++j){
        console.log(text[j] + '\n');//print text bubble contents
    }
}

for (var i = 0; i <num_bubbles ;++i){
    print_bubble(i, text);
}​

It's not a clean solution but works: JS Fiddle here 这不是一个干净的解决方案,但有效: JS小提琴在这里

document.getElementById("output").innerHTML= "";
var text = [
    "RISE AND SHINE, JONATHON! HMM, ", 
    "THAT'S A MOUTHFUL, HOW ABOUT ", 
    "I JUST CALL YOU JOHN? MY ", 
    "NAME IS PROFESSOR ", 
    "PHIL. I BUILT YOU FROM ", 
    "SCRATCH. I WANT TO TELL YOU ",
    "A LITTLE STORY BEFORE WE START."
];
var counter = 0;
var s = 0;  //start index
var e = 0;  //end index
var l = text.length;
var out = "";  //just for testing/debugging

while(counter < 15)  //to prevent the browser from freezing/infinite loop
{
  if(e+3 < l)  //take 3 items if we have more than 3 left
      e = e +3;
  else
      e = l;  //else use the remaining

  for (var i = s; i < e; i++) {
    out = (text[i])+"<br>";
      document.getElementById("output").innerHTML+=out;

  }
document.getElementById("output").innerHTML+="<HR>";  //separates after 3 items
s = e;
if(e == l) //reset: start from zero
{
    s = 0;
    e = 0;
}

  counter++;    
}
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM