void Game2048::initUI() { //scene -> gameLayer -> root / uiNode LayerColor *gameLayer = LayerColor::create(Color4B(0xff, 0xff, 0xf0, 0xff)); this->addChild(gameLayer); Node *root = Node::create(); gameLayer->addChild(root); Node *uiNode = Node::create(); gameLayer->addChild(uiNode); lblScore = Label::createWithTTF("", "fonts/Marker Felt.ttf", 24); lblScore->setPosition(Vec2(DISPLAY_CX, DISPLAY_HEIGHT * 4 / 5)); uiNode->addChild(lblScore); //root -> bg bgSize = DISPLAY_WIDTH - offsetW; LayerColor *bgLayer = LayerColor::create(Color4B(0xcd, 0xba, 0x96, 0xff), bgSize, bgSize); bgLayer->setPosition(Point(DISPLAY_CX,DISPLAY_CY)); bgLayer->setAnchorPoint(Point(0.5, 0.5)); bgLayer->ignoreAnchorPointForPosition(false); root->addChild(bgLayer); //NumNode numSize = (bgSize - offsetW) / s_row ; for (int i = 0; i < s_row; i++) { for (int j = 0; j < s_row; j++) { int numberX = numSize*(i+0.5) + DISPLAY_CX - bgSize/2 + offsetW/2 ; int numberY = numSize*(j+0.5) + DISPLAY_CY - bgSize/2 + offsetW/2; Node *numNode = Node::create(); numNode->setPosition(Point(numberX, numberY)); root->addChild(numNode); //bg LayerColor *numBg = LayerColor::create(Color4B(200, 190, 180, 0xff), numSize-offsetW/2, numSize-offsetW/2); numBg->setAnchorPoint(Point(0.5, 0.5)); numBg->ignoreAnchorPointForPosition(false); numNode->addChild(numBg); //card cardNumbers[i][j] = 0; Node *cardNode = createNumCard(cardNumbers[i][j]); cardNodes[i][j] = cardNode; numNode->addChild(cardNode); } } randomCreateNum(); randomCreateNum(); }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. /**/ // add "HelloWorld" splash screen" auto sprite = Sprite::create("bkq.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); sprite->setTag(1024); //scheduleUpdate(); //schedule(schedule_selector(HelloWorld::updateTest), 1.0f, kRepeatForever,5.0f); /**/ //color4b rgbA LayerColorĬÈÏêµã0,0 LayerColor* redLayer = LayerColor::create(Color4B(255, 0, 0, 255), visibleSize.width / 2, visibleSize.height / 2); redLayer->ignoreAnchorPointForPosition(false); redLayer->setPosition(visibleSize / 2); redLayer->setAnchorPoint(Vec2(0.5, 0.5)); this->addChild(redLayer); LayerColor* blueLayer = LayerColor::create(Color4B(0, 0, 255, 255), visibleSize.width / 6, visibleSize.height / 6); blueLayer->ignoreAnchorPointForPosition(false); blueLayer->setAnchorPoint(Vec2(1, 1)); redLayer->addChild(blueLayer); /* DrawNode * rect = DrawNode::create(); addChild(rect); rect->setZOrder(10); rect->drawSolidRect(Vec2(100,100),Vec2(300,300),Color4F(1,0,0,1)); DrawNode * rect2 = DrawNode::create(); addChild(rect2); rect2->drawSolidRect(Vec2(150, 150), Vec2(350, 350), Color4F(0, 1, 0, 1)); DrawNode * rect3 = DrawNode::create(); addChild(rect3); rect3->drawSolidRect(Vec2(200, 200), Vec2(400, 400), Color4F(0, 0, 1, 1)); */ return true; }
void MessageItem::init(char *msg, int time) { _msg = msg; _time = time; _curTime = 0; Size size = Size(400, 0); _node = LayerRGBA::create(); _node->retain(); LayerColor *bg = LayerColor::create(Color4B(0x0, 0x0, 0x0, 0xaa), 20, 20); bg->setAnchorPoint(Point(0.5f, 0.5f)); bg->ignoreAnchorPointForPosition(false); _node->addChild(bg); //Marker Felt auto label1 = LabelTTF::create(_msg, "Marker Felt", 20, size, TextHAlignment::CENTER, TextVAlignment::TOP); size.height = label1->getContentSize().height + 20; bg->setContentSize(size); label1->setAnchorPoint(Point(0.5f, 0.5f)); _node->addChild(label1); }
LayerColor* GameLayer::createLayer(const Size& size, Color4B color, Vec2 position){ LayerColor* layerColor = LayerColor::create(color, size.width, size.height); layerColor->setAnchorPoint(Vec2(0.5, 0.5)); layerColor->ignoreAnchorPointForPosition(false); layerColor->setPosition(position); return layerColor; }
LayerColor* GameLayer::createBackgroundTile(float x, float y, Size size, Color4B color) { LayerColor* layerColor = LayerColor::create(color, size.width, size.height); layerColor->ignoreAnchorPointForPosition(false); layerColor->setAnchorPoint(Vec2(0.5, 0.5)); layerColor->setPosition(getTilePosition(x, y)); return layerColor; }
bool SnakeLayer::init() { maximumLength = 100; itemsPicked = 0; score = 0; bonus = 0; direction = Point::ZERO; speed = 180; LayerColor* lc = LayerColor::create(Color4B(0, 0, 0, 210), PX(1.0f), PY(0.8f)); lc->setPosition(CP(0.5f, 0.5f)); lc->setAnchorPoint(Point(0.5f, 0.5f)); lc->ignoreAnchorPointForPosition(false); addChild(lc); bounds = lc->getBoundingBox(); LayerColor* scorebg = LayerColor::create(Color4B::BLACK, PX(1.0f), PY(0.1f)); scorebg->setPosition(Point::ZERO); addChild(scorebg); addChild(lineGroup = Node::create()); addChild(bonusGroup = Node::create()); addChild(itemGroup = Node::create()); LineNode* line = LineNode::create(CP(0.5f, 0.5f), CP(0.5f, 0.5f)); lineGroup->addChild(line); schedule(schedule_selector(SnakeLayer::considerSpawningBonus), 5.0f); scheduleUpdate(); lblScore = Label::createWithTTF(LS("ScoreColon") + " 0", NORMAL_TTF, 42.0f); lblScore->setColor(Color3B::WHITE); lblScore->setPosition(CP(0.5f, 0.05f)); //lblScore->enableGlow(Color4B::BLACK); //lblScore->enableShadow(Color4B::BLACK, Size::ZERO, 4); addChild(lblScore); string instruction = LS("SNAKE_INSTRUCTIONS"); lblInstruction = Label::createWithTTF(instruction, NORMAL_TTF, 48.0f); lblInstruction->setPosition(CP(0.5f, 0.5f)); lblInstruction->setHorizontalAlignment(TextHAlignment::CENTER); lblInstruction->setColor(Color3B::WHITE); lblInstruction->enableGlow(Color4B::RED); lblInstruction->runAction(RepeatForever::create( Sequence::createWithTwoActions(ScaleTo::create(1.0f, 1.1f), ScaleTo::create(1.0f, 1.0f)))); addChild(lblInstruction); instructionVisible = true; return true; }
// on "init" you need to initialize your instance bool Bug914Layer::init() { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if (BugsTestBaseLayer::init()) { auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(Bug914Layer::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(Bug914Layer::onTouchesMoved, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // ask director the the window size auto size = Director::getInstance()->getWinSize(); LayerColor *layer; for( int i=0;i < 5;i++) { layer = LayerColor::create(Color4B(i*20, i*20, i*20,255)); layer->setContentSize(Size(i*100, i*100)); layer->setPosition(Vec2(size.width/2, size.height/2)); layer->setAnchorPoint(Vec2(0.5f, 0.5f)); layer->ignoreAnchorPointForPosition(false); addChild(layer, -1-i); } // create and initialize a Label auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 64.0f); auto item1 = MenuItemFont::create("restart", CC_CALLBACK_1(Bug914Layer::restart, this)); auto menu = Menu::create(item1, nullptr); menu->alignItemsVertically(); menu->setPosition(Vec2(size.width/2, 100)); addChild(menu); // position the label on the center of the screen label->setPosition(Vec2( size.width /2 , size.height/2 )); // add the label as a child to this Layer addChild(label); return true; } return false; }
// on "init" you need to initialize your instance bool Bug914Layer::init() { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if (BugsTestBaseLayer::init()) { setTouchEnabled(true); // ask director the the window size Size size = Director::getInstance()->getWinSize(); LayerColor *layer; for( int i=0;i < 5;i++) { layer = LayerColor::create(Color4B(i*20, i*20, i*20,255)); layer->setContentSize(Size(i*100, i*100)); layer->setPosition(Point(size.width/2, size.height/2)); layer->setAnchorPoint(Point(0.5f, 0.5f)); layer->ignoreAnchorPointForPosition(false); addChild(layer, -1-i); } // create and initialize a Label LabelTTF *label = LabelTTF::create("Hello World", "Marker Felt", 64); MenuItem *item1 = MenuItemFont::create("restart", CC_CALLBACK_1(Bug914Layer::restart, this)); Menu *menu = Menu::create(item1, NULL); menu->alignItemsVertically(); menu->setPosition(Point(size.width/2, 100)); addChild(menu); // position the label on the center of the screen label->setPosition(Point( size.width /2 , size.height/2 )); // add the label as a child to this Layer addChild(label); return true; } return false; }