我正在尝试创建一个菜单层,但在 Chrome 控制台上出现此错误:
MenuLayer is not defined
但这是我的 menu.js,它定义了 MenuLayer:
var MenuLayer = cc.LayerColor.extend({
ctor : function(){
//1. call super class's ctor function
this._super();
cc.associateWithNative( this, cc.LayerColor );
},
onEnter:function () {
this._super();
//2. get the singleton director
var director = cc.Director.getInstance();
//3. get the screen size of your game canvas
var winsize = director.getWinSize();
//4. calculate the center point
var centerpos = cc.p(winsize.width / 2, winsize.height / 2);
//5. create a background image and set it's position at the center of the screen
//var spritebg = cc.Sprite.create(s_HelloBG);
//spritebg.setPosition(centerpos);
//this.addChild(spritebg);
//6.create a menu and assign onPlay event callback to it
cc.MenuItemFont.setFontSize(60);
var menuItemPlay= cc.MenuItemSprite.create(
cc.Sprite.create(s_start_n),
cc.Sprite.create(s_start_s),
this.onPlay, this);
var menu = cc.Menu.create(menuItemPlay);
menu.setPosition(centerpos);
this.addChild(menu);
},
//this is the callback when the menu is clicked
onPlay : function(){
cc.log("==onplay clicked");
var director = cc.Director.getInstance();
director.replaceScene(new MainLayer());
}
});
MenuLayer.create = function () {
var sg = new MenuLayer();
if (sg ) {
return sg;
}
return null;
};
MenuLayer.scene = function () {
var scene = cc.Scene.create();
var layer = MenuLayer.create();
scene.addChild(layer);
return scene;
};
这是我的代码,我分别引用 menu.js 和 starter Scene:
engineDir:'./Platform/HTML5/cocos2d/',
appFiles:[
'./Src/resource.js',
'./Src/MainLayer.js',
'./Src/GameOver.js',
'./Src/main.js',
'./Src/menu.js'
]
var myApp = new cocos2dApp(MenuLayer.scene);