예제 #1
0
// 碰撞检测
void GameScene::checkCollision()
{
    CCSprite *bird =(CCSprite*)this->getChildByTag(TAG_BIRD);
    CCRect birdRect =bird->boundingBox();
    if (bird->getPositionY()<ground1->boundingBox().size.height)
    {
        gameState=GameOver;
        score=0;
        this->birdFallAction();
        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sound/lh_hit.ogg");
        return;
    }
    for(int i=0;i<hose->hoseList->count();i++)
    {
        CCSprite *hoseSprite =(CCSprite*)hose->hoseList->objectAtIndex(i);
        CCRect hoseRect =hoseSprite->boundingBox();
        if (birdRect.intersectsRect(hoseRect))
        {
            gameState=GameOver;
            score=0;
            this->birdFallAction();
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sound/lh_hit.ogg");
            break;
        }
        // 更新分数
        int pPosX =hoseSprite->getPositionX()+hoseSprite->getContentSize().width*hoseSprite->getScaleX();
        int birdX =bird->getPositionX()-bird->getContentSize().width*bird->getScaleX();
        if (pPosX==birdX)
        {
            score++;
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sound/lh_point.ogg");
            CCLabelBMFont* scoreSprite =(CCLabelBMFont*)this->getChildByTag(TAG_SCORE);
            CCString *strScore =CCString::createWithFormat("%d",score/2);
            scoreSprite->setString(strScore->m_sString.c_str());
        }
    }
    updatehightestRecord();
}
예제 #2
0
void GamePlay::gameOver(float dt) {
//	Director::getInstance()->pause();
	gameState = End;
//invisible frame ready
	if (frameReady != NULL) {
		for (int i = 0; i < SUM_NUM; i++)
			frameReady[i]->setVisible(false);
	}

	boardWin = Sprite::create("board.png");
	boardWin->setPosition(Vec2(-visibleSize.width / 4, visibleSize.height / 2));
	boardWin->setZOrder(30);
	this->addChild(boardWin);
	float haflHeight = boardWin->getContentSize().height / 2;
	float haflWidth = boardWin->getContentSize().width / 2;

	auto jumpto = JumpTo::create(0.5,
			Vec2(visibleSize.width / 2, visibleSize.height / 2 - 3 * PADDING),
			PADDING * 3, 4);
	boardWin->runAction(jumpto);

	updatehightestRecord();

	if (onSound)
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(
				"sound/music_win.ogg");
//congratulation
	Sprite* congratulation = Sprite::create("congratulation.png");
	congratulation->setPosition(
			Vec2(haflWidth,
					haflHeight * 2 - 3 * PADDING
							- congratulation->getContentSize().height));
	congratulation->setZOrder(40);
	boardWin->addChild(congratulation);

//highscore
	Sprite* newhighscore = Sprite::create("newhighscore.png");
	newhighscore->setPosition(
			Vec2(haflWidth,
					congratulation->getPositionY()
							- congratulation->getContentSize().height / 2
							- newhighscore->getContentSize().height / 2
							- 2 * PADDING));
	newhighscore->setZOrder(40);
	boardWin->addChild(newhighscore);

	//TODO
	if (timeplay > 0)
		NativeUtils::submitScore(
				getLeaderBoard(GamePlay::instance()->getLevel()),
				timeplay * 1000);
	if (timeplay > highScore) {
		newhighscore->setVisible(false);
	} else {
		newhighscore->setVisible(true);
	}

//gameover text
	Sprite* gameovertext = Sprite::create("gameovertext.png");
	gameovertext->setPosition(
			Vec2(4 * PADDING + gameovertext->getContentSize().width / 2,
					newhighscore->getPositionY()
							- newhighscore->getContentSize().height / 2
							- gameovertext->getContentSize().height / 2
							- 2 * PADDING));
	gameovertext->setZOrder(40);
	boardWin->addChild(gameovertext);

	float sizewidth = gameovertext->getContentSize().width;
	float sizeheight = gameovertext->getContentSize().height;
//show score
	LabelTTF *currentscore = LabelTTF::create("00:00", "fonts/hlvgchac.ttf",
			40);
	currentscore->setZOrder(50);
	currentscore->setColor(Color3B(Color3B::BLUE));
	showTextTime(currentscore, timeplay);
	currentscore->setPositionX(
			sizewidth + PADDING + currentscore->getContentSize().width / 2);
	currentscore->setPositionY(sizeheight / 2);
	gameovertext->addChild(currentscore);

	LabelTTF *highscore = LabelTTF::create("00:00", "fonts/hlvgchac.ttf", 40);
	highscore->setZOrder(50);
	highscore->setColor(Color3B(Color3B::BLUE));
	showTextTime(highscore, highScore);
	highscore->setPositionX(
			sizewidth + PADDING + currentscore->getContentSize().width / 2);
	highscore->setPositionY(highscore->getContentSize().height / 2);
	gameovertext->addChild(highscore);

	std::string miniModeSprite = getLevelString(
			GamePlay::instance()->getLevel());
	Sprite* miniMode = Sprite::create("mini" + miniModeSprite + ".png");
	miniMode->setPosition(
			Vec2(
					haflWidth * 2 - miniMode->getContentSize().width / 2
							- 3 * PADDING,
					gameovertext->getPositionY()
							+ miniMode->getContentSize().height / 2
							+ 2 * PADDING));
	boardWin->addChild(miniMode);
//button ok
	MenuItemImage* okBtn = MenuItemImage::create("okBtn.png", "okBtn.png",
			CC_CALLBACK_0(GamePlay::gameCloseWinBoard, this));
	auto okMenu = Menu::create(okBtn, nullptr);
	okMenu->setZOrder(40);
	okMenu->setPosition(
			Vec2(haflWidth / 2 + PADDING,
					gameovertext->getPositionY()
							- gameovertext->getContentSize().height / 2
							- okBtn->getContentSize().height / 2
							- 2 * PADDING));
	boardWin->addChild(okMenu);
//button share
	MenuItemImage* shareBtn = MenuItemImage::create("shareBtn.png",
			"shareBtn.png", CC_CALLBACK_0(GamePlay::gameShareWin, this));
	auto shareMenu = Menu::create(shareBtn, nullptr);
	shareMenu->setZOrder(40);
	shareMenu->setPosition(
			Vec2(haflWidth * 3 / 2 - PADDING, okMenu->getPositionY()));
	boardWin->addChild(shareMenu);
}