简体   繁体   English

未定义的Javascript未捕获错误不是侦听器中的函数

[英]Javascript uncaught error undefined is not a function in listener

 var HelloWorldLayer = cc.Layer.extend({
sprite:null,
ctor:function(){
    var myWorld;
    this._super();
            var winsize = cc.director.getWinSize();
    myWorld = new cc.Sprite(res.HelloWorld_png);
    myWorld.attr({
        x: winsize.width / 2,
        y: winsize.height / 2,
        scale: 1,
        rotation: 180
    });
    this.addChild(myWorld, 0);
    var centerpos = cc.p(winsize.width / 2, winsize.height / 2);
    cc.eventManager.addListener(cc.EventListener.create({
        event: cc.EventListener.TOUCH_ALL_AT_ONCE,
        onTouchesBegan: function(touches, event) {
            console.log("onTouchesBegan!");
            var spritebg;
            for(var i = 0; i < 10; i++){
                spritebg = cc.Sprite.create(res.Hat_png);
                spritebg.setPosition(
                     (Math.floor(Math.random() * 600) + 100),
                     (Math.floor(Math.random() * 600) + 100));              
                //this.addChild(spritebg);
                myWorld.appendChild(spritebg);
            }   
        }
    }), this);

 }  
 });

No matter what I do, I keep getting the same error when I try to add the child when I click on the screen. 无论我做什么,当我单击屏幕时尝试添加孩子时,都会出现相同的错误。 I'm new to javascript and don't really get the language. 我是javascript的新手,并没有真正的语言。 I got the same error message when I tried adding the children inside of a function. 当我尝试将子项添加到函数中时,出现相同的错误消息。

Having had a look at the Cocos2D reference for cc.Sprite , it seems that there isn't a method appendChild on the Sprite object. cc.SpriteCocos2D参考cc.Sprite ,似乎Sprite对象上没有方法appendChild The error you are getting is because you are trying to call a method that does not exist. 您得到的错误是因为您试图调用一个不存在的方法。

Perhaps you wanted myWorld.addChild(spritebg); 也许您想要myWorld.addChild(spritebg); instead? 代替?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM