这篇主要介绍Sprite类,在介绍这个类之前,先设计一个点开游戏进入游戏的主界面。
只修改MainScene.cpp中的init函数
其中用到了C++标准容器vector创建多个menuItemLabel,这个懂C++的还比较好理解。
有点匪夷所思的是Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2).x利用这种方式获取左边点的X左边,它不就是等于origin.x + visibleSize.width/2。
还有就是提供的例子上,设计的字体大小不大合适,一个屏幕装不下这么多item,把字体改小了,可以设计一个滑动的当做拓展。
#include "MainScene.h"
#include "Sprites/Sprites.h"
USING_NS_CC;
Scene *MainScene::createScene() {
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = MainScene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// On "init" you need to initialize your instance
bool MainScene::init()
{
// Super init first
if (!Node::init())
return false;
Size visibleSize = Director::getInstance()->getVisibleSize();
// Return visible origin coordinate of the OpenGL view in points
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// Add a menu item with "X" image, which is clicked to quit the program
// Add a "close" icon to exit the progress, it's an autorelease object
auto closeItem = MenuItemImage::create("CloseNormal.png",
"CloseSelected.png", CC_CALLBACK_1(MainScene::onMenuCloseCallback, this));
// getContentsSize()
// Returns the untransoformed size of the node
// default value (0.5, 0.5)
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto closeMenu = Menu::create(closeItem, NULL);
closeMenu->setPosition(Vec2::ZERO);
this->addChild(closeMenu, 1);
// Add main menu
auto mainMenu = Menu::create();
// items of distance
int index = 0;
std::vector<std::string> vChapters = {"Sprites", "Actions",
"BuildingAndTransitioningScenes", "UIComponents",
"OtherNodeTypes", "EventDispatcher", "Scripting",
"VirtualReality", "FileSystemAccess"};
std::vector<MenuItemLabel *> vMenuItems;
// create the menu items for each other
for (int i = 0; i < vChapters