创建 JS 函数添加到舞台

Create JS function add to stage

在 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);
}

我可能只是在做一些愚蠢的事情,感谢任何提示。

谢谢,

加载 Screen() 函数定义后删除分号 (;)

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);

}

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

 myGraphics.beginLinearGradientFill(["#000","#FFF"], [0, 1], 0, 20, 0,   120).drawRect(20, 20, 120, 120);

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

确保声明了变量。

例如 var stage; var background;

并确保它们是全局的。

window.onload = function(){
    var stage;
    var background;

    function init(){
        // create reference to stage and add 'tick' listener.
    }
    function loadScreen(){
        // draw something onto the stage.
    }
};

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