从CoffeeScript中的函数返回一个对象

时间:2017-07-27 19:46:29

标签: object three.js coffeescript

我正在编写一段应该为3D场景制作动画的代码。我这样做,使用类和函数在代码中整齐地构造3D场景。这本身就是一个文本输出的东西。

逻辑上它不需要吐出文本。图书馆Threejs应该与我建立的东西互动。

我想做这样的事情,例如:

class Tile extends SuperSpace
  height: 2
  sideLength: 10
class Plain extends Tile
  constructor: ( { @color = 'lightgreen', @height = @height, @heightPlacement = 2 } = {} ) ->
    console.log """
                  New plain:
                    color: '#{@color}'
                    sideLength: #{@sideLength}
                    height: #{@height}
                    heightPlacement: #{@heightPlacement}
                """
  mesh: ->
    geometry = new THREE.BoxGeometry 10/10, 2/10, 10/10
    material = new THREE.MeshBasicMaterial { color: 0x22ff22 }
    cube = new THREE.Mesh geometry, material
    return cube

在场景中调用它就像这样:

scene.add plain.mesh

这与文档中现有的更改示例无关。 plain.mesh应为返回的cube

不知何故,对象没有得到低谷。我得到undefined或者我把整个功能放回控制台:

function () {
      var cube, geometry, material;
      geometry = new THREE.BoxGeometry(10 / 10, 2 / 10, 10 / 10);
      material = new THREE.MeshBasicMaterial({
        color: 0x22ff22
      });
      cube …

难以满足。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这有效:

class Tile extends SuperSpace
  height: 2
  sideLength: 10
class Plain extends Tile
  constructor: ( { @color = 'lightgreen', @height = @height, @heightPlacement = 2 } = {} ) ->
    console.log """
                  New plain:
                    color: '#{@color}'
                    sideLength: #{@sideLength}
                    height: #{@height}
                    heightPlacement: #{@heightPlacement}
                """
  mesh: ->
    geometry = new THREE.BoxGeometry 10/10, 2/10, 10/10
    material = new THREE.MeshBasicMaterial { color: 0x22ff22 }
    cube = new THREE.Mesh geometry, material
    return cube

plain = new Plain()
console.log plain.mesh()