// on "init" you need to initialize your instance bool ChooseMap::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(ChooseMap::getArray), "2PMessage", NULL); CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(ChooseMap::storymode), "storyMessage", NULL); string path = "images/newGame/"; char *mapName[6] = { "chibi", "guandu", "hanzhong", "hulaoguan", "xuzhou", "yiling"}; Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //平铺背景 auto *bg = CCSprite::create("images/background/bg_fight.png"); bg->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(bg, -1); //标题 auto *title = CCSprite::create(path + "map_choose.png"); title->setPosition(ccp(visibleSize.width * 0.1, visibleSize.height * 0.9)); this->addChild(title); title->setScale(1.4f, 1.4f); //hint string hints = XMLParser::parseXML("hintStrings", 3); CCLabelTTF *ttf = CCLabelTTF::create(hints, "【朦胧补】水黑体", 35); ttf->setPosition(ccp(visibleSize.width * 0.85, visibleSize.height * 0.87)); this->addChild(ttf); //上一步 auto* last = CCSprite::create(path+"last.png"); last->setScale(1.4f, 1.4f); auto* last_selected = CCSprite::create(path + "last_selected.png"); last_selected->setScale(1.4f, 1.4f); auto *Last = CCMenuItemSprite::create(last, last_selected, last, this, menu_selector(ChooseMap::backto)); Last->setPosition(ccp(visibleSize.width - 200, 50)); //开始游戏 auto* begin = CCSprite::create(path + "begin.png"); begin->setScale(1.4f, 1.4f); auto* begin_selected = CCSprite::create(path + "begin_selected.png"); begin_selected->setScale(1.4f, 1.4f); auto *Begin = CCMenuItemSprite::create(begin, begin_selected, begin, this, menu_selector(ChooseMap::startGame)); Begin->setPosition(ccp(visibleSize.width - 80, 50)); auto *menu = CCMenu::create(Begin, NULL); menu->addChild(Last, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 2); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [](Touch* touch, Event* event)->bool{ CCLOG("listener1 onTouchBegan"); auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->convertToNodeSpace(touch->getLocation()); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode)){//判断触摸点是否在目标的范围内 return true; } else return false; }; listener->onTouchMoved = [](Touch* touch, Event* event){CCLOG("listener1 onTouchMoved"); }; listener->onTouchEnded = [](Touch* touch, Event* event){ CCMenuItemImage *target = static_cast< CCMenuItemImage*>(event->getCurrentTarget()); if (target->isSelected()){ target->unselected(); } else { SimpleAudioEngine::sharedEngine()->playEffect("select.wav",false);//开始播放背景音效,false表示不循环 target->selected(); } }; for (int k = 0; k < 2; k++){ double f; if (k == 0){ f = 0.58; } else { f = 0.23; } for (int j = 0; j < 3; j++){ int i = 3 * k + j; Sprite *sprite = Sprite::create(path + mapName[i] + ".png"); sprite->setScale(0.5f, 0.5f); map[i] = CCMenuItemImage::create(path + mapName[i] + "zhizhan.png", path + mapName[i] + "zhizhan_selected.png"); map[i]->setScale(0.5f, 0.5f); sprite->setPosition(ccp(visibleSize.width * 0.3 * j + 280, visibleSize.height * f - 50)); map[i]->setPosition(ccp(visibleSize.width * 0.3 * j + 280, visibleSize.height * f + 80)); this->addChild(sprite); this->addChild(map[i], 1); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), map[i]); } } return true; }
// on "init" you need to initialize your instance bool ChooseCharacter::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } string path = "images/newGame/"; string heropath = "images/country/"; char *heroName[40] = { "caocao", "caiwenji", "caoren", "dianwei", "guojia", "xiahoudun", "xiahouyuan", "xuchu", "zhangliao", "zhenbi", "sunquan", "chengpu", "daqiao", "huanggai", "lusu", "luxun", "sunce", "taishici", "xiaoqiao", "zhouyu", "liubei", "guanyinping", "guanyu", "huangzhong", "machao", "weiyan", "zhangfei", "zhangxingcai", "zhaoyun", "zhugeliang", "lvbu", "diaochan", "dongzhuo", "gongsunzan", "liubiao", "liuzhang", "mateng", "yuanshao", "yuanshu", "zhanglu" }; Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //平铺背景 auto *bg = CCSprite::create("images/background/bg_fight.png"); bg->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 2)); this->addChild(bg, -1); //标题 auto *title = CCSprite::create(path+"character_choose.png"); title->setPosition(ccp(visibleSize.width * 0.1, visibleSize.height * 0.9)); this->addChild(title); title->setScale(1.4f, 1.4f); auto *player = CCSprite::create(path + "1P.png"); player->setPosition(ccp(visibleSize.width * 0.25, visibleSize.height * 0.9)); this->addChild(player); player->setScale(1.2f, 1.2f); string hints = XMLParser::parseXML("hintStrings", 2); CCLabelTTF *ttf = CCLabelTTF::create(hints, "【朦胧补】水黑体", 35); ttf->setPosition(ccp(visibleSize.width * 0.85, visibleSize.height * 0.9)); this->addChild(ttf); //确认 auto* back = CCSprite::create("back.png"); back->setScale(1.4f, 1.4f); auto* back_selected = CCSprite::create("back_selected.png"); back_selected->setScale(1.4f, 1.4f); auto *Back = CCMenuItemSprite::create(back, back_selected, back, this, menu_selector(ChooseCharacter::backto)); Back->setPosition(ccp(visibleSize.width - 240, 50)); //退出 auto* next = CCSprite::create(path+"next.png"); next->setScale(1.4f, 1.4f); auto* next_selected = CCSprite::create(path + "next_selected.png"); next_selected->setScale(1.4f, 1.4f); auto *Next = CCMenuItemSprite::create(next, next_selected, next, this, menu_selector(ChooseCharacter::nextto)); Next->setPosition(ccp(visibleSize.width - 100, 50)); auto *menu = CCMenu::create(Next, NULL); menu->addChild(Back, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 2); auto *wei = CCSprite::create(path + "wei.png"); wei->setPosition(ccp(visibleSize.width * 0.17, visibleSize.height * 0.75)); this->addChild(wei); wei->setScale(3.0f, 3.0f); auto *shu = CCSprite::create(path + "shu.png"); shu->setPosition(ccp(visibleSize.width * 0.61, visibleSize.height * 0.75)); this->addChild(shu); shu->setScale(3.0f, 3.0f); auto *wu = CCSprite::create(path + "wu.png"); wu->setPosition(ccp(visibleSize.width * 0.39, visibleSize.height * 0.75)); this->addChild(wu); wu->setScale(3.0f, 3.0f); auto *qun = CCSprite::create(path + "qun.png"); qun->setPosition(ccp(visibleSize.width * 0.83, visibleSize.height * 0.75)); this->addChild(qun); qun->setScale(3.0f, 3.0f); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [](Touch* touch, Event* event)->bool{ CCLOG("listener1 onTouchBegan"); auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->convertToNodeSpace(touch->getLocation()); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode)){//判断触摸点是否在目标的范围内 return true; } else return false; }; listener->onTouchMoved = [](Touch* touch, Event* event){CCLOG("listener1 onTouchMoved"); }; listener->onTouchEnded = [](Touch* touch, Event* event){ CCMenuItemImage *target = static_cast< CCMenuItemImage*>(event->getCurrentTarget()); if (target->isSelected()){ target->unselected(); } else { SimpleAudioEngine::sharedEngine()->playEffect("select.wav",false);//开始播放背景音效,false表示不循环 target->selected(); } }; for (int j = 0; j < 4; j++){ for (int k = 0; k < 2; k++){ for (int l = 0; l < 5; l++){ int i = l + j * 10 + k * 5; hero[i] = CCMenuItemImage::create(heropath + heroName[i] + ".png", heropath + heroName[i] + "_selected.png"); hero[i]->setScale(2.0f, 2.0f); hero[i]->setPosition(ccp(visibleSize.width * (0.12 + 0.1*k + 0.22*j) , visibleSize.height * (0.6 - 0.1*l))); this->addChild(hero[i]); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), hero[i]); } } } return true; }