createJS-未捕获的ReferenceError:未定义lib

时间:2018-10-18 23:01:12

标签: javascript createjs animate-cc

我正在使用Adobe Animate CC和CreateJS创建游戏的开始阶段,并且出现以下错误:Uncaught ReferenceError:未定义lib。基本上现在,我只是想让背景和一些随机数出现在屏幕上。我已经在Adobe Animate中将背景和随机数创建为符号,并且通过为它们分配“链接”来给它们一个类名。我已经在HTML中包含了由Animate创建的javascript文件以及指向CreateJS和以下代码的链接:

这是我的Javascript代码:

class NumberedBox extends createjs.Container 
{
  constructor(number=0)
  {
    super();
    var movieclip = new lib.NumberedBox();
    movieclip.numberedText.text = number;
    this.addChild(movieclip);

    movieclip.x = Math.random() * 200;
    movieclip.y = Math.random() * 200;
  }

}



class Game {

constructor()
{
   this.canvas = document.getElementById("canvas");
   this.stage = new createjs.Stage(this.canvas);

   createjs.Ticker.framerate = 60;
   createjs.Ticker.on("tick", this.stage);

   this.stage.addChild(new lib.Background());
   this.stage.addChild(new NumberedBox(88));
 }

}

var game = new Game();

我得到的错误是在这行代码(上面)上:     this.stage.addChild(new lib.Background());

