简体   繁体   中英

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. 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. The error you are getting is because you are trying to call a method that does not exist.

Perhaps you wanted myWorld.addChild(spritebg); instead?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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