2014-01-20 161 views
-1

我是javascript新手,我試着調用下面列出的創建函數,並繼續獲取相同的錯誤,即object [object Object]不是javascript中的函數任何人都可以請幫忙。對象[對象對象]不是javascript中的函數

// This is just a sample script. Paste your real code (javascript or HTML) here. 
function init() { 

    //Marker 
    canvasMarker = document.createElement('canvas'); 
    context = canvasMarker.getContext('2d'); 

    container = document.createElement('div'); 
    document.body.appendChild(container); 
    document.body.appendChild(canvasMarker); 

    /* This is where ths problem is encountered. */ 
    detector = create(canvasMarker); 

    camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 1, 1000); 
    camera.position.set(80, 20, 10); 

    //Creates scene 
    scene = new THREE.Scene(); 

    var ambient = new THREE.AmbientLight(0x101030); 
    //scene.add(ambient); 

    var directionalLight = new THREE.DirectionalLight(0xffeedd); 
    directionalLight.position.set(0, 0, 1); 
    scene.add(directionalLight); 

    // loads texture 

    var manager = new THREE.LoadingManager(); 
    manager.onProgress = function (item, loaded, total) { 
     console.log(item, loaded, total); 
    }; 

    var texture = new THREE.Texture(); 
    var loader = new THREE.ImageLoader(manager); 
    loader.load(texturePath, function (image) { 
     texture.image = image; 
     texture.needsUpdate = true; 
     texture.transparent = true; 
    }); 

    // loads model 
    var loader = new THREE.OBJLoader(manager); 
    loader.load(objectPath, function (object) { 

     object.traverse(function (child) { 

      if (child instanceof THREE.Mesh) { 

       child.material.map = texture; 
       child.material.transparent = true; 

      } 

     }); 

     object.position.y = -100; 
     scene.add(object); 

    }); 

    //setup three.js renderer 
    renderer = new THREE.WebGLRenderer({ 
     alpha: true 
    }); 

    //canvas   
    var mycanvas = renderer.domElement; 
    mycanvas.setAttribute('id', 'Canvas3d'); 
    renderer.setSize(window.innerWidth, window.innerHeight); 
    container.appendChild(mycanvas); 
    document.addEventListener('mousemove', onDocumentMouseMove, false); 

    //window listener for resize 
    window.addEventListener('resize', onWindowResize, false); 
    video = document.getElementById('myvideo'); 

} 

function onWindowResize() { 

    windowHalfX = window.innerWidth/2; 
    windowHalfY = window.innerHeight/2; 

    camera.aspect = window.innerWidth/window.innerHeight; 
    camera.updateProjectionMatrix(); 
    renderer.setSize(window.innerWidth, window.innerHeight); 

} 

function onDocumentMouseMove(event) { 
    mouseX = (event.clientX - windowHalfX)/2; 
    mouseY = (event.clientY - windowHalfY)/2; 
} 

//Animate the model 
function animate() { 
    // The ardetector requires that we set a flag when the canvas has changed. 
    canvasMarker.changed = true; 

    // Ask the detector to make a detection pass. 
    detector.detect(onMarkerCreated, onMarkerUpdated, onMarkerDestroyed); 

    requestAnimationFrame(animate); 
    render(); 

} 

//Render the Model 
function render() { 
    camera.position.x += (mouseX - camera.position.x) * .05; 
    camera.position.y += (-mouseY - camera.position.y) * .05; 
    camera.lookAt(scene.position); 
    renderer.render(scene, camera); 
} 

