2013-02-22 264 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(); 
    //... 
    } 
} 
相关问题