bool HelloWorld::onContactBegin(PhysicsContact &contact) { auto shapeA = contact.getShapeA(); auto shapeB = contact.getShapeB(); if ((shapeA->getCategoryBitmask() == eObjectBitmask::FLOOR && shapeB->getCategoryBitmask() == eObjectBitmask::PIXEL) || (shapeA->getCategoryBitmask() == eObjectBitmask::PIXEL && shapeB->getCategoryBitmask() == eObjectBitmask::FLOOR)) { pixel->isContact = true; //pixel->update(); } return true; }
bool PlayScene::onContactBegin(PhysicsContact &contact) { auto shapeA = contact.getShapeA(); auto shapeB = contact.getShapeB(); if ((shapeA->getCategoryBitmask() == objectBitmask::CHARACTER && shapeB->getCategoryBitmask() == objectBitmask::LASER) || (shapeB->getCategoryBitmask() == objectBitmask::CHARACTER && shapeA->getCategoryBitmask() == objectBitmask::LASER)) { // Đụng laser là chết, xử lý sau //isDead = true; isDead = true; } else { if ((shapeA->getCategoryBitmask() == objectBitmask::CHARACTER && shapeB->getCategoryBitmask() == objectBitmask::DIAMOND) || (shapeB->getCategoryBitmask() == objectBitmask::CHARACTER && shapeA->getCategoryBitmask() == objectBitmask::DIAMOND)) { // delete diamond listDiamond.back()->removeDiamond(); listDiamond.pop_back(); } if ((shapeA->getCategoryBitmask() == objectBitmask::CHARACTER && shapeB->getCategoryBitmask() == objectBitmask::BONUS) || (shapeB->getCategoryBitmask() == objectBitmask::CHARACTER && shapeA->getCategoryBitmask() == objectBitmask::BONUS)) { //listLightning.pop_back(); //listLightning.clear(); if (listLightning.empty() == false) { listLightning.back()->removeBonus(); listLightning.clear(); } } } return true; }
//init()より継承 bool MainScene::initWithLevel(int level) { if (!Layer::init()) { return false; } //ウィンドウサイズを取得 auto winSize = Director::getInstance()->getWinSize(); // this->scheduleUpdate(); //背景を描画 auto background = Sprite::create("background.png"); background->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT); // auto parallaxNode = ParallaxNode::create(); this->addChild(parallaxNode); //ステージを作成(Stageクラスから呼び出し) auto stage = Stage::createWithLevel(level); this->setStage(stage); //stageの幅を取得 auto mapWidth = stage->getTiledMap()->getContentSize().width; //バックグラウンドの幅を取得 auto backgroundWidth = background->getContentSize().width; // parallaxNode->addChild(background, 0, Vec2((backgroundWidth - winSize.width) / mapWidth, 0), Vec2::ZERO); this->setParallaxNode(parallaxNode); // 物体が衝突したことを検知するEventListener auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = [this](PhysicsContact& contact) { auto otherShape = contact.getShapeA()->getBody() == _stage->getPlayer()->getPhysicsBody() ? contact.getShapeB() : contact.getShapeA(); auto body = otherShape->getBody(); auto category = body->getCategoryBitmask(); auto layer = dynamic_cast<TMXLayer *>(body->getNode()->getParent()); //衝突した物体が何かを判別し、条件によって行動を起こす //categoryとその後のものを比較 //なぜ&なのかはhttp://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1110789081参照 if (category & static_cast<int>(Stage::TileType::ENEMY)) { // ゲームオーバー。ゲームオーバーメソッドに飛ばす this->onGameOver(); } else if (category & (int)Stage::TileType::COIN) { // コイン layer->removeChild(body->getNode(), true); //コイン効果音 CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(AudioUtils::getFileName("coin").c_str()); //コイン個数を増加させる _coin += 1; } return true; }; // this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); //ステージレイヤーを追加 this->addChild(stage); // タッチしたときにタッチされているフラグをオンにする auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = [this](Touch *touch, Event *event) { this->setIsPress(true); return true; }; listener->onTouchEnded = [this](Touch *touch, Event *event) { this->setIsPress(false); }; listener->onTouchCancelled = [this](Touch *touch, Event *event) { this->setIsPress(false); }; //イベントリスナーの優先順位を定義 this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); // ステージの背景スプライト表示 auto stageBackground = Sprite::create("stage_ui.png"); stageBackground->setPosition(Vec2(stageBackground->getContentSize().width / 2, winSize.height - stageBackground->getContentSize().height / 2.0)); this->addChild(stageBackground); //ナンバーを表示 auto stageLabel = Label::createWithCharMap("numbers.png", 16, 18, '0'); stageLabel->setString(StringUtils::format("%d", _stage->getLevel() + 1)); stageLabel->setPosition(Vec2(60, winSize.height - 22)); this->addChild(stageLabel); // 制限時間を表示 auto secondLabel = Label::createWithCharMap("numbers.png", 16, 18, '0'); secondLabel->setPosition(Vec2(300, winSize.height - 10)); secondLabel->enableShadow(); this->addChild(secondLabel); this->setSecondLabel(secondLabel); // コインの背景 auto coin = Sprite::create("coin.png"); coin->setPosition(Vec2(160, winSize.height - 15)); this->addChild(coin); //数字を表示 auto label = Label::createWithCharMap("numbers.png", 16, 18, '0'); this->addChild(label); label->setPosition(Vec2(200, winSize.height - 10)); label->enableShadow(); this->setCoinLabel(label); return true; }