最近的博客更新总是跟不上进度,看来想要补上之前的博客还时需要利用更多时间的,下面让我继续写吧。
Cocos中的核心类
1.Application 应用程序类入口
class AppDelegate:public Application
applicationDidFinishLunching() —应用程序初始化完成
applicationEnterBackground() —程序进入后台
applicationResumeForGround()—程序恢复到前台
2.Director 导演类
场景显示、切换
事件分发
动作执行
计划任务的处理
3.Scene 场景类
Layer 图层类
Menu 菜单层
Label 文字层
Sprite 图片层
4.图层属性
1.坐标
2.锚点
3.缩放
4.旋转
5.tag
6.添加子图层
7.计划任务 游戏逻辑
8.执行动作
---------------------------------
下面来写一个实例:
在一个场景中添加多个图层
添加一个名为HelloWorld.png的图片
auto sp=Sprite::create("HelloWorld.png"); //生成一个图片sp(这个可以自己随意取名)
sp->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/2)); //设置图片在屏幕中的位置,这里 设为屏幕中心
this->addChild(sp); //在场景中添加这个图片
---------------------------------
在场景中添加文字层,文字为"Hello World"
auto label=Label::createWithSystemFont("Hello World","",40); //使用的是系统字体,字号为40
label->setPosition(Vec2(200,400)); //坐标设置为(200,400)
label->setColor(Color3B(255,255,255)); //设置文字的颜色,这三个255代表白色
this->addChild(label);
-----------------------------------
在场景中添加菜单层 菜单按钮有开始(start) 帮助(help) 关于(about) 退出(exit) 采用文字按钮
//获取屏幕尺寸
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto start = MenuItemFont::create("start",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
start->setColor(Color3B(255,0,0)); //设置字体颜色
start->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/3));
start->setTag(100); //给按钮做标记,方便以后使用
auto help = MenuItemFont::create("help",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
help->setColor(Color3B(0,255, 0));
help->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/3 -40));
help->setTag(101);
auto about = MenuItemFont::create("about",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
about->setColor(Color3B(0,0, 255));
about->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/3 -80));
about->setTag(102);
auto exit = MenuItemFont::create("exit",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
exit->setColor(Color3B(0,0,0));
exit->setPosition(Vec2(origin.x+visibleSize.width/2,origin.y+visibleSize.height/3 -120));
exit->setTag(103);
//在场景中添加菜单,菜单中包涵这几个按钮
auto menu = Menu::create(start,help,about,exit,NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu,1);