0

I want to return multiple lines (one line each time the for-loop goes around), but it only returns the first line. I tried to write a function just to return the line, but I got errors with it because I don't know where to put it.

What's a good way of doing this?

for (var i = 0; i < testArray.length; i +=3) {
    geometry.vertices.push(
        new THREE.Vector3( testArray[i], testArray[i + 1], testArray[i + 2] ),
        new THREE.Vector3( testArray[i + 3], testArray[i + 4], testArray[i + 5] ));

    var line = new THREE.Line(geometry, material);

    return line;
    // or if using function, returnLine(line);
}

Attempted function:

function returnLine(line) {
    return line;
}
  • 4
    `return` ends the function. Are you trying to build an array and return that? – Izkata Jun 05 '15 at 16:55
  • I don't think so. I want to return a geometry, but there are multiple geometries that I'm trying to return. 'testArray' is an array of coordinates, so the first Vector3 is one point, and the second Vector3 is another point. There is a line connecting the two. –  Jun 05 '15 at 16:57
  • Maybe something like this? http://stackoverflow.com/questions/2282140/whats-the-yield-keyword-in-javascript – Panu Oksala Jun 05 '15 at 16:58
  • What is the end goal? What is the calling function expected to retrieve? And what is this `returnLine` function? Do you think calling a function will cause the current function to do the return? That's not how it works... – Corey Ogburn Jun 05 '15 at 17:00
  • Why are you creating multiple lines meshes with only 2 points each? Why not one line mesh with many points? – WestLangley Jun 05 '15 at 17:07
  • I don't know if this is what you want, but because you never empty out the geometry vertices in the loop each "line" you create will contain all previous lines. Maybe you could tell us a little more about the big picture and what you're trying to do and exactly what's not working. – Corey Ogburn Jun 05 '15 at 17:11
  • The file that I'm trying to read renders in loops, so it's not one long line. @WestLangley: I don't know if that's what you mean by one line mesh, but if I'm understanding correctly that's why I didn't do that. –  Jun 05 '15 at 17:24
  • Your three.js code makes no sense to me. – WestLangley Jun 05 '15 at 17:35

3 Answers3

3

You can only return one value. The first call of return ends your function. You cannot solve this by wrapping your return statement in another function, either.

Wrap the values you wish to return in an array, this allows you to access all of them in the calling function:

var output = new Array();

for (var i = 0; i < testArray.length; i +=3) {
    geometry.vertices.push(
        new THREE.Vector3( testArray[i], testArray[i + 1], testArray[i + 2] ),
        new THREE.Vector3( testArray[i + 3], testArray[i + 4], testArray[i + 5] ));

    var line = new THREE.Line(geometry, material);

    output.push(line);
}

return output;

This way, you can process your output in the calling function by looping through the returned array and processing each line.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
1
var lines = [];
for (var i = 0; i < testArray.length; i +=3) {
    geometry.vertices.push(
        new THREE.Vector3( testArray[i], testArray[i + 1], testArray[i + 2] ),
        new THREE.Vector3( testArray[i + 3], testArray[i + 4], testArray[i + 5] ));
    var line = new THREE.Line(geometry, material);
    lines.push(line);
}
return lines;
bhspencer
  • 13,086
  • 5
  • 35
  • 44
-1

return keyword used in functions, you can still loop testArray using map array method

var testArray = [1, 2, 3], //whatever
newArray = [];

newArray = testArray.map(function(element, i){
geometry.vertices.push(
    new THREE.Vector3( testArray[i], testArray[i + 1], testArray[i + 2] ),
    new THREE.Vector3( testArray[i + 3], testArray[i + 4], testArray[i + 5] ));

var line = new THREE.Line(geometry, material);

return line;

});

function returnLine(index) {
    return newArray[ index || 0 ];
};
KQI
  • 322
  • 1
  • 16