bool LevelSelectScene::init() { if (!cocos2d::Scene::init()) return false; mScreenSize = cocos2d::Director::getInstance()->getWinSize(); mVisibleSize = cocos2d::Director::getInstance()->getVisibleSize(); mOrigin = cocos2d::Director::getInstance()->getVisibleOrigin(); cocos2d::Texture2D::TexParams texParams; texParams.magFilter = GL_LINEAR; texParams.minFilter = GL_LINEAR; texParams.wrapS = GL_REPEAT; texParams.wrapT = GL_REPEAT; cocos2d::Sprite* bg = cocos2d::Sprite::create("bg.png", cocos2d::Rect(mOrigin.x, mOrigin.y, mVisibleSize.width, mVisibleSize.height)); bg->getTexture()->setTexParameters(texParams); bg->setPosition(cocos2d::Vec2(mOrigin.x + mVisibleSize.width/2, mOrigin.y + mVisibleSize.height/2)); addChild(bg); auto logo = cocos2d::Sprite::createWithSpriteFrameName("levels_title"); logo->setPosition(cocos2d::Vec2(mOrigin.x + mVisibleSize.width/2, mOrigin.y + mVisibleSize.height * 0.92f)); this->addChild(logo); mMenu = cocos2d::Menu::create(); mMenu->ignoreAnchorPointForPosition(false); mMenu->setPosition(cocos2d::Vec2(mOrigin.x, mOrigin.y)); mMenu->setAnchorPoint(cocos2d::Vec2::ZERO); mMenu->setContentSize(cocos2d::Size(mVisibleSize.width, mVisibleSize.height)); this->addChild(mMenu); cocos2d::MenuItem* exitBtn = cocos2d::MenuItemSprite::create(cocos2d::Sprite::createWithSpriteFrameName("back_btn"), nullptr, [] (cocos2d::Ref* btn) { cocos2d::Director::getInstance()->popScene(); }); exitBtn->setPosition(9, 9); mMenu->addChild(exitBtn); cocos2d::MenuItem* menuItem; float width = (mVisibleSize.width - 20) / 3; float height = (mVisibleSize.height - 40) / 5; int n = LEVEL_NR; //cocos2d::SpriteFrame* frame = cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("bitmap_font"); //cocos2d::log("rect %f %f", frame->getOriginalSize().width, frame->getOriginalSizeInPixels().height); for (int y = 0; y < 5; y++) { for (int x = 0; x < 3; x++) { int levelNr = y * 3 + x; if (levelNr >= n) break; menuItem = cocos2d::MenuItem::create(); menuItem->setPosition(cocos2d::Vec2(10 + width * x, mVisibleSize.height - 25 - y * height)); menuItem->setContentSize(cocos2d::Size(width, height)); menuItem->setAnchorPoint(cocos2d::Vec2::ANCHOR_TOP_LEFT); menuItem->setTag(LEVELS[levelNr].getId()); mMenu->addChild(menuItem); cocos2d::Sprite* btnBg = cocos2d::Sprite::createWithSpriteFrameName("level_btn"); btnBg->setPosition(width/2, height * 0.60f); btnBg->setTag(99999); menuItem->addChild(btnBg); cocos2d::Label* label = cocos2d::Label::createWithTTF(helpers::String::format("%d", levelNr+1), "fonts/semibold.otf", 6); label->enableShadow(cocos2d::Color4B::BLACK, cocos2d::Size(0.25f, -0.25f)); label->setPosition(btnBg->getContentSize().width/2, btnBg->getContentSize().height*0.5f); /* cocos2d::Label* label = cocos2d::Label::createWithCharMap(frame->getTexture(), frame->getOriginalSizeInPixels().width/10, frame->getOriginalSizeInPixels().height, '0'); label->setString(helpers::String::format("%d", levelNr+1)); label->setPosition(btnBg->getContentSize().width/2, btnBg->getContentSize().height*0.5f); *//* cocos2d::LayerColor* lc = cocos2d::LayerColor::create(cocos2d::Color4B::RED); lc->ignoreAnchorPointForPosition(false); lc->setPosition(btnBg->getContentSize().width/2, btnBg->getContentSize().height/2); lc->setAnchorPoint(cocos2d::Vec2::ANCHOR_MIDDLE); lc->setContentSize(label->getContentSize()); btnBg->addChild(lc); */ btnBg->addChild(label); for (int i = 0; i < 3; i++) { cocos2d::Sprite* star = cocos2d::Sprite::createWithSpriteFrameName("star_b"); star->setPosition(width / 4 * (i+1), i == 1 ? 5 : 6); star->setTag(i+1); menuItem->addChild(star); } } } auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher(); auto listener = cocos2d::EventListenerKeyboard::create(); listener->onKeyPressed = CC_CALLBACK_2(LevelSelectScene::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(LevelSelectScene::onKeyReleased, this); dispatcher->addEventListenerWithSceneGraphPriority(listener, this); if (AppDelegate::pluginAnalytics != nullptr) { cocos2d::plugin::LogEventParamMap params; params.insert(cocos2d::plugin::LogEventParamPair(GameScene::ANALYTICS_GAME_MODE_INDEX, "mode_3")); AppDelegate::pluginAnalytics->logPageView("level_select", ¶ms); } return true; }
void TouchesPerformTest3::onEnter() { PerformBasicLayer::onEnter(); auto s = Director::getInstance()->getWinSize(); // add title auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 32); addChild(label, 1); label->setPosition(Vec2(s.width/2, s.height-50)); #define TOUCH_PROFILER_NAME "TouchProfileName" #define TOUCHABLE_NODE_NUM 1000 srand((unsigned)time(nullptr)); for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i) { int zorder = rand() % TOUCHABLE_NODE_NUM; auto layer = new TouchableLayer(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(TouchableLayer::onTouchBegan, layer); listener->onTouchMoved = CC_CALLBACK_2(TouchableLayer::onTouchMoved, layer); listener->onTouchEnded = CC_CALLBACK_2(TouchableLayer::onTouchEnded, layer); listener->onTouchCancelled = CC_CALLBACK_2(TouchableLayer::onTouchCancelled, layer); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer); addChild(layer, zorder); layer->release(); } auto emitEventlabel = Label::createWithSystemFont("Emit Touch Event", "", 24); auto menuItem = MenuItemLabel::create(emitEventlabel, [this](Ref* sender){ CC_PROFILER_PURGE_ALL(); std::vector<Touch*> touches; for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i) { Touch* touch = new Touch(); touch->setTouchInfo(i, 10, (i+1) * 10); touches.push_back(touch); } EventTouch event; event.setEventCode(EventTouch::EventCode::BEGAN); event.setTouches(touches); for (int i = 0; i < 100; ++i) { CC_PROFILER_START(TOUCH_PROFILER_NAME); _eventDispatcher->dispatchEvent(&event); CC_PROFILER_STOP(TOUCH_PROFILER_NAME); } CC_PROFILER_DISPLAY_TIMERS(); for (auto& touch : touches) { touch->release(); } }); menuItem->setPosition(Vec2(0, -20)); auto menu = Menu::create(menuItem, nullptr); addChild(menu); }
bool RogueScene::init() { // 1. super init first if ( !Layer::init() ) { return false; } // 乱数 srand((unsigned int)time(NULL)); // TouchEvent settings auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(RogueScene::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(RogueScene::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(RogueScene::onTouchEnded, this); // this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); this->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1); auto winSize = Director::getInstance()->getWinSize(); // --------------------- // タイルマップを生成 // --------------------- auto pTiledMap = TMXTiledMap::create("tmx/desert.tmx"); pTiledMap->setPosition(Point::ZERO); this->addChild(pTiledMap, RogueScene::zTiledMapIndex, RogueScene::kTiledMapTag); m_baseMapSize = pTiledMap->getMapSize(); m_baseTileSize = pTiledMap->getTileSize(); m_baseContentSize = pTiledMap->getContentSize(); m_mapManager.init(0, (int)m_baseMapSize.height, 0, (int)m_baseMapSize.width); // 使ってなかった // // フロントレイヤー // auto pFrontLayer = Layer::create(); // pTiledMap->addChild(pFrontLayer, // RogueScene::TiledMapIndex::zTiledMapFrontIndex, // RogueScene::TiledMapTag::kTiledMapFrontTag); // エネミーレイヤー auto pEnemyLayer = Layer::create(); pTiledMap->addChild(pEnemyLayer, RogueScene::TiledMapIndex::zTiledMapEnemyBaseIndex, RogueScene::TiledMapTag::kTiledMapEnemyBaseTag); // ドロップアイテムレイヤー auto pDropItemLayer = Layer::create(); pTiledMap->addChild(pDropItemLayer, RogueScene::TiledMapIndex::zTiledMapDropItemBaseIndex, RogueScene::TiledMapTag::kTiledMapDropItemBaseTag); // 障害物をmapManagerに適応する auto pColisionLayer = pTiledMap->getLayer("colision"); for (int x = 0; x < m_baseMapSize.width; x++) { for (int y = 0; y < m_baseMapSize.height; y++) { if (pColisionLayer->getTileAt(Point(x, y))) { MapIndex mapIndex = {x, y, MoveDirectionType::MOVE_NONE}; auto tileMapIndex = mapIndexToTileIndex(mapIndex); m_mapManager.addObstacle(&tileMapIndex); } } } // --------------------- // グリッド線を生成 // --------------------- auto draw = DrawNode::create(); draw->setPosition(Point::ZERO); // 線の太さと色 float lineSize = 1 * 0.5; Color4F color = Color4F::MAGENTA; // 縦線を引く for (int x = 1; x < m_baseMapSize.width; x++) { float xPoint = x * m_baseTileSize.width; draw->drawSegment(Point(xPoint, 0), Point(xPoint, m_baseContentSize.height), lineSize, color); } // 横線を引く for (int y = 1; y < m_baseMapSize.height; y++) { float yPoint = y * m_baseTileSize.height; draw->drawSegment(Point(0, yPoint), Point(m_baseContentSize.width, yPoint), lineSize, color); } // マップに追加 pTiledMap->addChild(draw, RogueScene::TiledMapIndex::zGridLineIndex, RogueScene::TiledMapTag::kGridLineTag); //------------------------- // ステータスバー? //------------------------- auto statusLayer = LayerColor::create(Color4B::BLACK); statusLayer->setContentSize(Size(winSize.width, m_baseTileSize.height * 0.8)); statusLayer->setPosition(Point(0, winSize.height - statusLayer->getContentSize().height)); // TODO: あとで更新する auto sampleText = LabelTTF::create(" --F Lv-- HP ---/--- 満腹度 ---/--- - G", GAME_FONT(16), 16); sampleText->setPosition(Point(sampleText->getContentSize().width / 2, statusLayer->getContentSize().height / 2)); statusLayer->addChild(sampleText); this->addChild(statusLayer, RogueScene::zStatusBarIndex, RogueScene::kStatusBarTag); // // 下のステータスバー2 // auto pStatusLayer2 = LayerColor::create(Color4B::BLACK); // pStatusLayer2->setContentSize(Size(m_baseTileSize.width, m_baseTileSize.height)); // pStatusLayer2->setPosition(Point(0, 0)); // // // TODO: アイコン表示するかな(ステータスバー2?) // auto pFaceSprite = Sprite::createWithSpriteFrame(SpriteFrame::create("actor_4_f.png", Rect(0, 0, 96, 96))); // float scale = 1.0f / 3.0f; // pFaceSprite->setScale(scale, scale); // // pFaceSprite->setContentSize(pFaceSprite->getContentSize() * scale); // // CCLOG("getContentSize (%f, %f) ", pFaceSprite->getContentSize().width, pFaceSprite->getContentSize().height); // // pFaceSprite->setPosition(Point(pFaceSprite->getContentSize().width / 2, pFaceSprite->getContentSize().height / 2)); // pFaceSprite->setPosition(Point(pFaceSprite->getContentSize().width * pFaceSprite->getScaleX() / 2, pFaceSprite->getContentSize().height * pFaceSprite->getScaleY() / 2)); // pStatusLayer2->addChild(pFaceSprite); // // this->addChild(pStatusLayer2, RogueScene::zStatusBar2Index, RogueScene::kStatusBar2Tag); //------------------------- // ゲームログ表示 //------------------------- // float startWidth = pFaceSprite->getContentSize().width * pFaceSprite->getScaleX(); auto pGameLogLayer = LayerColor::create(Color4B(0, 0, 0, 192)); pGameLogLayer->setContentSize(Size(winSize.width * 0.8, m_baseTileSize.height * 1.5)); pGameLogLayer->setPosition(winSize.width / 2 - pGameLogLayer->getContentSize().width / 2, 0); int baseFontSize = 10; auto pLogTextLabel = LabelTTF::create("", GAME_FONT(baseFontSize), baseFontSize, Size::ZERO, TextHAlignment::LEFT, TextVAlignment::TOP); pLogTextLabel->setPosition(Point(pLogTextLabel->getContentSize().width / 2 + pLogTextLabel->getFontSize() / 4, pGameLogLayer->getContentSize().height - pLogTextLabel->getContentSize().height / 2 - pLogTextLabel->getFontSize() / 4)); pGameLogLayer->addChild(pLogTextLabel); this->addChild(pGameLogLayer, RogueScene::zGameLogIndex, RogueScene::kGameLogTag); // ------------------------ // ミニマップ // ------------------------ // 青で半透明 auto miniMapLayer = LayerColor::create(Color4B(0, 0, 196, 128)); // 1/8サイズ miniMapLayer->setContentSize(Size(m_baseMapSize.width * m_baseTileSize.width / 8, m_baseMapSize.height * m_baseTileSize.height / 8)); // ステータスバーの下くらい miniMapLayer->setPosition(0, miniMapLayer->getPositionY() + winSize.height - miniMapLayer->getContentSize().height - statusLayer->getContentSize().height); this->addChild(miniMapLayer, RogueScene::zMiniMapIndex, RogueScene::kMiniMapTag); // ------------------------ // イベントリ作成 // ------------------------ showItemList(1); hideItemList(); // --------------------- // プレイヤー生成 // --------------------- ActorSprite::ActorDto actorDto; actorDto.name = "ジニー"; actorDto.faceImgId = 0; actorDto.imageResId = 1015; // 基本 actorDto.attackRange = 1; actorDto.movePoint = 5; actorDto.playerId = 4; // 攻守 actorDto.attackPoint = 5; actorDto.defencePoint = 1; // 経験値 actorDto.exp = 0; actorDto.nextExp = 10; // HP actorDto.hitPoint = 15; actorDto.hitPointLimit = 15; actorDto.lv = 1; // 満腹度?精神力? actorDto.magicPoint = 100; actorDto.magicPointLimit = 100; ActorMapItem actorMapItem; actorMapItem.mapDataType = MapDataType::PLAYER; // 画面の中心(固定) actorMapItem.mapIndex = pointToIndex(Point(winSize.width/2, winSize.height/2)); actorMapItem.seqNo = 1; actorMapItem.moveDist = actorDto.movePoint; actorMapItem.attackDist = actorDto.attackRange; actorMapItem.moveDone = false; actorMapItem.attackDone = false; auto actorSprite = ActorSprite::createWithActorDto(actorDto); actorSprite->setPosition(indexToPoint(actorMapItem.mapIndex)); // 画面の中心 actorSprite->setActorMapItem(actorMapItem); actorSprite->runBottomAction(); // プレイヤーは画面中心にくるのでmapLayerに追加しない this->addChild(actorSprite, RogueScene::zActorBaseIndex, (RogueScene::kActorBaseTag + actorMapItem.seqNo)); // マップに追加 m_mapManager.addActor(actorSprite->getActorMapItem()); refreshStatus(); // プレイヤーの位置表示用(同じく1/8サイズ) auto miniMapActorLayer = LayerColor::create(Color4B::YELLOW); // タイルの1/8サイズ miniMapActorLayer->setContentSize(m_baseTileSize / 8); // 現在位置からPositionを取得して1/8にする miniMapActorLayer->setPosition(indexToPointNotTileSize(actorSprite->getActorMapItem()->mapIndex) / 8); // 移動時に更新できるようにplayerIdをtag管理 miniMapActorLayer->setTag(actorSprite->getTag()); // add miniMapLayer->addChild(miniMapActorLayer); // --------------------- // 敵キャラ生成 // --------------------- ActorSprite::ActorDto enemyDto; enemyDto.name = "スライム"; enemyDto.faceImgId = 0; enemyDto.imageResId = 1011; // 基本 enemyDto.attackRange = 1; // TODO: 未使用 enemyDto.movePoint = 10; // 索敵範囲 enemyDto.playerId = 901; // 攻守 enemyDto.attackPoint = 2; enemyDto.defencePoint = 0; // 経験値 enemyDto.exp = 0; enemyDto.nextExp = 10; // HP enemyDto.hitPoint = 10; enemyDto.hitPointLimit = 10; enemyDto.lv = 1; // 満腹度?精神力? enemyDto.magicPoint = 100; enemyDto.magicPointLimit = 100; MapIndex enemyMapIndex1 = {4, 4, MoveDirectionType::MOVE_DOWN}; tileSetEnemyActorMapItem(enemyDto, enemyMapIndex1); ActorSprite::ActorDto enemyDto2 = enemyDto; MapIndex enemyMapIndex2 = {14,12, MoveDirectionType::MOVE_DOWN}; tileSetEnemyActorMapItem(enemyDto2, enemyMapIndex2); ActorSprite::ActorDto enemyDto3 = enemyDto; MapIndex enemyMapIndex3 = {20,4, MoveDirectionType::MOVE_DOWN}; tileSetEnemyActorMapItem(enemyDto3, enemyMapIndex3); //------------------------- // アイテム配置 //------------------------- DropItemSprite::DropItemDto dropItemDto; dropItemDto.itemId = 1; dropItemDto.imageResId = 64; // imageId 10064 dropItemDto.name = "ポーション"; MapIndex mapIndex = {7, 5, MoveDirectionType::MOVE_NONE}; tileSetDropMapItem(dropItemDto, mapIndex); DropItemSprite::DropItemDto dropItemDto2; dropItemDto2.itemId = 2; dropItemDto2.imageResId = 168; // imageId 10168 dropItemDto2.name = "ぶどう"; MapIndex mapIndex2 = {10, 9, MoveDirectionType::MOVE_NONE}; tileSetDropMapItem(dropItemDto2, mapIndex2); // ------------------------------- // メニュー // ------------------------------- auto rect = Rect(0, 0, 300, 30); auto capRect = Rect(0, 0, 300, 30); auto pScale9Sprite1 = extension::Scale9Sprite::create("menu_button.png", rect, capRect); pScale9Sprite1->setContentSize(Size(40, 20)); pScale9Sprite1->setOpacity(192); auto pScale9Sprite2 = extension::Scale9Sprite::create("menu_button.png", rect, capRect); pScale9Sprite2->setContentSize(Size(40, 20)); pScale9Sprite2->setOpacity(128); auto pMenuItem1 = MenuItemSprite::create(pScale9Sprite1, pScale9Sprite2, [this](Object *pSender) { CCLOG("menuItem1が押された!"); showItemList(1); }); pMenuItem1->setColor(Color3B::GREEN); pMenuItem1->setPosition(Point(winSize.width - pMenuItem1->getContentSize().width / 2, pMenuItem1->getContentSize().height / 2)); auto pMenu = Menu::create(pMenuItem1, NULL); pMenu->setPosition(Point::ZERO); this->addChild(pMenu, RogueScene::zMenuIndex, RogueScene::kMenuTag); // --------------------------------- // プレイヤーの先行 changeGameStatus(GameStatus::PLAYER_TURN); return true; }
// on "init" you need to initialize your instance bool NewScene::init() { ////////////////////////////// // 1. super init first if ( !LayerColor::initWithColor(Color4B::GREEN) ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //------------------ LABEL------------------------ auto label = Label::createWithTTF("Nueva Escena", "fonts/airstrike3d.ttf", 24); label->setColor(Color3B::BLACK); label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label, 1); //---------- BOTON --------------------- Button *b1 = Button::create("b1.png","b2.png"); b1->setPosition(Point(origin.x + visibleSize.width - b1->getContentSize().width / 2, origin.y + visibleSize.height - b1->getContentSize().height )); this->addChild(b1, 1); b1->addTouchEventListener(CC_CALLBACK_2(NewScene::touchEvent, this)); auto b2 = Button::create("b1.png","b2.png"); b2->setPosition(Point(origin.x + b2->getContentSize().width / 2, origin.y + visibleSize.height / 2)); this->addChild(b2,1); b2->addTouchEventListener(CC_CALLBACK_2(NewScene::touchEvent2,this)); //-----------CHECKBOX---------------- auto checkbox1 = CheckBox::create("ui/3.png","ui/3.png","ui/1.png","ui/3.png","ui/3.png" ); checkbox1->setPosition(Point(origin.x + checkbox1->getContentSize().width / 2, origin.y + visibleSize.height - checkbox1->getContentSize().height)); this->addChild(checkbox1,1); checkbox1->addEventListener(CC_CALLBACK_2(NewScene::selectedEvent, this)); auto checkbox2 = CheckBox::create("ui/3.png","ui/3.png","ui/1.png","ui/3.png","ui/3.png" ); checkbox2->setPosition(Point(origin.y + checkbox1->getContentSize().width / 2, checkbox1->getPositionY() - checkbox1->getContentSize().height)); this->addChild(checkbox2,1); checkbox2->addEventListener(CC_CALLBACK_2(NewScene::selectedEvent, this)); //-----------VOLVER------------------ auto menuItem = MenuItemLabel::create(Label::createWithTTF("Volver", "fonts/airstrike3d.ttf", 34), CC_CALLBACK_1(NewScene::volver, this)); menuItem->setPosition(Point(origin.x + visibleSize.width / 2, origin.y + menuItem->getContentSize().height / 2)); menuItem->setColor(Color3B::BLACK); auto menuGral = Menu::create(menuItem,NULL); menuGral->setPosition(Point::ZERO); this->addChild(menuGral,2); this->setKeyboardEnabled(true); return true; }
void HelloWorld::addPhysicsSprite() { // _scene->getPhysicsWorld()->setUpdateRate(5.0f); // wall 添加物理边境 Size size = VisibleRect::getVisibleRect().size; auto wall = Node::create(); //给节点添加静态矩形刚体(PhysicsBody)并带材质(PhysicsMaterial) wall->setPhysicsBody(PhysicsBody::createEdgeBox(Size(size.width - 5, size.height - 5), PhysicsMaterial(0.1f, 1.0f, 1.0f))); // wall->getPhysicsBody()->setDynamic(false);//设置为静态刚体(Edge // wall->getPhysicsBody()->setEnable(false); wall->getPhysicsBody()->setGroup(1); //组编号 /**一个body的CategoryBitmask和另一个body的ContactTestBitmask的逻辑与的结果不等于0时,接触事件将被发出,否则不发送。 *一个body的CategoryBitmask和另一个body的CollisionBitmask的逻辑与结果不等于0时,他们将碰撞,否则不碰撞 */ // wall->getPhysicsBody()->setCategoryBitmask(0x03);// 0011 碰撞系数编号 // wall->getPhysicsBody()->setContactTestBitmask(0x03);// 0001 碰撞检测编号 // wall->getPhysicsBody()->setCollisionBitmask(0x01);// 0001 碰撞编号 wall->setPosition(VisibleRect::center()); //位置可见区域中心 this->addChild(wall); //多纳的头 auto tou = Sprite::create(shizi_tou_png); // tou->setPhysicsBody(PhysicsBody::createCircle(271, PhysicsMaterial(1000.1f, 0.0f, 1.0f)));//添加半径为271动态圆形刚体 并赋予材质密度1000.1f 反弹力0.0f 摩擦力1.0f auto toubody = PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_tou"); tou->setPhysicsBody(toubody); tou->getPhysicsBody()->setTag(DRAG_BODYS_TAG);//给刚体设置标签 tou->getPhysicsBody()->setMass(0.1);//刚体设置质量 tou->getPhysicsBody()->setGroup(2);//刚体组编号 tou->getPhysicsBody()->setCategoryBitmask(0x01); // 0001 tou->getPhysicsBody()->setContactTestBitmask(0x01); // 0001 tou->getPhysicsBody()->setCollisionBitmask(0x01); // 0001 // tou->getPhysicsBody()->setDynamic(false); 动态刚体是可以设置为静态 tou->getPhysicsBody()->setRotationEnable(false); //设置不可旋转刚体 碰撞后也不会旋转刚体 // tou->getPhysicsBody()->setGravityEnable(false);//设置是否接受重力影响 tou->getPhysicsBody()->setLinearDamping(3.0f); //设置线性阻尼系数 理论是0-1 但是可以大于1 值越大惯性越小 this->addChild(tou); tou->setPosition(VisibleRect::center()); auto touyingzi = Node::create(); touyingzi->setPhysicsBody(PhysicsBody::createCircle(271, PhysicsMaterial(1000.1f, 0.0f, 1.0f))); touyingzi->getPhysicsBody()->setTag(DRAG_BODYS_TAG_Y); //多纳的身子 //PhysicsShapePolygon 是通过点数组来构建不规则的凸多边形;用工具PhysicsEditor 编辑shap 让后导出Chipmunk 格式的plist 中的数据 注:PhysicsEditor Relative为锚点Anchor 设置为cocos默认值(0.5,0.5)才行 , auto shengzi = Sprite::create(shizi_shenzi_png); // auto shengzibody = PhysicsBody::create(); // Point vert1[3] = {Point(109.50000, 71.00000), Point(14.00000, 77.00000), Point(117.50000, 147.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert1, 3, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert2[6] = {Point(-130.50000, -154.00000), Point(-120.50000, 46.00000), Point(-67.50000, 102.00000), Point(14.00000, 77.00000), Point(-4.00000, -93.00000), Point(-63.00000, -178.50000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert2, 6, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert3[6] = {Point(138.50000, 18.00000), Point(110.50000, -177.00000), Point(51.50000, -175.00000), Point(-4.00000, -93.00000), Point(14.00000, 77.00000), Point(109.50000, 71.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert3, 6, PhysicsMaterial(0.1f, 0.0f, 1.0f))); // Point vert4[4] = {Point(-67.50000, 102.00000), Point(-55.00000, 172.50000), Point(-54.00000, 172.50000), Point(14.00000, 77.00000)}; // shengzibody->addShape(PhysicsShapePolygon::create(vert4, 4, PhysicsMaterial(0.1f, 0.0f, 1.0f))); auto shengzibody = PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_shenzi"); shengzi->setPhysicsBody(shengzibody); shengzibody->setTag(DRAG_BODYS_TAG1); shengzibody->setRotationEnable(false); shengzibody->setGroup(2); shengzibody->setMass(0.1); shengzibody->setCategoryBitmask(0x02); //0010 shengzibody->setContactTestBitmask(0x02); //0010 shengzibody->setCollisionBitmask(0x02); //0010 shengzibody->setLinearDamping(3.0f); this->addChild(shengzi); shengzi->setPosition(VisibleRect::bottom() - Point(0, -300)); // auto contactListener = EventListenerPhysicsContactWithBodies::create(tou->getPhysicsBody(), shengzi->getPhysicsBody()); // contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); // _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); //多纳的头 auto b1 = Sprite::create(shizi_youshou_png); b1->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_youshou")); b1->setPosition(VisibleRect::center()+Point(300,0)); b1->getPhysicsBody()->setTag(1); b1->getPhysicsBody()->setRotationEnable(false); this->addChild(b1); auto b2 = Sprite::create(shizi_zuoshou_png); b2->setPhysicsBody(PEShapeCache::getInstance()->getPhysicsBodyByName("shizi_zuoshou")); b2->setPosition(VisibleRect::center()-Point(300,0)); b2->getPhysicsBody()->setRotationEnable(false); b2->getPhysicsBody()->setTag(1); this->addChild(b2); // LabelTTF auto label2 = LabelTTF::create("多纳小狮子爱学习", "Arial", 64); label2->setPhysicsBody(PhysicsBody::createBox(label2->getBoundingBox().size, PhysicsMaterial(0.1f, 0.0f, 1.0f))); label2->getPhysicsBody()->setTag(1); // label2->getPhysicsBody()->setRotationEnable(false); label2->setPosition(VisibleRect::center()+Point(0,300)); addChild(label2, 0); PEShapeCache::getInstance()->removeBodysWithWithFile(buttonsbodys_plist); //注册碰撞检测监听 auto contactListener1 = EventListenerPhysicsContact::create(); contactListener1->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this); contactListener1->onContactPostSolve = CC_CALLBACK_2(HelloWorld::onContactPostSolve, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener1, this); }
bool GameScene::init() { if (!Layer::init()) return false; _state = GameState::STOPPED; std::srand(std::time(nullptr)); _tube_spawn_interval = 0; _game_score = 0; CCLOG("screen width: %.2f, screen height: %.2f", Director::getInstance()->getVisibleSize().width, Director::getInstance()->getVisibleSize().height ); CCLOG("visible origin x: %.2f, visible origin y: %.2f", Director::getInstance()->getVisibleOrigin().x, Director::getInstance()->getVisibleOrigin().y ); _background = Sprite::create(BACKGROUND_FILENAME); _background->setAnchorPoint(Vec2{ 0, 0 }); _background->setPosition(0, 0); this->addChild(_background, BACKGROUND_Z_INDEX); _floor = Sprite::create(FLOOR_FILENAME); _floor->setAnchorPoint(Vec2{ 0, 0 }); _floor->setPosition(0, 0); this->addChild(_floor, FLOOR_Z_INDEX); _robin = Robin::create_sprite(); this->addChild(_robin, ROBIN_Z_INDEX); auto tree_1 = ScrollingComponent::create_sprite(TREE_FILENAME, TREE_SPEED_X, TREE_1_POS, TREE_SCALE); this->addChild(tree_1, TREE_Z_INDEX); _scrolling_components.push_back(tree_1); auto tree_2 = ScrollingComponent::create_sprite(TREE_FILENAME, TREE_SPEED_X, TREE_2_POS, TREE_SCALE); this->addChild(tree_2, TREE_Z_INDEX); _scrolling_components.push_back(tree_2); auto tree_3 = ScrollingComponent::create_sprite(TREE_FILENAME, TREE_SPEED_X, TREE_3_POS, TREE_SCALE); this->addChild(tree_3, TREE_Z_INDEX); _scrolling_components.push_back(tree_3); auto slow_cloud_1 = ScrollingComponent::create_sprite( CLOUD_FILENAME, SLOW_CLOUD_SPEED_X, SLOW_CLOUD_1_POS, SLOW_CLOUD_SCALE); this->addChild(slow_cloud_1, SLOW_CLOUD_Z_INDEX); _scrolling_components.push_back(slow_cloud_1); auto slow_cloud_2 = ScrollingComponent::create_sprite( CLOUD_FILENAME, SLOW_CLOUD_SPEED_X, SLOW_CLOUD_2_POS, SLOW_CLOUD_SCALE); this->addChild(slow_cloud_2, SLOW_CLOUD_Z_INDEX); _scrolling_components.push_back(slow_cloud_2); auto fast_cloud_1 = ScrollingComponent::create_sprite( CLOUD_FILENAME, FAST_CLOUD_SPEED_X, FAST_CLOUD_1_POS, FAST_CLOUD_SCALE); this->addChild(fast_cloud_1, FAST_CLOUD_Z_INDEX); _scrolling_components.push_back(fast_cloud_1); auto fast_cloud_2 = ScrollingComponent::create_sprite( CLOUD_FILENAME, FAST_CLOUD_SPEED_X, FAST_CLOUD_2_POS, FAST_CLOUD_SCALE); this->addChild(fast_cloud_2, FAST_CLOUD_Z_INDEX); _scrolling_components.push_back(fast_cloud_2); auto fast_cloud_3 = ScrollingComponent::create_sprite( CLOUD_FILENAME, FAST_CLOUD_SPEED_X, FAST_CLOUD_3_POS, FAST_CLOUD_SCALE); this->addChild(fast_cloud_3, FAST_CLOUD_Z_INDEX); _scrolling_components.push_back(fast_cloud_3); auto mount_1 = ScrollingComponent::create_sprite( MOUNT_FILENAME, MOUNT_SPEED_X, MOUNT_1_POS, MOUNT_SCALE); this->addChild(mount_1, MOUNT_Z_INDEX); _scrolling_components.push_back(mount_1); auto mount_2 = ScrollingComponent::create_sprite( MOUNT_FILENAME, MOUNT_SPEED_X, MOUNT_2_POS, MOUNT_SCALE); this->addChild(mount_2, MOUNT_Z_INDEX); _scrolling_components.push_back(mount_2); _score_label = Label::createWithTTF("Score: 0", "Marker Felt.ttf", 24); _score_label->setPosition(LABEL_SCORE_POS); _score_label->setTextColor(Color4B::RED); this->addChild(_score_label, LABEL_SCORE_Z_INDEX); auto touch_listener = EventListenerTouchOneByOne::create(); touch_listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); touch_listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touch_listener, this); this->schedule(schedule_selector(GameScene::update_game)); return true; }
bool ChrsGrid::init(ValueMap level_info, int col, int row) { Node::init(); m_letter_info = level_info;//获取关卡配置信息 //设置棋盘的锚点和大小,锚点为中心 this->setContentSize(Size(col*GRID_WIDTH, row*GRID_WIDTH)); this->setAnchorPoint(Vec2(0.5, 0.5)); //绘制一个矩形,用以测试棋盘范围 auto drawnode = DrawNode::create(); drawnode->drawRect(Vec2(0, 0), Vec2(this->getContentSize().width, this->getContentSize().height), Color4F::RED); //addChild(drawnode); //棋盘底图 auto gridbg = ui::Scale9Sprite::create("grid_bg.png"); gridbg->setContentSize(Size(col * GRID_WIDTH + 10, row * GRID_WIDTH + 10)); gridbg->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2); addChild(gridbg, 0, 1000); //得到单词集合 m_Letters = level_info.at("letter").asValueVector(); //初始化汉字集合 initChrBox(); //生成布局 //根据行列初始化一个空的汉字盒子大小 m_row = row; m_col = col; m_canCrush = false; m_SelectedChrs.clear(); m_NewChrs.clear(); m_ChrsBox.resize(m_col); for (auto &vec : m_ChrsBox) { vec.resize(m_row); } //生成汉字字典树 createTrie(&chr_root, &m_Letters); //1.根据布局大小创建出汉字阵列 //2.布局坐标以左下角为原点,x右y上为正方向 for (int x = 0; x < m_col; x++) { for (int y = 0; y < m_row; y++) { m_ChrsBox[x][y] = createAChr(x, y); } } //判断是否是死图 while (isDeadMap()) { //这里稍后做一个更新的算法 for (int x = 0; x < m_col; x++) { for (int y = 0; y < m_row; y++) { m_ChrsBox[x][y]->removeFromParent(); m_ChrsBox[x][y] = createAChr(x, y); } } } //加入触摸监听 auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(ChrsGrid::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(ChrsGrid::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(ChrsGrid::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(ChrsGrid::onTouchCancelled, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //开始提示倒计时 resetCountdown(); schedule(schedule_selector(ChrsGrid::onCountdownCallBack), 1); log("ChrsGrid init!"); return true; }
bool CLvUpLayer::init() { //CBaseLayer::init(); CBaseLayer::initWithColor(Color4B(0, 0, 0, 200)); m_pListener = EventListenerTouchOneByOne::create(); m_pListener->setSwallowTouches(true); m_pListener->onTouchBegan = CC_CALLBACK_2(CLvUpLayer::onTouchBegan, this); m_pListener->onTouchMoved = CC_CALLBACK_2(CLvUpLayer::onTouchMoved, this); m_pListener->onTouchEnded = CC_CALLBACK_2(CLvUpLayer::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(m_pListener,this); Sprite* m_pSoldierBg = Sprite::createWithSpriteFrameName("hero_bg.png"); m_pSoldierBg->setPosition(Vec2(m_winSize.width*0.5, m_winSize.height*0.5)); addChild(m_pSoldierBg); Size tBg = m_pSoldierBg->getContentSize(); Sprite *pCloseSprite = Sprite::createWithSpriteFrameName("closeBtn.png"); Sprite *pCloseSpriteSelc = Sprite::createWithSpriteFrameName("closeBtn.png"); pCloseSpriteSelc->setColor(Color3B::GRAY); LYMenuItemSprite *pCloseItem = LYMenuItemSprite::create(pCloseSprite,pCloseSpriteSelc,CC_CALLBACK_1(CLvUpLayer::OnExitLvLayer, this)); CommonData::enlargeItemSprite(pCloseItem, 2); m_pMenuCose = Menu::create(pCloseItem,NULL); m_pMenuCose->setPosition(tBg.width*0.95,tBg.height*0.94); m_pSoldierBg->addChild(m_pMenuCose,1, 100); //士兵 float fbtScale = 0.95f; float fLabelScale = 0.55f; Sprite *pSoldierbtn = Sprite::createWithSpriteFrameName("bluebtn.png"); pSoldierbtn->setScale(fbtScale); Sprite *pSoldierbtnSelect = Sprite::createWithSpriteFrameName("organge.png"); pSoldierbtnSelect->setScale(fbtScale); Sprite *pSoldierbtnEnable = Sprite::createWithSpriteFrameName("organge.png"); pSoldierbtnEnable->setScale(fbtScale); Label *pLabelSoldier = Label::createWithTTF("士兵","fonts/cuti.ttf",30); pLabelSoldier->setScale(fLabelScale); Size tLable1 = pSoldierbtn->getContentSize(); pLabelSoldier->setPosition(Point(tLable1.width*0.5,tLable1.height*0.5)); pSoldierbtn->addChild(pLabelSoldier); Label *pLabelSoldierSelc = Label::createWithTTF("士兵","fonts/cuti.ttf",30); pLabelSoldierSelc->setScale(fLabelScale); pLabelSoldierSelc->setPosition(Point(tLable1.width*0.5,tLable1.height*0.5)); pSoldierbtnEnable->addChild(pLabelSoldierSelc); Label *pLabelSoldier2 = Label::createWithTTF("士兵","fonts/cuti.ttf",30); pLabelSoldier2->setScale(fLabelScale); pLabelSoldier2->setPosition(Point(tLable1.width*0.5,tLable1.height*0.5)); pSoldierbtnSelect->addChild(pLabelSoldier2); m_pMenuItemSoldier = LYMenuItemSprite::create(pSoldierbtn,pSoldierbtnSelect,pSoldierbtnEnable,CC_CALLBACK_1(CLvUpLayer::OnMenuClose, this)); m_pMenuItemSoldier->setScale(2); m_pMenuItemSoldier->setEnabled(false); m_pMenuItemSoldier->setTag(LvUpTagSoldier); m_pMenuItemSoldier->setPosition(Point(m_winSize.width*0.3,m_winSize.height*0.9)); //能量 Sprite *pEnergybtn = Sprite::createWithSpriteFrameName("bluebtn.png"); pEnergybtn->setScale(fbtScale); Sprite *pEnergybtnSelect = Sprite::createWithSpriteFrameName("organge.png"); pEnergybtnSelect->setScale(fbtScale); Sprite *pEnergybtnEnable = Sprite::createWithSpriteFrameName("organge.png"); pEnergybtnEnable->setScale(fbtScale); Label *pLabelEnergy = Label::createWithTTF("能量","fonts/cuti.ttf",30); pLabelEnergy->setScale(fLabelScale); Size tLable2 = pSoldierbtn->getContentSize(); pLabelEnergy->setPosition(Point(tLable2.width*0.5,tLable2.height*0.5)); pEnergybtn->addChild(pLabelEnergy); Label *pLabelEnergy2 = Label::createWithTTF("能量","fonts/cuti.ttf",30); pLabelEnergy2->setScale(fLabelScale); pLabelEnergy2->setPosition(Point(tLable2.width*0.5,tLable2.height*0.5)); pEnergybtnSelect->addChild(pLabelEnergy2); Label *pLabelEnergyDis = Label::createWithTTF("能量","fonts/cuti.ttf",30); pLabelEnergyDis->setScale(fLabelScale); pLabelEnergyDis->setPosition(Point(tLable2.width*0.5,tLable2.height*0.5)); pEnergybtnEnable->addChild(pLabelEnergyDis); m_pMenuItemEnegy = LYMenuItemSprite::create(pEnergybtn,pEnergybtnSelect,pEnergybtnEnable,CC_CALLBACK_1(CLvUpLayer::OnMenuClose, this)); m_pMenuItemEnegy->setPosition(Point(m_winSize.width*0.5,m_winSize.height*0.9)); m_pMenuItemEnegy->setTag(LvUpTagNengliang); m_pMenuItemEnegy->setScale(2); //建筑 Sprite *pBuildbtn = Sprite::createWithSpriteFrameName("bluebtn.png"); pBuildbtn->setScale(fbtScale); Sprite *pBuildbtnSelect = Sprite::createWithSpriteFrameName("organge.png"); pBuildbtnSelect->setScale(fbtScale); Sprite *pBuildbtnEnable = Sprite::createWithSpriteFrameName("organge.png"); pBuildbtnEnable->setScale(fbtScale); Label *pLabelBuilding = Label::createWithTTF("英雄","fonts/cuti.ttf",30); //Label::createWithSystemFont("英雄","Arial",30); pLabelBuilding->setScale(fLabelScale); Size tLable3 = pSoldierbtn->getContentSize(); pLabelBuilding->setPosition(Point(tLable3.width*0.5,tLable3.height*0.5)); pBuildbtn->addChild(pLabelBuilding); Label *pLabeBuilding2 = Label::createWithTTF("英雄","fonts/cuti.ttf",30); pLabeBuilding2->setScale(fLabelScale); pLabeBuilding2->setPosition(Point(tLable3.width*0.5,tLable3.height*0.5)); pBuildbtnEnable->addChild(pLabeBuilding2); Label *pLabeBuildingDisc = Label::createWithTTF("英雄","fonts/cuti.ttf",30); pLabeBuildingDisc->setScale(fLabelScale); pLabeBuildingDisc->setPosition(Point(tLable3.width*0.5,tLable3.height*0.5)); pBuildbtnSelect->addChild(pLabeBuildingDisc); bool bGril = CommonData::GetSingletonPtr()->IsGirlHeroVisable(); m_pMenuItemBuilding = LYMenuItemSprite::create(pBuildbtn,pBuildbtnSelect,pBuildbtnEnable,CC_CALLBACK_1(CLvUpLayer::OnMenuClose, this)); m_pMenuItemBuilding->setPosition(Point(m_winSize.width*0.7,m_winSize.height*0.9)); m_pMenuItemBuilding->setTag(LVUptagBuilding); m_pMenuItemBuilding->setScale(2); if(!bGril) { m_pMenuItemBuilding->setVisible(false); m_pMenuItemEnegy->setPosition(m_winSize.width*0.7,m_winSize.height*0.9); } Menu * pMenu = Menu::create(m_pMenuItemSoldier,m_pMenuItemEnegy,m_pMenuItemBuilding,NULL); pMenu->setPosition(Vec2::ZERO); addChild(pMenu,2); if((guiderManagerLayer::getInstance()->getGuideType() == GUIDE_UPGRADE_SOLIDER) && (guiderManagerLayer::getInstance()->getStepNum() == 2)) { NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(CLvUpLayer::addSoliderCloseGuide), "SoliderCloseGuide", NULL); } m_pLayer1 = CSoldierLvUpLayer::create(); addChild(m_pLayer1,1,100); if(!CDataManager::getEndedBy(GUIDE_SOLIDER_NUM)) { m_pMenuItemEnegy->setVisible(false); } //debug模式下显示 #ifdef DEBUG_Mode m_pMenuItemEnegy->setVisible(true); #endif if((guiderManagerLayer::getInstance()->getGuideType() == GUIDE_SOLIDER_NUM) && (guiderManagerLayer::getInstance()->getStepNum() == 2)) { m_pMenuItemBuilding->setVisible(false); m_pMenuItemEnegy->setVisible(true); NotificationCenter::getInstance()->addObserver(this, callfuncO_selector(CLvUpLayer::addSoliderCloseGuide), "SoliderCloseGuide", NULL); Vec2 pos = Vec2(m_winSize.width * 0.67f, m_winSize.height * 0.905f); guiderManagerLayer::getInstance()->setStepNum(guiderManagerLayer::getInstance()->getStepNum() + 1); guiderManagerLayer::getInstance()->createLayer(this, GUIDE_SHOW_FINGER, pos); } if((CPersonalData::getInstance()->getUserInfo()->m_iSmallLevel >= 23) && (!CDataManager::getEndedBy(GUIDE_GIRL_HERO))) { //m_pMenuItemBuilding Sprite *pRedPoint = Sprite::createWithSpriteFrameName("newTip.png"); m_pMenuItemBuilding->addChild(pRedPoint, 1, 50); pRedPoint->setScale(0.5f); Vec2 pos = Vec2(m_pMenuItemBuilding->getNormalImage()->getContentSize().width * 0.58f, m_pMenuItemBuilding->getNormalImage()->getContentSize().height * 0.65f); pRedPoint->setPosition(pos); scheduleUpdate(); } return true; }
// on "init" you need to initialize your instance bool ChapterScene::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size 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 a "close" icon to exit the progress. it's an autorelease object auto returnItem = MenuItemImage::create( "button/return.png", "button/return_selected.png", CC_CALLBACK_1(ChapterScene::doReturn, this)); returnItem->setScale(visibleSize.width/20/returnItem->getContentSize().width); returnItem->setPosition(visibleSize.width-returnItem->getContentSize().width/2, visibleSize.height-returnItem->getContentSize().height/2); auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(ChapterScene::menuCloseCallback,this)); closeItem->setScale(visibleSize.width/800); closeItem->setPosition(Vec2(origin.x + visibleSize.width - 0.5*(closeItem->getBoundingBox().getMaxX()-closeItem->getBoundingBox().getMinX()) , origin.y + 0.5*(closeItem->getBoundingBox().getMaxY()-closeItem->getBoundingBox().getMinY()))); // create menu, it's an autorelease object auto menu = Menu::create(returnItem, 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 // add "ChapterScene" splash screen" auto background = Sprite::create("background/bg_001.png"); // position the sprite on the center of the screen background->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); background->setScaleX(visibleSize.width/background->getContentSize().width); background->setScaleY(visibleSize.height/background->getContentSize().height); // add the sprite as a child to this layer this->addChild(background, 0); // add the menu icon std::string images[] = {"background/bgs_002.png", "background/bgs_003.png", "background/bgs_004.png"}; int imagesNum = 3; m_imageWidth = visibleSize.width * 0.6; m_imageHeight = visibleSize.height * 0.6; m_imageSpace = visibleSize.width * 0.05; float imageOrigin = m_imageSpace + m_imageWidth/2; m_xmin = 0; m_x = m_xmax = imagesNum*m_imageWidth + (imagesNum+1)*m_imageSpace - visibleSize.width; for(int i=0; i<imagesNum; i++) { std::string image = images[i]; Sprite *pSprite = Sprite::create(image); pSprite->setScaleX(m_imageWidth/pSprite->getContentSize().width); pSprite->setScaleY(m_imageHeight/pSprite->getContentSize().height); float offset = imageOrigin + (m_imageWidth + m_imageSpace) * i; pSprite->setPosition(origin.x+offset, origin.y+visibleSize.height/2); pSprite->setTag(i); this->addChild(pSprite, 0); m_menuIconVector.pushBack(pSprite); } // register event listener auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(ChapterScene::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(ChapterScene::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(ChapterScene::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; }
bool HomeSceneLayer::init(){ if (!Layer::init()) { return false; } //对手机返回键的监听 auto listener = EventListenerKeyboard::create(); //和回调函数绑定 listener->onKeyReleased = CC_CALLBACK_2(HomeSceneLayer::onKeyReleased,this); //添加到事件分发器中 Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this); //SpriteFrameCache::getInstance()->addSpriteFramesWithFile("UIResource_ch.plist"); auto sky=Sprite::createWithSpriteFrameName("sky.png"); sky->setAnchorPoint(Vec2(1.0,1.0)); sky->setPosition(Vec2(WINSIZE_WIDTH,WINSIZE_HEIGHT)); this->addChild(sky); auto cloud_01=Sprite::createWithSpriteFrameName("cloud_01.png"); cloud_01->setPosition(CLOUD_01_VEC2); this->addChild(cloud_01); auto moveBy_01=MoveBy::create(15,Vec2(WINSIZE_WIDTH-250,0)); auto rev_01=moveBy_01->reverse(); auto seq_01=Sequence::create(moveBy_01,rev_01, NULL); cloud_01->runAction(RepeatForever::create(seq_01)); auto cloud_02=Sprite::createWithSpriteFrameName("cloud_02.png"); cloud_02->setPosition(CLOUD_02_VEC2); this->addChild(cloud_02); auto moveBy_02=MoveBy::create(13,Vec2(-WINSIZE_WIDTH+250,0)); auto rev_02=moveBy_02->reverse(); auto seq_02=Sequence::create(moveBy_02,rev_02, NULL); cloud_02->runAction(RepeatForever::create(seq_02)); auto cloud_03=Sprite::createWithSpriteFrameName("cloud_03.png"); cloud_03->setPosition(Vec2(cloud_03->getContentSize().width/2,400)); this->addChild(cloud_03); auto cloud_04=Sprite::createWithSpriteFrameName("cloud_04.png"); cloud_04->setPosition(Vec2(WINSIZE_WIDTH-50,380)); this->addChild(cloud_04); auto wall=Sprite::createWithSpriteFrameName("wall.png"); wall->setAnchorPoint(Vec2(0.5,0)); wall->setPosition(Vec2(WINSIZE_WIDTH/2,0)); this->addChild(wall); auto Defend_monk=Sprite::createWithSpriteFrameName("Defend_monk.png"); Defend_monk->setPosition(Vec2(320,670)); this->addChild(Defend_monk); auto best_score=Sprite::createWithSpriteFrameName("best_score.png"); best_score->setPosition(Vec2(320,520)); this->addChild(best_score); auto labFont=Label::createWithBMFont("fonts.fnt","0"); if (Value(UserDefault::getInstance()->getStringForKey("string").c_str()).asInt()!=0) { labFont->setString(UserDefault::getInstance()->getStringForKey("string").c_str()); } labFont->setAnchorPoint(Vec2(0,0));//左对齐 labFont->setPosition(Vec2(304,502)); this->addChild(labFont); float baseX=106; //设定初始值,即第一个和尚的X,Y轴坐标。 float baseY=235; for (int i=0;i<5;i++) { //循环5次,创建五个精灵对象 __String *fileName=__String::createWithFormat("monk_0%d.png",i+1); //fileName存储变换的路径字符串 auto monkSpr=Sprite::createWithSpriteFrameName(fileName->getCString()); //获得fileName对象存储的路径 monkSpr->setPosition(Vec2(baseX,baseY)); this->addChild(monkSpr); baseX+=107; //从第二个和尚开始加X坐标值加120 } auto StartItem = MenuItemSprite::create( Sprite::createWithSpriteFrameName("start_01.png"), Sprite::createWithSpriteFrameName("start_02.png"), CC_CALLBACK_1(HomeSceneLayer::menuStartCallback, this)); StartItem->setPosition(Vec2(320,410)); auto CommentItem = MenuItemSprite::create( Sprite::createWithSpriteFrameName("comment_01.png"), Sprite::createWithSpriteFrameName("comment_02.png"), CC_CALLBACK_1(HomeSceneLayer::menucommentCallback, this)); CommentItem->setPosition(Vec2(132,110)); auto FeedbackItem = MenuItemSprite::create( Sprite::createWithSpriteFrameName("feedback_01.png"), Sprite::createWithSpriteFrameName("feedback_02.png"), CC_CALLBACK_1(HomeSceneLayer::menuFeedbackCallback, this)); FeedbackItem->setPosition(Vec2(320,110)); auto ShareItem = MenuItemSprite::create( Sprite::createWithSpriteFrameName("share_01.png"), Sprite::createWithSpriteFrameName("share_02.png"), CC_CALLBACK_1(HomeSceneLayer::menuShareCallback, this)); ShareItem->setPosition(Vec2(508,110)); auto menu = Menu::create(StartItem,CommentItem,FeedbackItem,ShareItem,NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu); return true; }
// on "init" you need to initialize your instance bool TileMap::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); _visibleSize = visibleSize; _origin = origin; auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(TileMap::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); auto label = LabelTTF::create("TileMap", "Arial", 24); label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label, 1); _tileMap = TMXTiledMap::create("bg_TileMap.tmx"); _tileMap->setPosition(Vec2(0,0)); // _tileMap->setScale(_visibleSize.width/_tileMap->getContentSize().width,_visibleSize.height/_tileMap->getContentSize().height); _tileMap->setVisible(true); _background = _tileMap->layerNamed("bg"); _collidable = _tileMap->layerNamed("Meta"); _collidable->setVisible(false); Director::getInstance()->setProjection(Director::Projection::_2D); this->addChild(_tileMap, 0, 50); TMXObjectGroup *group = _tileMap->getObjectGroup("Objects"); ValueMap spawnPoint = group->getObject("SpawnPoint"); float x = spawnPoint["x"].asFloat(); float y = spawnPoint["y"].asFloat(); // ActorManager::getInstance()->_actorTarget = new Vector<Actor*>(50); // ActorManager::getInstance()->_monsterTarget = Vector<Actor*>(50); _actor = Actor::create(); _player = Sprite::create("ninja.png"); _player->setTag(100); _player->setPosition(Vec2(x,y)); // _posInScreen = Vec2(x,y); this->addChild(_player, 2); auto bld = new ActorBlood(100); _actor->setActorSprite(_player); _actor->setActorTag(1); _actor->setActorBlood(bld); ActorManager::getInstance()->_actorTarget.pushBack(_actor); _collision = new CollisionBlood(this, ActorManager::getInstance()->_monsterTarget); for (int i=1; i<6; i++) { ValueMap point = group->getObject("target"+Value(i).asString()); float x = point["x"].asFloat(); float y = point["y"].asFloat(); std::string str = "BasiliskBrown.PNG"; _collision->addTarget(Vec2(x,y), str); } /* * screen Touch */ _listener = EventListenerTouchOneByOne::create(); _listener->onTouchBegan = CC_CALLBACK_2(TileMap::onTouchBegan,this); _listener->onTouchMoved = CC_CALLBACK_2(TileMap::onTouchMoved,this); _listener->onTouchEnded = CC_CALLBACK_2(TileMap::onTouchEnded,this); _listener->setSwallowTouches(true);//²»ÏòÏ´«µÝ´¥Ãþ _eventDispatcher->addEventListenerWithSceneGraphPriority(_listener,this); //_listener->setEnabled(false); /* * AstartPathFinding */ AstartPathFinding* astart = new AstartPathFinding(_tileMap->getMapSize().width, _tileMap->getMapSize().height); _astart = astart; astart->mapInit(); for (int i=0; i<_tileMap->getMapSize().width; i++) for (int j=0; j<_tileMap->getMapSize().height; j++) { if (checkNodePassable(Vec2(i,_tileMap->getMapSize().height-j-1)) == false) astart->getMap(i,j)->setPassable(false); } // for (int i=0; i<_tileMap->getMapSize().width; i++) // for (int j=0; j<_tileMap->getMapSize().height; j++) { // log("checkNodePassable_[%d][%d] = %d",i, j,_astart->getMap(i,j)->getPassable()); // } // log("_tileMap->getMapSize().width = %f",_tileMap->getMapSize().width); // log("_tileMap->getMapSize().height = %f",_tileMap->getMapSize().height); this->scheduleUpdate(); return true; }