void HandleImageLoad::imageAsyncLoad(const string &fileName) { string fileNamePic = fileName; if (fileName.find(".png") == std::string::npos) { UtilString::stringReplace(fileNamePic, ".plist", ".png"); } auto fullpath = FileUtils::getInstance()->fullPathForFilename(fileNamePic); // check if file exists if (fullpath.empty() || !FileUtils::getInstance()->isFileExist(fullpath)) { return; } auto textureCache = Director::getInstance()->getTextureCache(); if (textureCache->getTextureForKey(fullpath)) { return; } textureCache->addImageAsync(fileNamePic, CC_CALLBACK_1(HandleImageLoad::imageAsyncCallback, this, fileName)); _numPlistNeedLoad++; }
// on "init" you need to initialize your instance bool CShop::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 3. add your codes below... auto m_textureCache = TextureCache::getInstance(); //添加背景 auto bg = Sprite::createWithTexture(m_textureCache->getTextureForKey("select_degree.jpg")); bg->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(bg); //////////道具种类///////////// //双倍分数 图标 auto scoreSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_score.png")); scoreSprite->setPosition(Vec2(visibleSize.width/4,visibleSize.height*0.6)); this->addChild(scoreSprite); //双倍金币 图标 auto goldSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_gold.png")); goldSprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height*0.6 - scoreSprite->getContentSize().height)); this->addChild(goldSprite); //延时 图标 auto delaySprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_delay.png")); delaySprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 2)); this->addChild(delaySprite); //提示 图标 auto hintSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("prop_hint.png")); hintSprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 3)); this->addChild(hintSprite); //////////玩家当前道具数量///////////// std::string num; //玩家金币数量 auto m_goldSprite = Sprite::createWithTexture( m_textureCache->getTextureForKey("rest_gold.png")); m_goldSprite->setPosition(Vec2(visibleSize.width - m_goldSprite->getContentSize().width*0.6, visibleSize.height - m_goldSprite->getContentSize().height*0.6)); m_goldSprite->runAction(Sequence::create(JumpTo::create(1.0,m_goldSprite->getPosition(),m_goldSprite->getContentSize().height,2),nullptr)); this->addChild(m_goldSprite); num = String::createWithFormat("%d", CScore::getInstance()->getGold())->_string; auto remainLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); remainLabel->setString(num); remainLabel->setScale(1.5); remainLabel->setAlignment(TextHAlignment::RIGHT); remainLabel->setAnchorPoint(Vec2(1,1)); remainLabel->setPosition(Vec2(visibleSize.width, visibleSize.height-m_goldSprite->getContentSize().height)); this->addChild(remainLabel,1,101); /* //金币积分兑换 auto totalSprite1 = Sprite::create("total_change.png"); auto totalSprite2 = Sprite::create("total_change.png"); totalSprite2->setScale(1.2); auto totalGoldItem = MenuItemSprite::create( totalSprite1, totalSprite2 ); totalGoldItem->setCallback(CC_CALLBACK_1(CShop::onChangeItemCallback, this)); totalGoldItem->setPosition(Vec2( visibleSize.width - totalGoldItem->getContentSize().width / 2, visibleSize.height - m_goldSprite->getContentSize().height-remainLabel->getContentSize().height*2)); num = String::createWithFormat("%d", AdHelp::queryPoints())->_string; totalLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); totalLabel->setString(num); totalLabel->setScale(1.5); totalLabel->setAlignment(TextHAlignment::RIGHT); totalLabel->setAnchorPoint(Vec2(1, 1)); totalLabel->setPosition(Vec2(visibleSize.width, visibleSize.height - m_goldSprite->getContentSize().height - remainLabel->getContentSize().height-totalSprite1->getContentSize().height)); this->addChild(totalLabel, 1,100); */ //返回主界面 按钮 auto spriteNor0 = Sprite::createWithTexture( m_textureCache->getTextureForKey("return.png")); auto spriteSel0 = Sprite::createWithTexture( m_textureCache->getTextureForKey("return.png")); spriteSel0->setScale(1.2); auto backItem = MenuItemSprite::create( spriteNor0, spriteSel0 ); backItem->setCallback(CC_CALLBACK_1(CShop::onReturnItemCallback, this)); backItem->setPosition(Vec2(visibleSize.width / 8, visibleSize.height * 0.8)); // auto menu0 = Menu::create(totalGoldItem, backItem, nullptr); auto menu0 = Menu::create(backItem, nullptr); menu0->setPosition(Vec2::ZERO); addChild(menu0); //双倍分数 道具数量 num = String::createWithFormat("%d", CScore::getInstance()->getMultScore())->_string; auto scoreLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); scoreLabel->setString(num); scoreLabel->setScale(1.5); scoreLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6)); this->addChild(scoreLabel,1,102); //双倍金币 num = String::createWithFormat("%d", CScore::getInstance()->getMultGold())->_string; auto goldLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); goldLabel->setString(num); goldLabel->setScale(1.5); goldLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6 - scoreSprite->getContentSize().height)); this->addChild(goldLabel,1, 103); //延时 num = String::createWithFormat("%d", CScore::getInstance()->getPropDelay())->_string; auto delayLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); delayLabel->setString(num); delayLabel->setScale(1.5); delayLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 2)); this->addChild(delayLabel, 1, 104); //提示 num = String::createWithFormat("%d", CScore::getInstance()->getPropHint())->_string; auto hintLabel = Label::createWithCharMap( "digital.png", 20, 20, '0'); hintLabel->setString(num); hintLabel->setScale(1.5); hintLabel->setPosition(Vec2(visibleSize.width *0.5, visibleSize.height*0.6 - scoreSprite->getContentSize().height * 3)); this->addChild(hintLabel, 1, 105); /* auto t = Label::createWithTTF( "test", "fonts/DFPShaoNvW5-GB.ttf", 40); t->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(t, 1);*/ //////////购买道具按钮///////////// SpriteBatchNode* spriteBatchNode = SpriteBatchNode::create("100.png"); this->addChild(spriteBatchNode); auto spriteNor1 = Sprite::createWithTexture(spriteBatchNode->getTexture()); auto spriteSel1 = Sprite::createWithTexture(spriteBatchNode->getTexture()); spriteSel1->setScale(1.2); // spriteBatchNode->addChild(spriteNor1); // spriteBatchNode->addChild(spriteSel1); auto spriteNor2 = Sprite::createWithTexture(spriteBatchNode->getTexture()); auto spriteSel2 = Sprite::createWithTexture(spriteBatchNode->getTexture()); spriteSel2->setScale(1.2); /* spriteBatchNode->addChild(spriteNor2); spriteBatchNode->addChild(spriteSel2); */ SpriteBatchNode* spriteBatchNode1 = SpriteBatchNode::create("300.png"); this->addChild(spriteBatchNode1); auto spriteNor3 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); auto spriteSel3 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); spriteSel3->setScale(1.2); /* spriteBatchNode->addChild(spriteNor3); spriteBatchNode->addChild(spriteSel3); */ auto spriteNor4 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); auto spriteSel4 = Sprite::createWithTexture(spriteBatchNode1->getTexture()); spriteSel4->setScale(1.2); /* spriteBatchNode->addChild(spriteNor4); spriteBatchNode->addChild(spriteSel4); */ //购买道具 分数加倍 按钮 auto scoreItem = MenuItemSprite::create( spriteNor1, spriteSel1 ); scoreItem->setCallback(CC_CALLBACK_1(CShop::onScoreItemCallback, this)); //购买道具 金币加倍 按钮 auto goldItem = MenuItemSprite::create( spriteNor2, spriteSel2 ); goldItem->setCallback(CC_CALLBACK_1(CShop::onGoldItemCallback, this)); //购买道具 延时 按钮 auto delayItem = MenuItemSprite::create( spriteNor3, spriteSel3 ); delayItem->setCallback(CC_CALLBACK_1(CShop::onDelayItemCallback, this)); //购买道具 提示 按钮 auto hintItem = MenuItemSprite::create( spriteNor4, spriteSel4 ); hintItem->setCallback(CC_CALLBACK_1(CShop::onHintItemCallback, this)); auto menu = Menu::create(scoreItem, goldItem, delayItem, hintItem, nullptr); menu->alignItemsVerticallyWithPadding(scoreItem->getContentSize().height); menu->setPosition(Vec2(visibleSize.width *3/ 4, visibleSize.height*0.43)); this->addChild(menu); return true; }
bool GameScene::init() { Layer::init(); auto visibleSize = Director::getInstance()->getVisibleSize(); auto texturecache = TextureCache::getInstance(); //加载背景图 m_bg = Sprite::createWithTexture(texturecache->getTextureForKey("bground1.png")); m_bg->setAnchorPoint(Vec2(0, 0)); m_bg->setTag(100); //1-4个bg的tag默认设置为100,101,102,103 addChild(m_bg); //网格背景 auto board = Sprite::createWithTexture(texturecache->getTextureForKey("board.png")); board->setAnchorPoint(Vec2(0, 1)); board->setPosition(Vec2(0, visibleSize.height)); board->setOpacity(80); //满分255 addChild(board); //初始化网格数据,网格的原点在左下角 m_jewelsgrid = JewelsGrid::create(8, 8); addChild(m_jewelsgrid); m_jewelsgrid->setPosition(0, visibleSize.height - m_jewelsgrid->getRow() * GRID_WIDTH); //倒计时条外框 auto bounusbar_frame = Sprite::createWithTexture(texturecache->getTextureForKey("bonusbar.png")); bounusbar_frame->setPosition(Vec2(visibleSize.width / 2, bounusbar_frame->getContentSize().height / 2 + 10)); addChild(bounusbar_frame); //倒计时条bonusbar_fill.png m_bonusbar = LoadingBar::create("bonusbar_fill.png"); m_bonusbar->setPercent(100); m_bonusbar->setPosition(bounusbar_frame->getPosition()); addChild(m_bonusbar); schedule(schedule_selector(GameScene::onReducingBonus), 0.1); //分数条 auto bonus = Sprite::createWithTexture(texturecache->getTextureForKey("bonus.png")); bonus->setPosition(visibleSize.width - bonus->getContentSize().width / 2 - 10, 80); addChild(bonus); m_scorelabel = Label::createWithTTF("0", "fonts/Marker Felt.ttf", 24); m_scorelabel->setAnchorPoint(Vec2(1, 0)); m_scorelabel->setPosition(visibleSize.width - 10, 35); m_scorelabel->setTag(10); m_scorelabel->enableOutline(Color4B::BLACK, 1); addChild(m_scorelabel); //测试用,刷新宝石阵列按钮 auto updateMenu = MenuItemFont::create("Update Map", CC_CALLBACK_1(GameScene::onUpdateMenuCallback, this)); updateMenu->setAnchorPoint(Vec2(1, 0)); updateMenu->setPosition(visibleSize.width / 2, -visibleSize.height / 2); auto menu = Menu::create(updateMenu, nullptr); addChild(menu); //关闭测试 menu->setEnabled(false); menu->setVisible(false); return true; }
bool GameScene::init() { if (!Layer::init()) { return false; } _deviceOrientation = DeviceOrientation::Portrait; auto visibleSize = Director::getInstance()->getVisibleSize(); //setContentSize(Size(visibleSize.width * 2, visibleSize.height)); // 背景レイヤーの上にメインシーン、レポートシーンを含めるコンテナシーンを重ねて、コンテナシーンを移動させ、背景は固定されるようにする // 背景レイヤー auto bkgLayer = Layer::create(); bkgLayer->setName(name::scene::game::BACKGROUND_LAYER); addChild(bkgLayer); auto cache = Director::getInstance()->getTextureCache(); auto tex = cache->getTextureForKey(name::resource::GAME_BACKGROUND); auto spr = Sprite::createWithTexture(tex); cache->removeTexture(tex); //spr->setName(name::GAME_BACKGROUND); spr->setAnchorPoint(Vec2::ZERO); spr->setPosition(Vec2::ZERO); bkgLayer->addChild(spr); // コンテナレイヤー(メインシーンとレポートシーンを含める) auto containerLayer = Layer::create(); containerLayer->setName(name::scene::game::CONTAINER_LAYER); addChild(containerLayer); auto mainScene = MainScene::create(); mainScene->setName(name::scene::MAIN); mainScene->setAnchorPoint(Vec2::ZERO); mainScene->setPosition(Vec2::ZERO); containerLayer->addChild(mainScene); auto reportScene = ReportScene::create(); reportScene->setName(name::scene::REPORT); reportScene->setAnchorPoint(Vec2::ZERO); reportScene->setPosition(Vec2(720, 0)); containerLayer->addChild(reportScene); // 左スクロールイベント Director::getInstance()->getEventDispatcher()->addCustomEventListener(config::event::ScrollToLeftNotification, [this](EventCustom* evt) { auto vec = Vec2::ZERO; if (DeviceOrientation::Portrait == _deviceOrientation) { vec = Vec2(-720, 0); } else if (DeviceOrientation::Landscape == _deviceOrientation) { vec = Vec2(-1280, 0); } getChildByName(name::scene::game::CONTAINER_LAYER)->runAction(MoveBy::create(0.3f, vec)); }); // 右スクロールイベント Director::getInstance()->getEventDispatcher()->addCustomEventListener(config::event::ScrollToRightNotification, [this](EventCustom* evt) { auto vec = Vec2::ZERO; if (DeviceOrientation::Portrait == _deviceOrientation) { vec = Vec2(720, 0); } else if (DeviceOrientation::Landscape == _deviceOrientation) { vec = Vec2(1280, 0); } // スクロールしてスクロール完了を通知 auto action = Sequence::createWithTwoActions(MoveBy::create(0.3f, vec), CallFunc::create([this]() { Director::getInstance()->getEventDispatcher()->dispatchCustomEvent(config::event::ScrollToRightCompletionNotification); })); getChildByName(name::scene::game::CONTAINER_LAYER)->runAction(action); }); // 端末の向き変更イベント Director::getInstance()->getEventDispatcher()->addCustomEventListener(config::event::OrientationChangedNotification, [this](EventCustom* evt) { auto data = static_cast<ValueMap*>(evt->getUserData()); auto w = data->at("w").asInt(); auto h = data->at("h").asInt(); auto glview = Director::getInstance()->getOpenGLView(); // フレームサイズをセットしてから、デザイン解像度をセット glview->setFrameSize(w, h); if (w > h) { //CCLOG("(landscape) w: %d, h: %d", w, h); glview->setDesignResolutionSize(config::resolution::DESIGN_RESOLUTION_LONG_SPAN, config::resolution::DESIGN_RESOLUTION_SHORT_SPAN, config::resolution::DESIGN_POLICY); _deviceOrientation = DeviceOrientation::Landscape; } else { //CCLOG("(portrait) w: %d, h: %d", w, h); glview->setDesignResolutionSize(config::resolution::DESIGN_RESOLUTION_SHORT_SPAN, config::resolution::DESIGN_RESOLUTION_LONG_SPAN, config::resolution::DESIGN_POLICY); _deviceOrientation = DeviceOrientation::Portrait; } reArrangeChildrens(); }); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) auto keyboardListener = EventListenerKeyboard::create(); keyboardListener->onKeyPressed = [this](EventKeyboard::KeyCode keyCode, Event* event) {}; keyboardListener->onKeyReleased = [this](EventKeyboard::KeyCode keyCode, Event* event) { if (EventKeyboard::KeyCode::KEY_V != keyCode && EventKeyboard::KeyCode::KEY_H != keyCode) return; auto data = ValueMap(); if (EventKeyboard::KeyCode::KEY_V == keyCode) { data["w"] = 540; data["h"] = 960; } else if (EventKeyboard::KeyCode::KEY_H == keyCode) { data["w"] = 960; data["h"] = 540; } auto evtDispatcher = Director::getInstance()->getEventDispatcher(); evtDispatcher->dispatchCustomEvent(config::event::OrientationChangedNotification, &data); }; Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(keyboardListener, this); #endif return true; }
// on "init" you need to initialize your instance bool GameStart::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto m_textureCache = TextureCache::getInstance(); //加载背景 auto bg = Sprite::createWithTexture(m_textureCache->getTextureForKey("bg_main.jpg")); // bg->setScale(1.5); bg->setAnchorPoint(Vec2(0, 0)); bg->setPosition(Vec2(0, 0)); addChild(bg); //加载标题 title_main auto title = Sprite::create("title_main.png"); title->setAnchorPoint(Vec2(0.5, 1)); title->setPosition(Vec2(visibleSize.width/2, visibleSize.height)); addChild(title); //开始游戏 按钮 auto tempStart1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_start.png")); auto tempStart2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_start.png")); tempStart2->setScale(1.2); auto startItem = MenuItemSprite::create( tempStart1, tempStart2, CC_CALLBACK_1(GameStart::startGame, this) ); moveWithBezier(startItem, Vec2(visibleSize.width / 4, visibleSize.height * 3 / 4), Vec2(visibleSize.width * 2 / 3, visibleSize.height / 3)); log("startButton's size %lf, %lf", startItem->getPositionX(), startItem->getPositionY()); //游戏说明 按钮 auto tempHelp1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_about.png")); auto tempHelp2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_about.png")); tempHelp2->setScale(1.2); auto aboutGameItem = MenuItemSprite::create( tempHelp1, tempHelp2, CC_CALLBACK_1(GameStart::aboutGame, this) ); moveWithBezier(aboutGameItem, Vec2(visibleSize.width / 2, visibleSize.height * 2 / 3), Vec2(visibleSize.width * 3 / 4, visibleSize.height / 4)); //游戏难度设置 按钮 auto tempDegree1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_degree.png")); auto tempDegree2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_degree.png")); tempDegree2->setScale(1.2); auto degreeItem = MenuItemSprite::create( tempDegree1, tempDegree2, CC_CALLBACK_1(GameStart::degreeUtil, this) ); moveWithBezier(degreeItem, Vec2(visibleSize.width / 8, visibleSize.height*7 / 8), Vec2(visibleSize.width * 7 / 8, visibleSize.height / 8)); /* //音乐设置 按钮 auto tempMusic1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_music.png")); auto tempMusic2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_music.png")); tempMusic2->setScale(1.2); auto musicItem = MenuItemSprite::create( tempMusic1, tempMusic2, CC_CALLBACK_1(GameStart::musicUtil, this) ); moveWithBezier(musicItem, Vec2(visibleSize.width / 4, visibleSize.height / 4), Vec2(visibleSize.width * 2 / 3, visibleSize.height * 2 / 3)); */ //退出游戏 按钮 auto tempOver1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_quit.png")); auto tempOver2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_quit.png")); tempOver2->setScale(1.2); auto closeItem = MenuItemSprite::create( tempOver1, tempOver2, CC_CALLBACK_1(GameStart::menuCloseCallback, this) ); moveWithBezier(closeItem, Vec2(visibleSize.width * 3 / 4, visibleSize.height / 4), Vec2(visibleSize.width / 4, visibleSize.height * 3 / 4)); // create menu, it's an autorelease object auto menu = Menu::create(startItem, aboutGameItem, degreeItem, closeItem, NULL); menu->alignItemsVerticallyWithPadding(closeItem->getContentSize().height / 2); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); /* //获取金币 按钮 auto tempGold1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("rest_gold.png")); auto tempGold2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("rest_gold.png")); tempGold2->setScale(1.2); auto goldItem = MenuItemSprite::create( tempGold1, tempGold2, CC_CALLBACK_1(GameStart::buyGold, this) ); moveWithBezier(goldItem, Vec2(visibleSize.width / 8, visibleSize.height *0.6), Vec2(visibleSize.width / 4, visibleSize.height * 3 / 5)); */ //高分记录 按钮 auto tempScore1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_rank.png")); auto tempScore2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_rank.png")); tempScore2->setScale(1.2); auto highScoreItem = MenuItemSprite::create( tempScore1, tempScore2, CC_CALLBACK_1(GameStart::highScore, this) ); moveWithBezier(highScoreItem, Vec2(visibleSize.width / 2, visibleSize.height / 3), Vec2(visibleSize.width / 4, visibleSize.height * 3 / 4)); //游戏商城 按钮 auto tempShop1 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_shop.png")); auto tempShop2 = Sprite::createWithTexture(m_textureCache->getTextureForKey("button_shop.png")); tempShop2->setScale(1.2); auto shopItem = MenuItemSprite::create( tempShop1, tempShop2, CC_CALLBACK_1(GameStart::gameShoping, this) ); moveWithBezier(shopItem, Vec2(visibleSize.width * 3 / 4, visibleSize.height / 4), Vec2(visibleSize.width / 3, visibleSize.height * 2 / 3)); // auto menu1 = Menu::create(goldItem,highScoreItem, shopItem, NULL); auto menu1 = Menu::create(highScoreItem, shopItem, NULL); menu1->alignItemsVerticallyWithPadding(closeItem->getContentSize().height / 2); menu1->setPosition(Vec2(-visibleSize.width*0.4,-visibleSize.height/4)); this->addChild(menu1, 1); //监听手机键盘 m_listener = EventListenerKeyboard::create(); m_listener->onKeyReleased = CC_CALLBACK_2(GameStart::onKeyReleased, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority( m_listener, this); //显示banner广告 AdHelp::showAd(2); return true; }