这是CreateJS创建的代码:

    (function (cjs, an) {

    var p; // shortcut to reference prototypes
    var lib={};var ss={};var img={};
    lib.ssMetadata = [];


    // symbols:
    // helper functions:

    function mc_symbol_clone() {
    var clone = this._cloneProps(new this.constructor(this.mode, 
    this.startPosition, this.loop));
    clone.gotoAndStop(this.currentFrame);
    clone.paused = this.paused;
    clone.framerate = this.framerate;
    return clone;
    }

    function getMCSymbolPrototype(symbol, nominalBounds, frameBounds) {
    var prototype = cjs.extend(symbol, cjs.MovieClip);
    prototype.clone = mc_symbol_clone;
    prototype.nominalBounds = nominalBounds;
    prototype.frameBounds = frameBounds;
    return prototype;
    }


    (lib.RestartButton = function(mode,startPosition,loop) {
    this.initialize(mode,startPosition,loop,{});

    // Layer_1
    this.shape = new cjs.Shape();
    this.shape.graphics.f("#000000").s().p("AgiBQIAAgFIAHAAQAKAAADgGQADgEAAgNIAAh5IgTAAQgLAAgEACQgHADgEAGQgEAGgCALIgDAAIABgmICBAAIABAmIgEAAQgBgKgDgEQgDgHgGgDQgGgEgKAAIgXAAIAAB5QABAPADADQAEAFAJAAIAGAAIAAAFg");
    this.shape.setTransform(146.2,28.5);

    this.shape_1 = new cjs.Shape();
    this.shape_1.graphics.f("#000000").s().p("AAlBQIg1hLIgKABIgDAAIgEgBIAAAvQABAOADAEQAEAFAJAAIAGAAIAAAFIhEAAIAAgFIAGAAQAKAAAFgGQACgEAAgNIAAhnQAAgPgDgDQgFgFgJAAIgGAAIAAgFIA6AAQAYAAANAFQALADAJAKQAIAKAAAOQAAAOgKALQgJALgUADIAhAtQALARAIAFQAIAFANABIAAAFgAghhDIAABCIAEAAIADAAQAXAAAKgKQALgJAAgQQABgPgKgJQgJgKgPAAIgSADg");
    this.shape_1.setTransform(131.1,28.5);

    this.shape_2 = new cjs.Shape();
    this.shape_2.graphics.f("#000000").s().p("AAWBRIAAgEQAJgBAEgCQADgDAAgEQAAgFgFgLIgJgXIg9AAIgLAZQgEAJAAAFQAAAEAEACQADADAMABIAAAEIgyAAIAAgEQAKgCADgDQAFgFAIgSIA4iBIADAAIA3CDQAHAQAFAFQAGAEAJABIAAAEgAghATIA1AAIgag/g");
    this.shape_2.setTransform(114.1,28.3);

    this.shape_3 = new cjs.Shape();
    this.shape_3.graphics.f("#000000").s().p("AghBQIAAgFIAFAAQALAAAEgGQACgEAAgNIAAh5IgTAAQgLAAgFACQgGADgEAGQgFAGAAALIgFAAIACgmICBAAIABAmIgEAAQgBgKgDgEQgDgHgGgDQgGgEgKAAIgWAAIAAB5QgBAPAEADQAEAFAKAAIAFAAIAAAFg");
    this.shape_3.setTransform(98.2,28.5);

    this.shape_4 = new cjs.Shape();
    this.shape_4.graphics.f("#000000").s().p("AgJBSIgPgEQgMgEgDAAQAAAAgBAAQgBAAAAABQgBAAAAAAQgBAAAAABQgCABgBAGIgEAAIAAg3IAEAAQADASAFAIQAFAIALAHQALAFALAAQAPAAAJgHQAIgIAAgLQAAgGgDgGQgDgGgHgFIgYgPQgVgLgIgGQgJgHgEgIQgFgJAAgJQAAgRANgNQAOgMAUAAQALAAAPAGQAGADADAAQADAAACgBQACgCABgGIAEAAIAAA3IgEAAQgCgQgGgKQgFgIgKgGQgKgFgKgBQgMABgIAHQgIAHAAAKQAAAHAFAGQAHAIAaAQQAXALAIAGQAIAGAFAJQAEAJAAAJQAAASgOANQgOANgWAAIgMgBg");
    this.shape_4.setTransform(84.2,28.5);

    this.shape_5 = new cjs.Shape();
    this.shape_5.graphics.f("#000000").s().p("AhDBQIAAgFIAFAAQAGAAAFgDQAEgCABgDIABgPIAAhnQAAgPgCgEQgEgEgLAAIgFAAIAAgFIB5AAIACAjIgFAAQgCgMgDgFQgDgFgFgCQgGgCgLAAIgrAAIAAA/IAiAAQAOAAAEgEQAHgFAAgOIAFAAIAAA3IgFAAQgBgNgCgDQgDgEgEgCQgFgCgKgBIgiAAIAAA1QAAAKABADIADAEQACABAHAAIAaAAQAOAAAGgCQAGgCAGgGQAHgHAHgOIAFAAIgOAog");
    this.shape_5.setTransform(70.1,28.5);

    this.shape_6 = new cjs.Shape();
    this.shape_6.graphics.f("#000000").s().p("AAlBQIg1hLIgJABIgEAAIgEgBIAAAvQAAAOADAEQAFAFAJAAIAGAAIAAAFIhEAAIAAgFIAGAAQAKAAAEgGQADgEAAgNIAAhnQAAgPgEgDQgEgFgJAAIgGAAIAAgFIA6AAQAZAAAMAFQALADAJAKQAIAKAAAOQAAAOgJALQgKALgUADIAhAtQALARAIAFQAIAFANABIAAAFgAghhDIAABCIAEAAIADAAQAXAAAKgKQAMgJAAgQQgBgPgIgJQgKgKgPAAIgSADg");
    this.shape_6.setTransform(55.1,28.5);

    this.shape_7 = new cjs.Shape();
    this.shape_7.graphics.f("#00FFFF").s().p("AxBFKIAAqTMAiDAAAIAAKTg");
    this.shape_7.setTransform(109,33);

    this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_7},{t:this.shape_6},{t:this.shape_5},{t:this.shape_4},{t:this.shape_3},{t:this.shape_2},{t:this.shape_1},{t:this.shape}]}).to({state:[{t:this.shape_7},{t:this.shape_6},{t:this.shape_5},{t:this.shape_4},{t:this.shape_3},{t:this.shape_2},{t:this.shape_1},{t:this.shape}]},1).to({state:[{t:this.shape_7},{t:this.shape_6},{t:this.shape_5},{t:this.shape_4},{t:this.shape_3},{t:this.shape_2},{t:this.shape_1},{t:this.shape}]},1).to({state:[{t:this.shape_7},{t:this.shape_6},{t:this.shape_5},{t:this.shape_4},{t:this.shape_3},{t:this.shape_2},{t:this.shape_1},{t:this.shape}]},1).wait(1));

}).prototype = p = new cjs.MovieClip();
p.nominalBounds = new cjs.Rectangle(0,0,218,66);


    (lib.NumberedBox = function(mode,startPosition,loop) {
    this.initialize(mode,startPosition,loop,{});

    // Layer_1
    this.numberedText = new cjs.Text("98", "24px 'Times New Roman'");
    this.numberedText.name = "numberedText";
    this.numberedText.lineHeight = 29;
    this.numberedText.lineWidth = 44;
    this.numberedText.parent = this;
    this.numberedText.setTransform(33.5,36.2);

    this.shape = new cjs.Shape();
    this.shape.graphics.f("#34EDFC").s().p("AoVHgIAAu/IQrAAIAAO/g");
    this.shape.setTransform(53.4,48);

    this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape},{t:this.numberedText}]}).wait(1));

}).prototype = getMCSymbolPrototype(lib.NumberedBox, new cjs.Rectangle(0,0,106.8,96), null);


    (lib.Background = function(mode,startPosition,loop) {
    this.initialize(mode,startPosition,loop,{});

    // Layer_1
    this.shape = new cjs.Shape();
    this.shape.graphics.f("#FF00CC").s().p("EhK1AoeMAAAhQ7MCVrAAAMAAABQ7g");
    this.shape.setTransform(655,249);

    this.timeline.addTween(cjs.Tween.get(this.shape).wait(1));

}).prototype = getMCSymbolPrototype(lib.Background, new cjs.Rectangle(176,-10,958,518), null);


    (lib.GameOverView = function(mode,startPosition,loop) {
    this.initialize(mode,startPosition,loop,{});

    // Layer_1
    this.RestartButton = new lib.RestartButton();
    this.RestartButton.name = "RestartButton";
    this.RestartButton.parent = this;
    this.RestartButton.setTransform(418.2,368.6,1,1,0,0,0,109,33);
    new cjs.ButtonHelper(this.RestartButton, 0, 1, 2, false, new lib.RestartButton(), 3);

    this.shape = new cjs.Shape();
    this.shape.graphics.f("#00FFFF").s().p("AgIBPQgEgEAAgFQAAgFAEgEQAEgEAEAAQAFAAAEAEQAEAEAAAFQAAAFgEAEQgEAEgFAAQgEAAgEgEgAgBAlIgKheIgBgJQAAgHAEgFQAEgEAEAAQAGAAAEAEQADAFAAAJIAAAHIgLBeg");
    this.shape.setTransform(407.6,212.7);

    this.shape_1 = new cjs.Shape();
    this.shape_1.graphics.f("#00FFFF").s().p("AgIBPQgEgEAAgFQAAgFAEgEQAEgEAEAAQAFAAAEAEQAEAEAAAFQAAAFgEAEQgEAEgFAAQgEAAgEgEgAgBAlIgKheIgBgJQAAgHAEgFQAEgEAEAAQAGAAAEAEQADAFAAAJIAAAHIgLBeg");
    this.shape_1.setTransform(399.6,212.7);

    this.shape_2 = new cjs.Shape();
    this.shape_2.graphics.f("#00FFFF").s().p("AAGA3IAAgEIACAAQAHAAADgCQADgDABgEIABgLIAAgrQAAgPgEgGQgEgHgJAAQgNAAgNAPIAAA4QAAALABACQABAEADACQADABAJAAIAAAEIg1AAIAAgEIACAAQAIAAADgEQADgEAAgMIAAgnQAAgTgBgEQgBgFgBgBQgCgCgDAAQgEAAgEACIgCgEIAhgNIAFAAIAAAWQASgWARAAQAIAAAHAEQAGAFAEAKQACAHAAAOIAAAtQAAAKACAEQABADADABQADACAIAAIAAAEg");
    this.shape_2.setTransform(389.7,215.1);

    this.shape_3 = new cjs.Shape();
    this.shape_3.graphics.f("#00FFFF").s().p("AgnAmQgMgRAAgUQAAgOAIgOQAHgPAMgHQAMgHAMAAQAZAAAOASQANARAAAUQAAANgHAPQgHAPgMAHQgMAIgOAAQgYAAgPgTgAgPgsQgGADgEAJQgEAKAAAOQAAAXAKAQQAJARAOAAQALAAAIgJQAHgJAAgXQAAgcgMgQQgIgMgMAAQgHAAgGAFg");
    this.shape_3.setTransform(377.6,215.3);

    this.shape_4 = new cjs.Shape();
    this.shape_4.graphics.f("#00FFFF").s().p("AAlBRIgkhkIgjBkIgEAAIgwiDIgGgRQgDgEgEgDQgEgCgHAAIAAgEIA7AAIAAAEIgDAAQgGAAgDADQgDADAAAEQAAAEAFAPIAfBYIAahLIgEgNIgEgLIgFgKIgEgEIgGgDIgHgBIAAgEIA9AAIAAAEIgEAAQgHAAgDADQgDADAAAFQAAAGAFAOIAfBWIAehXQAFgOAAgGQAAgDgCgCQgBgCgDgBQgEgCgIAAIAAgEIAwAAIAAAEQgGAAgEACQgEADgEAGIgHATIgsB/g");
    this.shape_4.setTransform(360.4,212.9);

    this.shape_5 = new cjs.Shape();
    this.shape_5.graphics.f("#00FFFF").s().p("AAXA4IAAgYQgOAPgIAEQgGAFgIAAQgJgBgHgFQgGgFgDgIQgCgJAAgPIAAguQAAgIgCgDQgCgDgDgCQgDgBgJAAIAAgFIAmAAIAABHQAAAQAFAEQAGAFAHAAQAEAAAGgDQAHgDAJgKIAAg7QAAgJgEgEQgDgDgLAAIAAgFIAlAAIAABBQAAASABAEQABAFACACQAAAAABAAQAAAAABABQAAAAABAAQAAAAABAAQAEAAAEgCIACAEIggAOg");
    this.shape_5.setTransform(384.3,186.9);

    this.shape_6 = new cjs.Shape();
    this.shape_6.graphics.f("#00FFFF").s().p("AgnAlQgMgQAAgUQAAgOAIgOQAHgPAMgHQAMgHAMAAQAZAAAOATQANAQAAAUQAAANgHAPQgHAPgMAHQgMAIgOAAQgYAAgPgUgAgPgsQgGADgEAJQgEAJAAAPQAAAWAKARQAJASAOgBQALAAAIgJQAHgJAAgXQAAgcgMgQQgIgMgMAAQgHABgGAEg");
    this.shape_6.setTransform(372.3,186.7);

    this.shape_7 = new cjs.Shape();
    this.shape_7.graphics.f("#00FFFF").s().p("AghBQIAAgFIAGAAQAJAAAFgGQADgEAAgNIAAglIgshCIgKgPQgEgDgIgEIgHgBIAAgFIBEAAIAAAFIgDAAQgGAAgEACQgFADAAAFQAAAFAHALIAgAxIAggvQAHgMAAgFQgBgDgBgDIgFgDQgEgCgGAAIAAgFIA4AAIAAAFIgDAAQgDAAgGACQgGAEgFAFQgEAEgIAMIgmA7IAAAoQAAAOADAEQAFAFAJAAIAGAAIAAAFg");
    this.shape_7.setTransform(357.6,184.1);

    this.shape_8 = new cjs.Shape();
    this.shape_8.graphics.f("#000000").s().p("EhK5AooMAAAhRPMCVzAAAMAAABRPgA6lWIMAiDAAAIAAqUMgiDAAAg");
    this.shape_8.setTransform(479.4,260);

    this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.shape_8},{t:this.shape_7},{t:this.shape_6},{t:this.shape_5},{t:this.shape_4},{t:this.shape_3},{t:this.shape_2},{t:this.shape_1},{t:this.shape},{t:this.RestartButton}]}).wait(1));

}).prototype = getMCSymbolPrototype(lib.GameOverView, new cjs.Rectangle(0,0,958.8,520), null);