* 
var create = function (sourceCanvas) { * 
    var JSARRaster = new NyARRgbRaster_Canvas2D(sourceCanvas); 
    var JSARParameters = new FLARParam(sourceCanvas.width, sourceCanvas.height); 
    var JSARDetector = new FLARMultiIdMarkerDetector(JSARParameters, 120); 
    JSARDetector.setContinueMode(true); 

    var getMarkerNumber = function (idx) { 
     var data = JSARDetector.getIdMarkerData(idx); 
     if (data.packetLength > 4) { 
      return -1; 
     } 

     var result = 0; 
     for (var i = 0; i < data.packetLength; i++) { 
      result = (result << 8) | data.getPacketData(i); 
     } 

     return result; 
    } 

    var getTransformMatrix = function (idx) { 
     var mat = new NyARTransMatResult(); 
     JSARDetector.getTransformMatrix(idx, mat); 

     var cm = new Float32Array(16); 
     cm[0] = mat.m00; 
     cm[1] = -mat.m10; 
     cm[2] = mat.m20; 
     cm[3] = 0; 
     cm[4] = mat.m01; 
     cm[5] = -mat.m11; 
     cm[6] = mat.m21; 
     cm[7] = 0; 
     cm[8] = -mat.m02; 
     cm[9] = mat.m12; 
     cm[10] = -mat.m22; 
     cm[11] = 0; 
     cm[12] = mat.m03; 
     cm[13] = -mat.m13; 
     cm[14] = mat.m23; 
     cm[15] = 1; 

     return cm; 
    } 

    var getCameraMatrix = function (zNear, zFar) { 
     var result = new Float32Array(16); 
     JSARParameters.copyCameraMatrix(result, zNear, zFar); 
     return result; 
    } 

    var persistTime = 1; 
    var newMarker = function (id, matrix) { 
     return { 
      id: id, 
      matrix: matrix, 
      age: persistTime, 
     } 
    } 

    var markers = {}; 
    var detect = function (onCreate, onUpdate, onDestroy) { 
     var markerCount = JSARDetector.detectMarkerLite(JSARRaster, 70); 
     for (var index = 0; index < markerCount; index++) { 
      var id = getMarkerNumber(index); 
      var marker = markers[id]; 
      if (marker === undefined) { 
       marker = newMarker(id, getTransformMatrix(index)); 
       markers[id] = marker; 
       onCreate(marker); 
      } else { 
       marker.matrix = getTransformMatrix(index); 
       marker.age = persistTime; 
       onUpdate(marker); 
      } 
     } 

     for (var id in markers) { 
      var marker = markers[id]; 
      if (marker) { 
       if (marker.age-- == 0) { 
        onDestroy(marker); 
        delete markers[id]; 
       } 
      } 
     } 
    } 

    return { 
     detect: detect, 
     getCameraMatrix: getCameraMatrix, 
    } 
} 

return { 
    create: create, 
} 

這是我的代碼。請幫忙。

+0

您正在Render()中創建「create()」函數作爲變量。它不會像init()那樣被外部函數訪問。將create函數移到Render()之外,init()可以看到它。 – MikeHelland

+0

這絕對看起來像一個範圍問題 - 考慮爲未來的問題修剪一些不太相關的代碼。我寫了一篇博客文章,討論了JavaScript在JavaScript中的工作原理 - http://jordanforeman.com/understanding-javascript/ –

+1

@MazeHatter創建不在reder – user2970434

回答

0

你在哪裏調用init()?

它必須在這個程序之外?

然後var創建已超出範圍。

更改var create = function(){到函數create(){和你可能有更好的運氣。

編輯:

如果,如果你的(僞)-js文件:

init(); 
function init() { } 
function create() { } 

return {create: create}; 

然後你會得到錯誤非法return語句,因爲JS文件不會 「迴歸」任何地方的價值。

在最底部的返回語句不會執行任何操作(除了拋出錯誤);

+0

在這種情況下,var create = function()尚未運行,因此未定義。將它移到init()之上或使用函數create()確保它在腳本運行之前存在 – MikeHelland

+0

在函數init之前調用init,當我將函數更改爲函數create()時會發生錯誤,告訴我illegla返回statment。你現在應該怎麼樣回覆sttsament - – user2970434

+0

我編輯了我的答案,解釋了爲什麼最終返回語句(如果不在函數中)沒有用並導致錯誤。 – MikeHelland

相關問題