如何在cocos2d-html中使用removeChild

时间:2015-04-07 10:57:48

标签: javascript cocos2d-iphone undefined removechild cocos2d-js

如何在此代码中解决此undefined gameLayer错误? 它一直说gameLayer未定义

var pause = setTimeout(function(){
    if(pickedTiles[0].pictureValue!=pickedTiles[1].pictureValue){
        pickedTiles[0].initWithFile("assets/cover.png");
        pickedTiles[1].initWithFile("assets/cover.png");
    }
    else{
        gameLayer.removeChild(pickedTiles[0]);
        gameLayer.removeChild(pickedTiles[1]);
    }
    pickedTiles = [];
}, 1000);}

这是完整的代码 https://www.dropbox.com/s/jfp6fudg31ojyfp/gameScript.js?dl=0


解决,使用

pickedTiles[0].removeFromParent();
pickedTiles[1].removeFromParent();

而不是

 gameLayer.removeChild(pickedTiles[0]);
 gameLayer.removeChild(pickedTiles[1]);

1 个答案:

答案 0 :(得分:0)

如果gameLayer存在,您可以检查:

var pause = setTimeout(function(){
    if(pickedTiles[0].pictureValue!=pickedTiles[1].pictureValue){
        pickedTiles[0].initWithFile("assets/cover.png");
        pickedTiles[1].initWithFile("assets/cover.png");
    }
    else{
        if(gameLayer){
            gameLayer.removeChild(pickedTiles[0]);
            gameLayer.removeChild(pickedTiles[1]);
        }
    }
    pickedTiles = [];
}, 1000);}