// stage content:
(lib.test = function(mode,startPosition,loop) {
    this.initialize(mode,startPosition,loop,{});

    // Layer_1
    this.instance = new lib.GameOverView();
    this.instance.parent = this;
    this.instance.setTransform(480.6,314,1,1,0,0,0,479.4,260);

    this.Background = new lib.Background();
    this.Background.name = "Background";
    this.Background.parent = this;
    this.Background.setTransform(1273.2,329,1,1,0,0,0,479,259);

    this.NumberedBox = new lib.NumberedBox();
    this.NumberedBox.name = "NumberedBox";
    this.NumberedBox.parent = this;
    this.NumberedBox.setTransform(1037.6,290,1,1,0,0,0,53.4,48);

    this.timeline.addTween(cjs.Tween.get({}).to({state:[{t:this.NumberedBox},{t:this.Background},{t:this.instance}]}).wait(1));

}).prototype = p = new cjs.MovieClip();
p.nominalBounds = new cjs.Rectangle(481.2,374,1927,524);
// library properties:
lib.properties = {
    id: '9B2E3308BA873548975E3DE315BD1F74',
    width: 960,
    height: 640,
    fps: 24,
    color: "#FFFFFF",
    opacity: 1.00,
    manifest: [],
    preloads: []
};



// bootstrap callback support:

(lib.Stage = function(canvas) {
    createjs.Stage.call(this, canvas);
}).prototype = p = new createjs.Stage();

p.setAutoPlay = function(autoPlay) {
    this.tickEnabled = autoPlay;
}
    p.play = function() { this.tickEnabled = true; 
    this.getChildAt(0).gotoAndPlay(this.getTimelinePosition()) }
    p.stop = function(ms) { if(ms) this.seek(ms); this.tickEnabled = false; }
p.seek = function(ms) { this.tickEnabled = true; this.getChildAt(0).gotoAndStop(lib.properties.fps * ms / 1000); }
p.getDuration = function() { return this.getChildAt(0).totalFrames / lib.properties.fps * 1000; }

    p.getTimelinePosition = function() { return 
    this.getChildAt(0).currentFrame / lib.properties.fps * 1000; }

    an.bootcompsLoaded = an.bootcompsLoaded || [];
    if(!an.bootstrapListeners) {
    an.bootstrapListeners=[];
    }

    an.bootstrapCallback=function(fnCallback) {
    an.bootstrapListeners.push(fnCallback);
    if(an.bootcompsLoaded.length > 0) {
        for(var i=0; i<an.bootcompsLoaded.length; ++i) {
            fnCallback(an.bootcompsLoaded[i]);
        }
    }
    };

    an.compositions = an.compositions || {};
    an.compositions['9B2E3308BA873548975E3DE315BD1F74'] = {
    getStage: function() { return exportRoot.getStage(); },
    getLibrary: function() { return lib; },
    getSpriteSheet: function() { return ss; },
    getImages: function() { return img; }
    };

    an.compositionLoaded = function(id) {
    an.bootcompsLoaded.push(id);
    for(var j=0; j<an.bootstrapListeners.length; j++) {
        an.bootstrapListeners[j](id);
    }
    }

    an.getComposition = function(id) {
    return an.compositions[id];
    }



    })(createjs = createjs||{}, AdobeAn = AdobeAn||{});
    var createjs, AdobeAn;

关于我在做什么错的任何想法?谢谢您的帮助!

3 个答案:

答案 0 :(得分:1)

在提供的脚本源中,lib参考仅在createjs上下文中定义,而在外部文档/ Windows上下文中未定义。您需要调用AdobeAn.getComposition(id).getLibrary()以获得对lib的引用并访问其属性(要使用的id值似乎嵌入在脚本源中)

答案 1 :(得分:0)

不同于canvas和stage属性,并不是所有地方都可以访问lib属性。实例化Game类时,必须将其作为参数发送。像这样:

   class NumberedBox extends createjs.Container  
    {  
        constructor(lib, number = 0)  
        {  
              super();  
              var movieclip = new lib.NumberedBox();  
              movieclip.numberedText.text = number;  
              this.addChild(movieclip);  

              movieclip.x = Math.random() * 200;  
              movieclip.y = Math.random() * 200;  
        }  
    }  

    class Game  
    {  
        constructor(lib)  
        {  
              this.lib = lib;  

              this.canvas = document.getElementById("canvas"); // you can call the canvas property from anywhere without doing this  
              this.stage = new createjs.Stage(this.canvas); // a stage is already created for you  

              createjs.Ticker.framerate = 60;  
              createjs.Ticker.on("tick", this.stage);  

              this.stage.addChild(new lib.Background());  
              this.stage.addChild(new NumberedBox(lib, 88));  
        }  
    }  

    var game = new Game(lib); // assuming that this line is being called from some timeline in your FLA document

这个问题的答案也可以在https://forums.adobe.com/message/10690481#中找到。感谢所有回答这个问题的人。我感谢您的帮助。 -雷蒙娜

答案 2 :(得分:0)

lib 不再可以作为全局变量访问,因此会出现错误。您可以通过修改已发布的HTML代码来存储lib的引用,也可以使用以下间接方法来访问它:

var lib = AdobeAn.getComposition(AdobeAn.bootcompsLoaded[0]).getLibrary();