void GameScene::resetMole(void) { if (hasAvailableHole()) { Hole* hole = findAvailableHole(); this->removeChild(hole->getSprite(), false); int randVal = random() % 100; if (randVal >= 0 && randVal <= 10) { hole->addMole("clefairy.png", CLEFAIRY); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("Clefairy.mp3"); } else if (randVal >= 11 && randVal <= 20) { hole->addMole("dugtrio.png", DUGTRIO); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("triotriotrio.mp3"); } else if (randVal >= 21 && randVal <= 30) { hole->addMole("parasect.png", PARASECT); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("Parasect.mp3"); } else { hole->addMole("diglett.png", DIGLETT); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("Diglett.mp3"); } this->addChild(hole->getSprite()); } }
// on "init" you need to initialize your instance bool GameScene::init() { // 1. super init first if ( !CCLayer::init() ) { return false; } srand(getpid()); _moleInterval = 2.5f; _moleTimer = _moleInterval * 0.99f; _difficultyInterval = 5.0f; _difficultyTimer = _difficultyInterval * 0.99f; CCSize size = CCDirector::sharedDirector()->getWinSize(); _background = CCSprite::create("background.png"); _background->setPosition(ccp(size.width / 2, size.height / 2)); _background->retain(); this->addChild(_background, 0); for (int i = 0; i < 9; i++) { Hole* newHole = new Hole(i); _holes.push_back(newHole); this->addChild(newHole->getSprite(), 0); } _ui = CCSprite::create("score_life_time_ui.png"); _ui->setPosition(ccp(size.width / 2, size.height - 50)); _ui->retain(); this->addChild(_ui, 0); _life1 = CCSprite::create("life.png"); _life1->setPosition(ccp(size.width - 162.5, size.height - 45)); _life1->retain(); this->addChild(_life1, 0); _life2 = CCSprite::create("life.png"); _life2->setPosition(ccp(size.width - 100, size.height - 45)); _life2->retain(); this->addChild(_life2, 0); _life3 = CCSprite::create("life.png"); _life3->setPosition(ccp(size.width - 37.5, size.height - 45)); _life3->retain(); this->addChild(_life3, 0); _score = 0; _scoreLabel = CCLabelTTF::create("0", "Arial", 20); _scoreLabel->setPosition(ccp(240, size.height - 52)); _scoreLabel->retain(); this->addChild(_scoreLabel); _time = 0; _timeLabel = CCLabelTTF::create("00:00", "Arial", 20); _timeLabel->setPosition(ccp(580, size.height - 52)); _timeLabel->retain(); this->addChild(_timeLabel); _moleStayInterval = 5.0f; _parasectTimer = 0.f; _parasectAction = CCRepeat::create(CCSequence::createWithTwoActions(CCMoveBy::create(0.1, ccp(5, 5)), CCMoveBy::create(0.1, ccp(-5, -5))), 10); _parasectAction->retain(); //listen for touches this->setTouchEnabled(true); //create main loop this->schedule(schedule_selector(GameScene::update)); return true; }