// on "init" you need to initialize your instancebool MainScene::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto keyboardListener = EventListenerKeyboard::create(); keyboardListener->onKeyPressed = CC_CALLBACK_2(MainScene::keyPressed, this); keyboardListener->onKeyReleased = CC_CALLBACK_2(MainScene::keyReleased, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // 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::menuCloseCallback, this)); 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 menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); // add "HelloWorld" splash screen" //auto sprite = Sprite::create("HelloWorld.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); auto minSize = ::std::min(visibleSize.width, visibleSize.height); auto fieldSize = minSize * 0.8; auto layerColorBG = LayerColor::create(Color4B(200, 190, 180, 255), fieldSize, fieldSize); layerColorBG->setPosition(Vec2(origin.x + visibleSize.width/2 - fieldSize/2, origin.y + visibleSize.height/2 - fieldSize/2)); this->addChild(layerColorBG); auto size = board_.size(); cells_.assign(size, ::std::vector<UiCell>(size, UiCell())); const auto cellsProportion = 0.90; auto cellSize = fieldSize * cellsProportion / size; auto cellMargin = fieldSize * (1 - cellsProportion) / (size + 1); for (auto i = 0; i < size; ++i) { for (auto j = 0; j < size; ++j) { auto cellPos = Vec2(cellMargin + j * (cellSize + cellMargin), fieldSize - cellMargin - cellSize - i * (cellSize + cellMargin)); auto layerCell = LayerColor::create(Color4B(180, 190, 200, 255), cellSize, cellSize); layerCell->setPosition(cellPos); layerColorBG->addChild(layerCell); layerCell = LayerColor::create(Color4B(190, 200, 180, 0), cellSize, cellSize); layerCell->setPosition(cellPos); layerColorBG->addChild(layerCell); auto label = LabelTTF::create("", "Arial", 48); label->setPosition(Vec2(cellSize/2, cellSize/2)); layerCell->addChild(label); cells_[i][j] = { layerCell, label }; } } board_.spawnNew(); board_.spawnNew(); return true;//.........这里部分代码省略.........