2013-02-22 263 views
2

這裏我有一個非常簡單的腳本。即使我鏈接到文件,我也會收到錯誤「createjs is not defined」

stage = new createjs.Stage("myCanvas"); 

circle = new createjs.Shape(); 
circle.graphics.beginFill("red").drawCircle(0,0,40); 

而我的HTML

<!DOCTYPE HTML> 

<html> 
<head> 

<link rel="stylesheet" type="text/css" href="mainStyle.css"> 
<script src="mainScript.js"></script> 
<script src="create.js"></script> 

</head> 
<body> 

    <canvas id="myCanvas" width=1000 height=500 ></canvas> 

</body> 
</html> 

然而,這是行不通的。我得到這個錯誤:

"createjs is not defined"

但在我的HTML它鏈接到文件。還有什麼可能導致這個?

+0

你可以發佈鏈接到該文件的代碼嗎? – 2013-02-22 03:56:16

+0

將其修改爲包含HTML – EpicPineapple 2013-02-22 03:58:54

回答

4

您的代碼有幾個問題。

首先,爲了直接回答這個問題,您必須加載構建createjs對象的庫,然後加載實例化它的腳本。改變你頭腦中的腳本標籤的順序。

其次,您的實例化將在canvas元素添加到DOM之前執行。爲了解決這個問題,你需要監聽文檔的onload事件。然後執行你的代碼。

第三,要獲得實際顯示的紅色圓形對象,您需要將其附加到畫布上,然後調用階段更新方法。

例:

window.onload = function(){ 
    stage = new createjs.Stage("myCanvas"); 

    circle = new createjs.Shape(); 
    circle.graphics.beginFill("red").drawCircle(0,0,40); 

    stage.addChild(circle); 

    stage.update(); 
} 
5
<script src="create.js"></script> 
<script src="mainScript.js"></script> 

您的代碼需要執行後,包括create.js - 訂單事宜。

+0

此作品!非常感謝 – EpicPineapple 2013-02-22 04:05:05

0

對於角度的項目上面的答案可能不足以解決問題。我做了什麼:

myAngularModule.service('preloadservice', function ($rootScope) { 

    if (typeof createjs === 'undefined') { 
    setTimeout(continueAfterWait, 500); 
    } else { 
    continueAfterWait(); 
    } 

    var continueAfterWait = function() { 
    var queue = new createjs.LoadQueue(); 
    //... 
    } 
} 
相關問題