创建JS功能添加到舞台

时间:2016-01-21 14:48:32

标签: javascript createjs

在createjs中,我试图向舞台添加元素,但只有在被函数调用时才会这样。

如果我将此代码添加到我的onload init()函数中,则代码可以工作:

 function init()
{
 background = new createjs.Shape();
 background.graphics.beginLinearGradientFill(["blue", "darkgreen"], [0, 1], 0, 0, 100, 100).drawRect(0, 0, 70, 70);
 background.x = 0;
 background.y = 0;  
 stage.addChild(background);
}

但是如果我像

那样添加它
function init()
{
loadScreen();
}
function loadScreen()
{
background = new createjs.Shape();
 background.graphics.beginLinearGradientFill(["blue", "darkgreen"], [0, 1], 0, 0, 100, 100).drawRect(0, 0, 70, 70);
 background.x = 0;
 background.y = 0;  
 stage.addChild(background);
}

我可能只是在做一些愚蠢的事情,任何提示都会受到赞赏。

谢谢,

3 个答案:

答案 0 :(得分:2)

在loadScreen()函数defenition

之后删除分号(;)
function init()
 {
  loadScreen();
 }
 function loadScreen()
 {
   background = new createjs.Shape();
    background.graphics.beginLinearGradientFill(["blue", "darkgreen"], [0, 1], 0, 0, 100, 100).drawRect(0, 0, 70, 70);
    background.x = 0;
    background.y = 0;  
    stage.addChild(background);

}

答案 1 :(得分:1)

我可以看到你删除了这一行 - >

res = {}
for row in results:
    name = row[0]
    title = row[1]
    vote = row[2]
    res.get(name, res.update({name: {title: vote}}).update(title: vote})

您是否尝试在init()中实例化背景,然后将对象传递给加载函数?

答案 2 :(得分:1)

确保声明变量。

E.g。 window.onload = function(){ var stage; var background; function init(){ // create reference to stage and add 'tick' listener. } function loadScreen(){ // draw something onto the stage. } };

确保它们是全球性的。

.cancel()

查看此网站以获取示例:http://createjs.com/docs/easeljs/modules/EaselJS.html