bool LittleSnake::init() { if (!Layer::init()) { return false; } srand(time(NULL)); directorSize = Director::getInstance()->getVisibleSize(); directorOrigin = Director::getInstance()->getVisibleOrigin(); log("Director visible size: width->%d height->%d", (int)directorSize.width,(int) directorSize.height); log("Director origin: x->%d y->%d", (int)directorOrigin.x, (int)directorOrigin.y); auto background = Sprite::create("background.png"); background->setPosition(directorOrigin.x + background->getBoundingBox().size.width/2 + 40, directorOrigin.y + directorSize.height/2); // boundary width background->setLocalZOrder(-999); this->addChild(background); auto backgroundBoundary = Sprite::create("BackgroundBoundary.png"); backgroundBoundary->setPosition(directorOrigin.x + backgroundBoundary->getBoundingBox().size.width/2, directorOrigin.y + directorSize.height/2); backgroundBoundary->setZOrder(-999); this->addChild(backgroundBoundary); loadSnakeFaces(); addSnakeFacesAsChild(); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->setSwallowTouches(true); touchListener->onTouchBegan = CC_CALLBACK_2(LittleSnake::onTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(LittleSnake::onTouchMoved, this); touchListener->onTouchEnded = CC_CALLBACK_2(LittleSnake::onTouchEnded, this); touchListener->onTouchCancelled = CC_CALLBACK_2(LittleSnake::onTouchCancelled, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this); auto menuItem1 = MenuItemFont::create("Go Back", CC_CALLBACK_1(LittleSnake::exitScene, this)); auto menuItem2 = MenuItemFont::create("Play Again", CC_CALLBACK_1(LittleSnake::playAgain, this)); menuItem1->setPosition(directorSize.width / 3 * 1, directorSize.height / 3 * 1); menuItem2->setPosition(directorSize.width / 3 * 2, directorSize.height / 3 * 1); menuItem1->setFontNameObj("fonts/Showcard Gothic.ttf"); // not refactoring right now menuItem2->setFontNameObj("fonts/Showcard Gothic.ttf"); menuItem1->setFontSizeObj(100); menuItem2->setFontSizeObj(100); menuItem1->setColor(Color3B(133, 96, 168)); // same as the menu font. not refactoring now. menuItem2->setColor(Color3B(133, 96, 168)); menu = Menu::create(menuItem1, menuItem2, NULL); menu->setPosition(0, 0); menu->setVisible(false); menu->setLocalZOrder(999); this->addChild(menu); return true; }
bool MatchResultScene::init() { if (!Layer::init()) return false; auto visibleSize = Director::getInstance()->getVisibleSize(); auto origin = Director::getInstance()->getVisibleOrigin(); auto label = LabelTTF::create("Team 1", "Arial", TITLE_FONT_SIZE); label->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - label->getContentSize().height * 1.5f); this->addChild(label); auto label2 = LabelTTF::create("Team 2", "Arial", TITLE_FONT_SIZE); label2->setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - label->getContentSize().height * 5.0f); this->addChild(label2); auto matchInfoMenuItem = MenuItemFont::create("View Other Match Results", CC_CALLBACK_1(MatchResultScene::otherMatchResultsCallback, this)); matchInfoMenuItem->setFontSizeObj(18); matchInfoMenuItem->setPosition(origin.x + visibleSize.width / 2 - matchInfoMenuItem->getContentSize().width / 2, origin.y - visibleSize.height / 2 + matchInfoMenuItem->getContentSize().height / 2); auto startScreenMenuItem = MenuItemImage::create("MainMenu.png", "MainMenu.png", CC_CALLBACK_1(MatchResultScene::mainMenuCallback, this)); startScreenMenuItem->setPosition(origin.x + visibleSize.width / 2 - startScreenMenuItem->getContentSize().width / 2, origin.y + visibleSize.height / 2 - startScreenMenuItem->getContentSize().height / 2); Vector<MenuItem*> MenuItems; MenuItems.pushBack(matchInfoMenuItem); MenuItems.pushBack(startScreenMenuItem); this->addChild(Menu::createWithArray(MenuItems)); return true; }
VRTest1::VRTest1() { auto size = Director::getInstance()->getVisibleSize(); auto image = Sprite::create("Images/background.png"); image->setPosition(size/2); addChild(image); auto button = MenuItemFont::create("Enable / Disable VR", [](Ref* ref){ auto glview = Director::getInstance()->getOpenGLView(); auto vrimpl = glview->getVR(); if (vrimpl) { glview->setVR(nullptr); } else { auto genericvr = new VRGenericRenderer; glview->setVR(genericvr); } }); button->setFontSizeObj(16); auto menu = Menu::create(button, nullptr); addChild(menu); menu->setPosition(size / 6 ); }
bool MainMenuScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); /////////////////////////// // Header auto headerLabel = Label::createWithTTF("MagneticWorld", "fonts/Marker Felt.ttf", 80); headerLabel->setTextColor(Color4B::GRAY); float currentY = origin.y + visibleSize.height - headerLabel->getContentSize().height; headerLabel->setPosition(Vec2(origin.x + visibleSize.width/2, currentY)); this->addChild(headerLabel, 1); /////////////////////////// // Menu auto startItem = MenuItemFont::create("- Start -", CC_CALLBACK_1(MainMenuScene::menuNewGameCallback, this)); startItem->setFontSizeObj(54); currentY -= (startItem->getContentSize().height + 80); startItem->setPosition(Vec2(origin.x + visibleSize.width/2, currentY)); auto highScoreItem = MenuItemFont::create("- Highscore -", CC_CALLBACK_1(MainMenuScene::menuCloseCallback, this)); highScoreItem->setFontSizeObj(54); currentY -= (highScoreItem->getContentSize().height + 20); highScoreItem->setPosition(Vec2(origin.x + visibleSize.width/2, currentY)); auto closeItem = MenuItemFont::create("- Quit -", CC_CALLBACK_1(MainMenuScene::menuCloseCallback, this)); closeItem->setFontSizeObj(54); currentY -= (closeItem->getContentSize().height + 120); closeItem->setPosition(Vec2(origin.x + visibleSize.width/2, currentY)); // create menu, it's an autorelease object auto menu = Menu::create(startItem, highScoreItem, closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// return true; }
Menu* WelcomeMenu::_createMenu() { auto startGameItem = MenuItemFont::create("开始游戏", CC_CALLBACK_1(WelcomeMenu::_startGameMenuCallback, this)); startGameItem->setFontSizeObj(50); startGameItem->setFontName("fonts/su_xin_shi_liu_jian_kai.ttf"); auto menu = Menu::create(startGameItem, NULL); menu->alignItemsVertically(); return menu; }
bool RestartLayer::init() { if ( !Layer::init() ) { return false; } // 设置UI auto backColor = LayerColor::create(Color4B::GRAY, GAME_WIDTH - 70 ,GAME_WIDTH /2 ); this->addChild( backColor ); backColor->ignoreAnchorPointForPosition( false ); backColor->setAnchorPoint( Point::ANCHOR_MIDDLE ); backColor->setPosition( Point( GAME_SCREEN_WIDTH/2 , GAME_SCREEN_HEIGHT/2 ) ); auto labelTips = Label::createWithSystemFont("Are you sure to reset ?" ,"Arial",15); labelTips->setAnchorPoint( Point::ANCHOR_MIDDLE ); labelTips->setPosition( Point( backColor->getContentSize().width/2 , backColor->getContentSize().height*3/4 ) ); backColor->addChild(labelTips); auto itemYES = MenuItemFont::create("Yes",CC_CALLBACK_1(RestartLayer::yesCallback, this)); itemYES->setFontSizeObj(20); itemYES->setPosition( Point (60,40 ) ); auto itemNO = MenuItemFont::create("No",CC_CALLBACK_1(RestartLayer::noCallback, this)); itemNO->setFontSizeObj(20); itemNO->setPosition( Point ( backColor->getContentSize().width - 60 ,40 ) ); auto menu = Menu::create( itemYES , itemNO , NULL); backColor->addChild( menu ); menu->setAnchorPoint(Point::ZERO); menu->setPosition(Point::ZERO); // 不向下传递触摸 auto eventListener = EventListenerTouchOneByOne::create(); eventListener->onTouchBegan = [&] ( Touch* t , Event* e) { return true; }; eventListener->setSwallowTouches(true); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority( eventListener , this ); return true; }
Scene* Chapter13::createScene() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // some upfront items that we need // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); Size playingSize = Size(visibleSize.width, visibleSize.height - (visibleSize.height/8)); // actual playing size to work with // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a scene // 'scene' is an autorelease object // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto scene = Scene::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold non-sprites. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold menu // create a menu // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; auto menuItem1 = MenuItemFont::create("Play Music"); menuItem1->setFontNameObj("fonts/Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setName("menuItem1"); menuItem1->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem1->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->playBackgroundMusic("music.mp3"); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem1"); cnode->setVisible(false); cnode = bnode->getChildByName("menuItem2"); cnode->setVisible(true); }); auto menuItem2 = MenuItemFont::create("Stop Music"); menuItem2->setFontNameObj("fonts/Marker Felt.ttf"); menuItem2->setFontSizeObj(32); menuItem2->setName("menuItem2"); menuItem2->setVisible(false); menuItem2->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem2->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->stopBackgroundMusic("music.mp3"); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem2"); cnode->setVisible(false); cnode = bnode->getChildByName("menuItem1"); cnode->setVisible(true); }); auto menuItem3 = MenuItemFont::create("Play Effect"); menuItem3->setFontNameObj("fonts/Marker Felt.ttf"); menuItem3->setFontSizeObj(32); menuItem3->setName("menuItem3"); menuItem3->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (++index) * 40)); menuItem3->setCallback([&](cocos2d::Ref *sender) { auto audio = CocosDenshion::SimpleAudioEngine::getInstance(); audio->playEffect("alert.mp3"); }); auto menu = Menu::create(menuItem1, menuItem2, menuItem3, NULL); menu->setName("menu"); menuNode->addChild(menu, 1); menu->setPosition(Vec2::ZERO); scene->addChild(menuNode, 2); // return the scene return scene; }
Scene* Chapter2Level1::createScene() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // some upfront items that we need // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size playingSize = Size(visibleSize.width, visibleSize.height - (visibleSize.height/8)); // actual playing size to work with // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a scene // 'scene' is an autorelease object // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto scene = Scene::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold all of the labels // create the player and score labels // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int paddingX = 20; int paddingY = 20; auto labelNode = Node::create(); auto player1 = Label::createWithTTF("Player 1", "fonts/Marker Felt.ttf", 32); auto player2 = Label::createWithTTF("Player 2", "fonts/Marker Felt.ttf", 32); auto player1Score = Label::createWithTTF("00000", "fonts/Marker Felt.ttf", 32); auto player2Score = Label::createWithTTF("00000", "fonts/Marker Felt.ttf", 32); labelNode->addChild(player1, -2); labelNode->addChild(player1Score, -1); labelNode->addChild(player2, 1); labelNode->addChild(player2Score, 2); player1->setPosition(Vec2(0 + player1->getContentSize().width / 2 + paddingX, visibleSize.height - player1->getContentSize().height / 2 - paddingY)); player1Score->setPosition(Vec2(0 + player1->getPositionX() + player1->getContentSize().width + paddingX, visibleSize.height - player1->getContentSize().height / 2 - paddingY)); player2Score->setPosition(Vec2(visibleSize.width - player2Score->getContentSize().width / 2 - paddingX, visibleSize.height - player2Score->getContentSize().height / 2 - paddingY)); player2->setPosition(Vec2(player2Score->getPositionX() - player2Score->getContentSize().width - paddingX, visibleSize.height - player2Score->getContentSize().height / 2 - paddingY)); scene->addChild(labelNode, 0); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold non-sprites. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a path/walkway // depending upon how large the screen is we need to decide how many blocks to lay down. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto testSprite = Sprite::create("ZigzagForest_Square.png"); int howMany = std::ceil(visibleSize.width / testSprite->getContentSize().width); int sX = 0; // act as a counter for setPosition x coordinate. int sY = 0; // act as a counter for setPosition y coordinate. playingSize = Size(visibleSize.width, visibleSize.height - testSprite->getContentSize().height); for (int i=0; i < howMany; i++) { auto sprite = Sprite::create("ZigzagForest_Square.png"); sprite->setAnchorPoint(Vec2(0,0)); sprite->setPosition(sX,sY); sX += sprite->getContentSize().width; nodeItems->addChild(sprite, -1); } testSprite = NULL; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a few blocks as obstables // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ testSprite = Sprite::create("ZigzagGrass_Mud_Round.png"); // left side blocks sX = visibleSize.width/4 - testSprite->getContentSize().width; sY = playingSize.height/2 - testSprite->getContentSize().height * 2; for (int i=0; i < 3; i++) { auto sprite = Sprite::create("ZigzagGrass_Mud_Round.png"); sprite->setAnchorPoint(Vec2(0,0)); sprite->setPosition(sX,sY); sX += sprite->getContentSize().width; if (i == 1) { sprite->setName("middleBlock1"); } nodeItems->addChild(sprite, 1); } // right side blocks sX = (visibleSize.width/2 + visibleSize.width/4) - testSprite->getContentSize().width; sY = playingSize.height/2 - testSprite->getContentSize().height * 2; for (int i=0; i < 3; i++) { auto sprite = Sprite::create("ZigzagGrass_Mud_Round.png"); sprite->setAnchorPoint(Vec2(0,0)); sprite->setPosition(sX,sY); sX += sprite->getContentSize().width; if (i == 1) { sprite->setName("middleBlock2"); } scene->addChild(sprite, 1); } // center blocks sX = visibleSize.width/2 - testSprite->getContentSize().width; sY = (playingSize.height/2 + playingSize.height/4) - testSprite->getContentSize().height * 2; for (int i=0; i < 3; i++) { auto sprite = Sprite::create("ZigzagGrass_Mud_Round.png"); sprite->setAnchorPoint(Vec2(0,0)); sprite->setPosition(sX,sY); sX += sprite->getContentSize().width; nodeItems->addChild(sprite, 1); } testSprite = NULL; scene->addChild(nodeItems, 1); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold sprites // create a sprite // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto spriteNode = Node::create(); spriteNode->setName("spriteNode"); auto sprite1 = Sprite::create("Blue_Front1.png"); sprite1->setAnchorPoint(Vec2(0,0)); sprite1->setPosition(100, (visibleSize.height - playingSize.height)); sprite1->setName("mainSprite"); spriteNode->addChild(sprite1, 1); scene->addChild(spriteNode, 1); //sprite1->scheduleOnce([&](float dt) { // // the local variable "sprite1" will go out of scope, so I have to get it from "this" // auto anode = Director::getInstance()->getRunningScene()->getChildByName("spriteNode"); // auto bnode = anode->getChildByName("mainSprite"); // // //auto moveBy = MoveBy::create(2, Point(400,0)); // // //bnode->runAction(moveBy); //}, 5, "udpate_key"); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold menu // create a menu // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto menuNode = Node::create(); menuNode->setName("menuNode"); auto menuItem1 = MenuItemFont::create("Demo Parent/Child"); menuItem1->setFontNameObj("fonts/Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setCallback([&](cocos2d::Ref *sender) { auto anode = Director::getInstance()->getRunningScene()->getChildByName("spriteNode"); auto bnode = anode->getChildByName("mainSprite"); auto cnode = Director::getInstance()->getRunningScene()->getChildByName("nodeItems"); auto dnode = cnode->getChildByName("middleBlock1"); // add a few more sprites auto sprite1 = Sprite::create("LightBlue_Front1.png"); sprite1->setAnchorPoint(Vec2(0,0)); sprite1->setName("childSprite1"); bnode->addChild(sprite1, 1); sprite1->setPosition(dnode->getPositionX(), dnode->getPositionY()); auto sprite2 = Sprite::create("White_Front1.png"); sprite2->setAnchorPoint(Vec2(0,0)); sprite2->setName("childSprite2"); bnode->addChild(sprite2, 1); sprite2->setPosition(dnode->getPositionX() - dnode->getContentSize().width * 3, dnode->getPositionY()); // hide the menu option auto ynode = Director::getInstance()->getRunningScene()->getChildByName("menuNode"); auto znode = ynode->getChildByName("menu"); znode->removeFromParentAndCleanup(true); // schedule an action after 3 seconds bnode->scheduleOnce([&](float dt) { auto anode = Director::getInstance()->getRunningScene()->getChildByName("spriteNode"); auto bnode = anode->getChildByName("mainSprite"); bnode->setRotation(40.0f); auto delay = DelayTime::create(4.0f); bnode->runAction(delay); bnode->setScale(2.0f); // You could also use an Action and Sequence combination to do this. //auto rotateBy = RotateBy::create(2, 40.0f); //auto scaleBy = ScaleTo::create(2, 2.0f); //auto seq = Sequence::create(rotateBy, scaleBy, NULL); //bnode->runAction(seq); }, 3, "udpate_key"); }); auto menuItemGoBack = MenuItemFont::create("Go Back"); menuItemGoBack->setFontNameObj("fonts/Marker Felt.ttf"); menuItemGoBack->setFontSizeObj(54); menuItemGoBack->setPosition(menuItemGoBack->getPositionX() + 400, menuItemGoBack->getPositionY()-300); menuItemGoBack->setCallback([&](Ref *sender) { //Director::getInstance()->replaceScene(Chapter2::createScene()); Director::getInstance()->replaceScene(TransitionFlipX::create(0.5, Chapter2::createScene()/*, Color3B(0, 255, 255)*/)); }); auto menu = Menu::create(menuItem1,menuItemGoBack, NULL); menu->setName("menu"); menuNode->addChild(menu, 1); menu->setPosition(visibleSize.width / 2, visibleSize.height - menuItem1->getContentSize().height * 2); scene->addChild(menuNode, 2); // return the scene return scene; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 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(HelloWorld::menuCloseCallback, this)); // // closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , // origin.y + closeItem->getContentSize().height/2)); // // // auto startItem = MenuItemImage::create("start_unselected.png", // "start_selected.png", // CC_CALLBACK_1(HelloWorld::startMarchCallback, this)); // startItem->setPosition(Point(winSize.width/2, winSize.height/2)); // // auto shipyardItem = MenuItemImage::create( // "CloseNormal.png", // "CloseSelected.png", // CC_CALLBACK_1(HelloWorld::shipyardCallback, this)); // shipyardItem->setPosition(Point(origin.x + visibleSize.width - shipyardItem->getContentSize().width / 2 - 100, origin.y + shipyardItem->getContentSize().height / 2)); auto startItem = MenuItemFont::create("Launch", CC_CALLBACK_1(HelloWorld::startMarchCallback, this)); startItem->setFontSizeObj(MENUITEM_FONTSIZE); startItem->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2 + 50)); auto closeItem = MenuItemFont::create("Quit Game", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setFontSizeObj(MENUITEM_FONTSIZE); closeItem->setPosition(Point(visibleSize.width/2, visibleSize.height/2 - 50)); // // auto chatItem = MenuItemFont::create("Chat", CC_CALLBACK_1(HelloWorld::chatItemCallback, this)); // chatItem->setFontSizeObj(24); // chatItem->setFontName("Helvetica"); // chatItem->setPosition(startItem->getPosition() + Point(0, 100)); // // create menu, it's an autorelease object auto menu = Menu::create(closeItem, startItem, NULL); menu->setPosition(Point::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(Point(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(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // // // add the sprite as a child to this layer // this->addChild(sprite, 0); return true; }
Scene* Chapter2::createScene() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // some upfront items that we need // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size playingSize = Size(visibleSize.width, visibleSize.height - (visibleSize.height/8)); // actual playing size to work with // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a scene for our game to hold everything // 'scene' is an autorelease object // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto scene = Scene::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create the title dialog // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto white = Color4F(1.0f, 0.5f, 0.3f, 1); int verts = 4; cocos2d::Point stripe1[] = { cocos2d::Point(0,0), cocos2d::Point(0,200), cocos2d::Point(600,200), cocos2d::Point(600,0) }; cocos2d::DrawNode* dotNode1 = cocos2d::DrawNode::create(); dotNode1->setContentSize(Size(600, 200)); dotNode1->drawPolygon(stripe1, verts, white, 0, white); dotNode1->setPosition(Vec2(visibleSize.width/2 - (dotNode1->getContentSize().width / 2), playingSize.height - dotNode1->getContentSize().height)); auto label = Label::createWithTTF("Super Cocos Aliens", "Marker Felt.ttf", 64); dotNode1->addChild(label, 1); label->setPosition(Vec2(dotNode1->getContentSize().width/2, dotNode1->getContentSize().height/2)); scene->addChild(dotNode1, -1); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold all labels // create the player and score labels // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int paddingX = 20; int paddingY = 20; auto labelNode = Node::create(); auto player1 = Label::createWithTTF("Player 1", "Marker Felt.ttf", 32); auto player2 = Label::createWithTTF("Player 2", "Marker Felt.ttf", 32); auto player1Score = Label::createWithTTF("00000", "Marker Felt.ttf", 32); auto player2Score = Label::createWithTTF("00000", "Marker Felt.ttf", 32); labelNode->addChild(player1, 0); labelNode->addChild(player2, 0); labelNode->addChild(player1Score, 0); labelNode->addChild(player2Score, 0); player1->setPosition(Vec2(0 + player1->getContentSize().width / 2 + paddingX, visibleSize.height - player1->getContentSize().height / 2 - paddingY)); player1Score->setPosition(Vec2(0 + player1->getPositionX() + player1->getContentSize().width + paddingX, visibleSize.height - player1->getContentSize().height / 2 - paddingY)); player2Score->setPosition(Vec2(visibleSize.width - player2Score->getContentSize().width / 2 - paddingX, visibleSize.height - player2Score->getContentSize().height / 2 - paddingY)); player2->setPosition(Vec2(player2Score->getPositionX() - player2Score->getContentSize().width - paddingX, visibleSize.height - player2Score->getContentSize().height / 2 - paddingY)); scene->addChild(labelNode, -1); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold non-sprites. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto nodeItems = Node::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a path/walkway // depending upon how large the screen is we need to decide how many blocks to lay down. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto testSprite = Sprite::create("ZigzagForest_Square.png"); int howMany = std::ceil(visibleSize.width / testSprite->getContentSize().width); int sX = 0; // act as a counter for setPosition x coordinate. int sY = 0; // act as a counter for setPosition y coordinate. playingSize = Size(visibleSize.width, visibleSize.height - testSprite->getContentSize().height); for (int i=0; i < howMany; i++) { auto sprite = Sprite::create("ZigzagForest_Square.png"); sprite->setAnchorPoint(Vec2(0,0)); sprite->setPosition(sX,sY); sX += sprite->getContentSize().width; nodeItems->addChild(sprite, 0); } testSprite = NULL; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create the blocks // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ testSprite = Sprite::create("ZigzagGrass_Mud_Round.png"); sX = visibleSize.width/2 - testSprite->getContentSize().width; sY = playingSize.height/2 - testSprite->getContentSize().height * 2; for (int i=0; i < 5; i++) { auto sprite = Sprite::create("ZigzagGrass_Mud_Round.png"); sprite->setAnchorPoint(Vec2(0,0)); sprite->setPosition(sX,sY); sX += sprite->getContentSize().width; nodeItems->addChild(sprite, 0); } testSprite = NULL; scene->addChild(nodeItems, 1); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold all sprites // create a few sprites // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto spriteNode = Node::create(); auto sprite1 = Sprite::create("Blue_Front1.png"); sprite1->setAnchorPoint(Vec2(0,0)); sprite1->setPosition(100, (visibleSize.height - playingSize.height)); spriteNode->addChild(sprite1, 1); auto sprite2 = Sprite::create("LightBlue_Front1.png"); sprite2->setAnchorPoint(Vec2(0,0)); sprite2->setPosition(500, (visibleSize.height - playingSize.height)); spriteNode->addChild(sprite2, 1); auto sprite3 = Sprite::create("White_Front1.png"); sprite3->setAnchorPoint(Vec2(0,0)); sprite3->setPosition(800, (visibleSize.height - playingSize.height)); spriteNode->addChild(sprite3, 1); scene->addChild(spriteNode, 1); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold menu // create a menu // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto menuNode = Node::create(); auto menuItem1 = MenuItemFont::create("Start Game"); menuItem1->setFontNameObj("Marker Felt.ttf"); menuItem1->setFontSizeObj(64); menuItem1->setCallback([&](cocos2d::Ref *sender) { Director::getInstance()->replaceScene(Chapter2Level1::createScene()); }); auto menu = Menu::create(menuItem1, NULL); menuNode->addChild(menu, 0); scene->addChild(menuNode, 2); // return the scene return scene; }
Scene* Chapter3::createScene() { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // some upfront items that we need // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto dirs = Director::getInstance(); Size visibleSize = dirs->getVisibleSize(); Vec2 origin = dirs->getVisibleOrigin(); Size playingSize = Size(visibleSize.width, visibleSize.height - (visibleSize.height/8)); // actual playing size to work with // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a scene // 'scene' is an autorelease object // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto scene = Scene::create(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold non-sprites. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto nodeItems = Node::create(); nodeItems->setName("nodeItems"); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // create a node to hold menu // create a menu // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ auto menuNode = Node::create(); menuNode->setName("menuNode"); int index = 2; auto menuItem1 = MenuItemFont::create("First: Create a Sprite"); menuItem1->setFontNameObj("Marker Felt.ttf"); menuItem1->setFontSizeObj(32); menuItem1->setName("menuItem1"); menuItem1->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem1->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto newSprite = Sprite::create("Blue_Front1.png"); newSprite->setPosition(100, 256); newSprite->setName("sprite1"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem1"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem2"); dnode->setVisible(true); }); auto menuItem2 = MenuItemFont::create("Next: Create a Sprite from a Rect"); menuItem2->setFontNameObj("Marker Felt.ttf"); menuItem2->setFontSizeObj(32); menuItem2->setName("menuItem2"); menuItem2->setVisible(false); menuItem2->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem2->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto newSprite = Sprite::create("Blue_Front1.png", Rect(0,0,50,50)); newSprite->setPosition(250, 256); newSprite->setName("sprite2"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem2"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem3"); dnode->setVisible(true); }); auto menuItem3 = MenuItemFont::create("Next: Create a Sprite from SpriteFrameCache"); menuItem3->setFontNameObj("Marker Felt.ttf"); menuItem3->setFontSizeObj(32); menuItem3->setName("menuItem3"); menuItem3->setVisible(false); menuItem3->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem3->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto spritecache = SpriteFrameCache::getInstance(); spritecache->addSpriteFramesWithFile("sprites.plist"); auto newSprite = Sprite::createWithSpriteFrameName("Blue_Front1.png"); newSprite->setPosition(400, 256); newSprite->setName("sprite3"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem3"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem4"); dnode->setVisible(true); }); auto menuItem4 = MenuItemFont::create("Next: Create a Sprite from SpriteFrame"); menuItem4->setFontNameObj("Marker Felt.ttf"); menuItem4->setFontSizeObj(32); menuItem4->setName("menuItem4"); menuItem4->setVisible(false); menuItem4->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem4->setCallback([&](cocos2d::Ref *sender) { auto dirs = Director::getInstance()->getRunningScene(); auto spritecache = SpriteFrameCache::getInstance(); spritecache->addSpriteFramesWithFile("sprites.plist"); auto newspriteFrame = SpriteFrame::create("Blue_Front1.png", Rect(0,0,50,50)); auto newSprite = Sprite::createWithSpriteFrame(newspriteFrame); newSprite->setPosition(550, 256); newSprite->setName("sprite4"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem4"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem5"); dnode->setVisible(true); }); auto menuItem5 = MenuItemFont::create("Next: Scale a Sprite"); menuItem5->setFontNameObj("Marker Felt.ttf"); menuItem5->setFontSizeObj(32); menuItem5->setName("menuItem5"); menuItem5->setVisible(false); menuItem5->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem5->setCallback([&](cocos2d::Ref *sender) { // hide everything else on the screen auto dirs = Director::getInstance()->getRunningScene(); dirs->removeChildByName("sprite1"); dirs->removeChildByName("sprite2"); dirs->removeChildByName("sprite3"); dirs->removeChildByName("sprite4"); auto newSprite = Sprite::create("Blue_Front1.png"); newSprite->setPosition(100, 256); newSprite->setName("sprite5"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto point00 = Sprite::create("dot.png"); point00->setName("point00"); point00->setPosition(newSprite->getPosition()); dirs->addChild(point00,2); auto newSpriteScaleXY = Sprite::create("Blue_Front1.png"); newSpriteScaleXY->setPosition(300,256); newSpriteScaleXY->setName("sprite6"); newSpriteScaleXY->setAnchorPoint(Vec2(0.5,0.5)); newSpriteScaleXY->setScale(2.0f); dirs->addChild(newSpriteScaleXY,1); auto pointXY = Sprite::create("dot.png"); pointXY->setName("pointXY"); pointXY->setPosition(newSpriteScaleXY->getPosition()); dirs->addChild(pointXY,2); auto newSpriteScaleX = Sprite::create("Blue_Front1.png"); newSpriteScaleX->setPosition(500,256); newSpriteScaleX->setName("sprite7"); newSpriteScaleX->setAnchorPoint(Vec2(0.5,0.5)); newSpriteScaleX->setScaleX(2.0f); dirs->addChild(newSpriteScaleX,1); auto pointX = Sprite::create("dot.png"); pointX->setName("pointX"); pointX->setPosition(newSpriteScaleX->getPosition()); dirs->addChild(pointX,2); auto newSpriteScaleY = Sprite::create("Blue_Front1.png"); newSpriteScaleY->setPosition(800, 256); newSpriteScaleY->setName("sprite8"); newSpriteScaleY->setAnchorPoint(Vec2(0.5,0.5)); newSpriteScaleY->setScaleY(2.0f); dirs->addChild(newSpriteScaleY,1); auto pointY = Sprite::create("dot.png"); pointY->setName("pointY"); pointY->setPosition(newSpriteScaleY->getPosition()); dirs->addChild(pointY,2); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem5"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem6"); dnode->setVisible(true); }); auto menuItem6 = MenuItemFont::create("Next: Anchor Point and Sprite"); menuItem6->setFontNameObj("Marker Felt.ttf"); menuItem6->setFontSizeObj(32); menuItem6->setName("menuItem6"); menuItem6->setVisible(false); menuItem6->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem6->setCallback([&](cocos2d::Ref *sender) { // hide everything else on the screen auto dirs = Director::getInstance()->getRunningScene(); dirs->removeChildByName("sprite5"); dirs->removeChildByName("sprite6"); dirs->removeChildByName("sprite7"); dirs->removeChildByName("sprite8"); dirs->removeChildByName("point00"); dirs->removeChildByName("pointXY"); dirs->removeChildByName("pointX"); dirs->removeChildByName("pointY"); auto newSprite00 = Sprite::create("Blue_Front1.png"); newSprite00->setPosition(100, 256); newSprite00->setName("sprite9"); newSprite00->setAnchorPoint(Vec2(0,0)); dirs->addChild(newSprite00,1); auto point00 = Sprite::create("dot.png"); point00->setName("point00"); point00->setPosition(newSprite00->getPosition()); dirs->addChild(point00,2); auto newSprite01 = Sprite::create("Blue_Front1.png"); newSprite01->setPosition(250, 256); newSprite01->setName("sprite10"); newSprite01->setAnchorPoint(Vec2(0,1)); dirs->addChild(newSprite01,1); auto point01 = Sprite::create("dot.png"); point01->setName("point01"); point01->setPosition(newSprite01->getPosition()); dirs->addChild(point01,2); auto newSprite10 = Sprite::create("Blue_Front1.png"); newSprite10->setPosition(400, 256); newSprite10->setName("sprite11"); newSprite10->setAnchorPoint(Vec2(1,0)); dirs->addChild(newSprite10,1); auto point10 = Sprite::create("dot.png"); point10->setName("point10"); point10->setPosition(newSprite10->getPosition()); dirs->addChild(point10,2); auto newSprite11 = Sprite::create("Blue_Front1.png"); newSprite11->setPosition(550, 256); newSprite11->setName("sprite12"); newSprite11->setAnchorPoint(Vec2(1,1)); dirs->addChild(newSprite11,1); auto point11 = Sprite::create("dot.png"); point11->setName("point11"); point11->setPosition(newSprite11->getPosition()); dirs->addChild(point11,2); auto newSprite22 = Sprite::create("Blue_Front1.png"); newSprite22->setPosition(700, 256); newSprite22->setName("sprite13"); newSprite22->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite22,1); auto point22 = Sprite::create("dot.png"); point22->setName("point22"); point22->setPosition(newSprite22->getPosition()); dirs->addChild(point22,2); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem6"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem7"); dnode->setVisible(true); }); auto menuItem7 = MenuItemFont::create("Next: Skew a Sprite"); menuItem7->setFontNameObj("Marker Felt.ttf"); menuItem7->setFontSizeObj(32); menuItem7->setName("menuItem7"); menuItem7->setVisible(false); menuItem7->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem7->setCallback([&](cocos2d::Ref *sender) { // hide everything else on the screen auto dirs = Director::getInstance()->getRunningScene(); dirs->removeChildByName("sprite9"); dirs->removeChildByName("sprite10"); dirs->removeChildByName("sprite11"); dirs->removeChildByName("sprite12"); dirs->removeChildByName("sprite13"); dirs->removeChildByName("point00"); dirs->removeChildByName("point01"); dirs->removeChildByName("point10"); dirs->removeChildByName("point11"); dirs->removeChildByName("point22"); auto newSprite = Sprite::create("Blue_Front1.png"); newSprite->setPosition(100, 256); newSprite->setName("sprite14"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto pointN = Sprite::create("dot.png"); pointN->setName("pointN"); pointN->setPosition(newSprite->getPosition()); dirs->addChild(pointN,2); auto newSpriteSkewX = Sprite::create("Blue_Front1.png"); newSpriteSkewX->setPosition(250, 256); newSpriteSkewX->setName("sprite15"); newSpriteSkewX->setAnchorPoint(Vec2(0.5,0.5)); newSpriteSkewX->setSkewX(20.0f); dirs->addChild(newSpriteSkewX,1); auto pointX = Sprite::create("dot.png"); pointX->setName("pointX"); pointX->setPosition(newSpriteSkewX->getPosition()); dirs->addChild(pointX,2); auto newSpriteSkewY = Sprite::create("Blue_Front1.png"); newSpriteSkewY->setPosition(400, 256); newSpriteSkewY->setName("sprite16"); newSpriteSkewY->setAnchorPoint(Vec2(0.5,0.5)); newSpriteSkewY->setSkewY(20.0f); dirs->addChild(newSpriteSkewY,1); auto pointY = Sprite::create("dot.png"); pointY->setName("pointY"); pointY->setPosition(newSpriteSkewY->getPosition()); dirs->addChild(pointY,2); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem7"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem8"); dnode->setVisible(true); }); auto menuItem8 = MenuItemFont::create("Next: Rotate a Sprite"); menuItem8->setFontNameObj("Marker Felt.ttf"); menuItem8->setFontSizeObj(32); menuItem8->setName("menuItem8"); menuItem8->setVisible(false); menuItem8->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem8->setCallback([&](cocos2d::Ref *sender) { // hide everything else on the screen auto dirs = Director::getInstance()->getRunningScene(); dirs->removeChildByName("sprite14"); dirs->removeChildByName("sprite15"); dirs->removeChildByName("sprite16"); dirs->removeChildByName("pointN"); dirs->removeChildByName("pointX"); dirs->removeChildByName("pointY"); auto newSprite = Sprite::create("Blue_Front1.png"); newSprite->setPosition(100, 256); newSprite->setName("sprite17"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto point00 = Sprite::create("dot.png"); point00->setName("point00"); point00->setPosition(newSprite->getPosition()); dirs->addChild(point00,2); auto newSpriteRotateP20 = Sprite::create("Blue_Front1.png"); newSpriteRotateP20->setPosition(250, 256); newSpriteRotateP20->setName("sprite18"); newSpriteRotateP20->setAnchorPoint(Vec2(0.5,0.5)); newSpriteRotateP20->setRotation(20.0f); dirs->addChild(newSpriteRotateP20,1); auto pointP20 = Sprite::create("dot.png"); pointP20->setName("pointP20"); pointP20->setPosition(newSpriteRotateP20->getPosition()); dirs->addChild(pointP20,2); auto newSpriteRotateN20 = Sprite::create("Blue_Front1.png"); newSpriteRotateN20->setPosition(400, 256); newSpriteRotateN20->setName("sprite19"); newSpriteRotateN20->setAnchorPoint(Vec2(0.5,0.5)); newSpriteRotateN20->setRotation(-20.0f); dirs->addChild(newSpriteRotateN20,1); auto pointN20 = Sprite::create("dot.png"); pointN20->setName("pointN20"); pointN20->setPosition(newSpriteRotateN20->getPosition()); dirs->addChild(pointN20,2); auto newSpriteRotateP60 = Sprite::create("Blue_Front1.png"); newSpriteRotateP60->setPosition(550, 256); newSpriteRotateP60->setName("sprite20"); newSpriteRotateP60->setAnchorPoint(Vec2(0.5,0.5)); newSpriteRotateP60->setRotation(60.0f); dirs->addChild(newSpriteRotateP60,1); auto pointP60 = Sprite::create("dot.png"); pointP60->setName("pointP60"); pointP60->setPosition(newSpriteRotateP60->getPosition()); dirs->addChild(pointP60,2); auto newSpriteRotateN60 = Sprite::create("Blue_Front1.png"); newSpriteRotateN60->setPosition(700, 256); newSpriteRotateN60->setName("sprite21"); newSpriteRotateN60->setAnchorPoint(Vec2(0.5,0.5)); newSpriteRotateN60->setRotation(-60.0f); dirs->addChild(newSpriteRotateN60,1); auto pointN60 = Sprite::create("dot.png"); pointN60->setName("pointN60"); pointN60->setPosition(newSpriteRotateN60->getPosition()); dirs->addChild(pointN60,2); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem8"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem9"); dnode->setVisible(true); }); auto menuItem9 = MenuItemFont::create("Next: Change the Color of a Sprite"); menuItem9->setFontNameObj("Marker Felt.ttf"); menuItem9->setFontSizeObj(32); menuItem9->setName("menuItem9"); menuItem9->setVisible(false); menuItem9->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem9->setCallback([&](cocos2d::Ref *sender) { // hide everything else on the screen auto dirs = Director::getInstance()->getRunningScene(); dirs->removeChildByName("sprite18"); dirs->removeChildByName("sprite19"); dirs->removeChildByName("sprite20"); dirs->removeChildByName("sprite21"); dirs->removeChildByName("point00"); dirs->removeChildByName("pointP20"); dirs->removeChildByName("pointN20"); dirs->removeChildByName("pointP60"); dirs->removeChildByName("pointN60"); auto newSprite = Sprite::create("Blue_Front1.png"); newSprite->setPosition(100, 256); newSprite->setName("sprite30"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); dirs->addChild(newSprite,1); auto newSprite2 = Sprite::create("Blue_Front1.png"); newSprite2->setPosition(200, 256); newSprite2->setName("sprite31"); newSprite2->setAnchorPoint(Vec2(0.5,0.5)); newSprite2->setColor(Color3B(155, 200, 50)); dirs->addChild(newSprite2,1); auto newSprite3 = Sprite::create("Blue_Front1.png"); newSprite3->setPosition(300, 256); newSprite3->setName("sprite32"); newSprite3->setAnchorPoint(Vec2(0.5,0.5)); newSprite3->setColor(Color3B(10, 230, 100)); dirs->addChild(newSprite3,1); auto newSprite4 = Sprite::create("Blue_Front1.png"); newSprite4->setPosition(400, 256); newSprite4->setName("sprite33"); newSprite4->setAnchorPoint(Vec2(0.5,0.5)); newSprite4->setColor(Color3B(100, 101, 230)); dirs->addChild(newSprite4,1); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem9"); cnode->setVisible(false); auto dnode = bnode->getChildByName("menuItem10"); dnode->setVisible(true); }); auto menuItem10 = MenuItemFont::create("Next: Change the Color of a Sprite"); menuItem10->setFontNameObj("Marker Felt.ttf"); menuItem10->setFontSizeObj(32); menuItem10->setName("menuItem10"); menuItem10->setVisible(false); menuItem10->setPosition(Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height/2).x, (Vec2(origin.x+visibleSize.width/2, origin.y+visibleSize.height).y - (index) * 40)); menuItem10->setCallback([&](cocos2d::Ref *sender) { // hide everything else on the screen auto dirs = Director::getInstance()->getRunningScene(); dirs->removeChildByName("sprite30"); dirs->removeChildByName("sprite31"); dirs->removeChildByName("sprite32"); dirs->removeChildByName("sprite33"); auto newSprite = Sprite::create("Blue_Front1.png"); newSprite->setPosition(100, 256); newSprite->setName("sprite34"); newSprite->setAnchorPoint(Vec2(0.5,0.5)); newSprite->setOpacity(0.0f); dirs->addChild(newSprite,1); auto newSprite2 = Sprite::create("Blue_Front1.png"); newSprite2->setPosition(200, 256); newSprite2->setName("sprite35"); newSprite2->setAnchorPoint(Vec2(0.5,0.5)); newSprite2->setOpacity(99); dirs->addChild(newSprite2,1); auto newSprite3 = Sprite::create("Blue_Front1.png"); newSprite3->setPosition(300, 256); newSprite3->setName("sprite36"); newSprite3->setAnchorPoint(Vec2(0.5,0.5)); newSprite3->setOpacity(89); dirs->addChild(newSprite3,1); auto newSprite4 = Sprite::create("Blue_Front1.png"); newSprite4->setPosition(400, 256); newSprite4->setName("sprite37"); newSprite4->setAnchorPoint(Vec2(0.5,0.5)); newSprite4->setOpacity(79); dirs->addChild(newSprite4,1); auto anode = dirs->getChildByName("menuNode"); auto bnode = anode->getChildByName("menu"); auto cnode = bnode->getChildByName("menuItem10"); cnode->setVisible(false); //auto dnode = bnode->getChildByName("menuItem10"); //dnode->setVisible(true); }); auto menu = Menu::create(menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuItem6, menuItem7, menuItem8, menuItem9, menuItem10, NULL); menu->setName("menu"); menuNode->addChild(menu, 1); menu->setPosition(Vec2::ZERO); scene->addChild(menuNode, 2); // return the scene return scene; }
// on "init" you need to initialize your instance bool Pause::init() { ////////////////////////////// // 1. super init first if ( !LayerColor::initWithColor(Color4B(0,0,0,50),800,480) ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //adding menu items auto Exit=MenuItemFont::create("Exit",CC_CALLBACK_1(Pause::Exit,this)); auto Resume=MenuItemFont::create("Resume",CC_CALLBACK_1(Pause::Resume,this)); Exit->setColor(Color3B(0,0,0)); Exit->setFontSizeObj(40.0); Resume->setColor(Color3B(0,0,0)); Resume->setFontSizeObj(40.0); //adding labels that will clarify graphic buttons' meanings auto MusicT=Label::create(); auto AudioT=Label::create(); MusicT->setString("Music"); AudioT->setString("Audio"); MusicT->setSystemFontSize(40.0); AudioT->setSystemFontSize(40.0); MusicT->setColor(Color3B(0,0,0)); AudioT->setColor(Color3B(0,0,0)); MusicT->setPosition(Point(visibleSize.width/2-50,(visibleSize.height/5)*4)); AudioT->setPosition(Point(visibleSize.width/2-50,(visibleSize.height/5)*3)); this->addChild(MusicT); this->addChild(AudioT); //creating grahic buttons auto MusicON=MenuItemImage::create("MUSICON.png","MUSICON.png"); auto MusicOFF=MenuItemImage::create("MUSICOFF.png","MUSICOFF.png"); auto AudioON=MenuItemImage::create("MUSICON.png","MUSICON.png"); auto AudioOFF=MenuItemImage::create("MUSICOFF.png","MUSICOFF.png"); //adding toggles to support two-state graphic buttons auto ToggleM=MenuItemToggle::createWithCallback(CC_CALLBACK_1(Pause::SwitchMusic,this),MusicON,MusicOFF,NULL); auto ToggleA=MenuItemToggle::createWithCallback(CC_CALLBACK_1(Pause::SwitchAudio,this),AudioON,AudioOFF,NULL); //setting items' positions ToggleM->setPosition(Point(visibleSize.width/2+50,(visibleSize.height/5)*4)); ToggleA->setPosition(Point(visibleSize.width/2+50,(visibleSize.height/5)*3)); Resume->setPosition(Point(visibleSize.width/2+50,(visibleSize.height/5)*2)); Exit->setPosition(Point(visibleSize.width/2+50,(visibleSize.height/5)*1)); //making sure that graphics will be displayed properly for disabled music/audio MusicD& md=MusicD::getHandle(); if(!md.GetIsMusicPlaying()) { ToggleM->setSelectedIndex(1); } if(!md.GetIsAudioPlaying()) { ToggleA->setSelectedIndex(1); } //adding menu auto *menu=Menu::create(ToggleA,ToggleM,Resume,Exit,NULL); menu->setPosition(Point(0,0)); this->addChild(menu); //adding touch swallowing event listener auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan=CC_CALLBACK_2(Pause::onTouchBegan,this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this); return true; }
//------------------------------------------------------------------ // // MenuLayerMainMenu // //------------------------------------------------------------------ MenuLayerMainMenu::MenuLayerMainMenu() { _touchListener = EventListenerTouchOneByOne::create(); _touchListener->setSwallowTouches(true); _touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this); _touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this); _touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this); _touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this); _eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1); // Font Item auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23)); auto spriteSelected = Sprite::create(s_MenuItem, Rect(0,23*1,115,23)); auto spriteDisabled = Sprite::create(s_MenuItem, Rect(0,23*0,115,23)); auto item1 = MenuItemSprite::create(spriteNormal, spriteSelected, spriteDisabled, CC_CALLBACK_1(MenuLayerMainMenu::menuCallback, this) ); // Image Item auto item2 = MenuItemImage::create(s_SendScore, s_PressSendScore, CC_CALLBACK_1(MenuLayerMainMenu::menuCallback2, this) ); // Label Item (LabelAtlas) auto labelAtlas = LabelAtlas::create("0123456789", "fonts/labelatlas.png", 16, 24, '.'); auto item3 = MenuItemLabel::create(labelAtlas, CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackDisabled, this) ); item3->setDisabledColor( Color3B(32,32,64) ); item3->setColor( Color3B(200,200,255) ); // Font Item auto item4 = MenuItemFont::create("I toggle enable items", [&](Ref *sender) { _disabledItem->setEnabled(! _disabledItem->isEnabled() ); }); item4->setFontSizeObj(20); item4->setFontName("fonts/Marker Felt.ttf"); // Label Item (LabelBMFont) auto label = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "configuration"); auto item5 = MenuItemLabel::create(label, CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackConfig, this)); // Testing issue #500 item5->setScale( 0.8f ); // Events MenuItemFont::setFontName("fonts/Marker Felt.ttf"); // Bugs Item auto item6 = MenuItemFont::create("Bugs", CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackBugsTest, this)); // Font Item auto item7= MenuItemFont::create("Quit", CC_CALLBACK_1(MenuLayerMainMenu::onQuit, this)); auto item8 = MenuItemFont::create("Remove menu item when moving", CC_CALLBACK_1(MenuLayerMainMenu::menuMovingCallback, this)); auto color_action = TintBy::create(0.5f, 0, -255, -255); auto color_back = color_action->reverse(); auto seq = Sequence::create(color_action, color_back, nullptr); item7->runAction(RepeatForever::create(seq)); auto menu = Menu::create( item1, item2, item3, item4, item5, item6, item7, item8, nullptr); menu->alignItemsVertically(); // elastic effect auto s = Director::getInstance()->getWinSize(); int i=0; for(const auto &child : menu->getChildren()) { auto dstPoint = child->getPosition(); int offset = (int) (s.width/2 + 50); if( i % 2 == 0) offset = -offset; child->setPosition( Vec2( dstPoint.x + offset, dstPoint.y) ); child->runAction( EaseElasticOut::create(MoveBy::create(2, Vec2(dstPoint.x - offset,0)), 0.35f) ); i++; } _disabledItem = item3; item3->retain(); _disabledItem->setEnabled( false ); addChild(menu); menu->setPosition(Vec2(s.width/2, s.height/2)); menu->setScale(0); menu->runAction(ScaleTo::create(1,1)); }