一.环境
配置环境需要,java-jdk,vs2012,cocos2d-x_3.0源码,android_sdk,ant,android_ndkr9d
二.场景
写了3个场景,GameScene,HelloWorldScene,AboutScene,场景跳转通过导演类:
Director::getInstance()->replaceScene(TransitionFlipY::create(1,GameScene::createScene()));
通过导演类的得到实例方法的替换场景方法可以替换场景,TransitionFlipY可以封装场景,并以绕Y轴动画方式进行跳转
场景类是继承自Layer类的
每个场景类要实现三大基本方法
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
HelloWorldScene是创建工程时候生成的,把它的背景图片改了,并创建menu进行场景跳转的触发
创建menu的方法,总结用到的两种
/* 创建图片按钮
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
*/
//新增菜单条目
auto gameItem=MenuItemFont::create("Start Game",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
auto aboutItem=MenuItemFont::create("About",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
auto exit=MenuItemFont::create("Exit",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
创建的Menu可以添加tag,可以由本类的this->getChildByTag()方法获得
创建场景之后添加方法如下:
auto menu = Menu::create(gameItem,aboutItem,exit,NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
背景图片是一个精灵,在游戏里大多元素都是由精灵组成的
// splash screen"
auto sprite = Sprite::create("back.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
改变了背景图片,要把窗口的大小也改变成背景图片的大小才比较顺眼
在AppDelegate.cpp类中有如下代码
if(!glview) {
glview = GLView::create("Plane Fighting");
director->setOpenGLView(glview);
}
点击create方法并转到定义,在定义中有
GLView* GLView::create(const std::string& viewName)
{
auto ret = new GLView;
if(ret && ret->initWithRect(viewName, Rect(0, 0, 400, 480), 1)) {
ret->autorelease();
return ret;
}
return nullptr;
}
改变Rect中的后两个参数就能改变屏幕的大小了
菜单场景基本完成了。顶上的文字是一个Label,左下角的文字是AppDelegate.cpp内自动生成的,看注释可以把它删掉。
菜单的选择在菜单的创建的时候调用了CC_CALLBACK_1函数加载menuCloseCallback函数进行处理了
菜单被点击的时候就会调用下面方法进行处理
void HelloWorld::menuCloseCallback(Ref* pSender)
{
MenuItem *nowItem =(MenuItem*)pSender;
//播放音效
SimpleAudioEngine::getInstance()->playEffect("button.mp3");
switch(nowItem->getTag())
{
case 9:{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
break;
}
case 10:{//game
Director::getInstance()->replaceScene(TransitionFlipY::create(1,GameScene::createScene()));
break;
}
case 11:{//about
Director::getInstance()->replaceScene(TransitionFlipY::create(1,AboutScene::createScene()));
break;
}
}
}