如何在此代码中解决此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]);
答案 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);}