void LevelTwo::createRemovablePlats() { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < 4; i++) { if (i == 0) { TowerBase * removablePlats = TowerBase::create(Vec2(200, 338), m_gameState, 5); m_removablePlats.push_back(removablePlats); spritebatch->addChild(removablePlats, -5); } else if (i == 1) { TowerBase * removablePlats = TowerBase::create(Vec2(245, 338), m_gameState, 5); m_removablePlats.push_back(removablePlats); spritebatch->addChild(removablePlats, -5); } else if (i == 2) { TowerBase * removablePlats = TowerBase::create(Vec2(200, -100), m_gameState, 6); m_removablePlats.push_back(removablePlats); spritebatch->addChild(removablePlats, -5); } else if (i == 3) { TowerBase * removablePlats = TowerBase::create(Vec2(245, -100), m_gameState, 6); m_removablePlats.push_back(removablePlats); spritebatch->addChild(removablePlats, -5); } } this->addChild(spritebatch, 1, END_SPRITE_BATCH); }
//INITS void GameScreen::InitAnimation() { SpriteBatchNode* spritebatch = SpriteBatchNode::create("Assets/Animation/Idle.png"); SpriteFrameCache* cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("Assets/Animation/Idle.plist"); _santaPaused = Sprite::createWithSpriteFrameName("Assets/Santa/idle_0001.png"); spritebatch->addChild(_santaPaused); addChild(spritebatch); _santaPaused->setPosition(Vec2(-_winSizeW*0.5f, _winSizeH*0.7f)); Vector<SpriteFrame*> animFrames; char str[100] = { 0 }; for (int i = 1; i <= 12; i++) { sprintf(str, "Assets/Santa/idle_%04d.png", i); SpriteFrame* frame = cache->getSpriteFrameByName(str); animFrames.pushBack(frame); } Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f); _santaPaused->runAction(RepeatForever::create(Animate::create(animation))); }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !LayerColor::initWithColor(Color4B(255,255,255,255)) ) { return false; } Texture2D *texture = TextureCache::sharedTextureCache()->addImage("Icon.png"); SpriteBatchNode* batch = SpriteBatchNode::createWithTexture(texture, 800); this->addChild(batch); for (int i = 0; i < 800; ++i) { int index = (i % 50) * 8; int rowIndex = (i / 50) * 8; Sprite *temp = Sprite::createWithTexture(texture); temp->setPosition(Vec2(index, rowIndex)); batch->addChild(temp); } return true; }
void LevelTwo::createButton() { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < ptr->m_numButton; i++) { Button * button = Button::create(Vec2(ptr->m_ButtonsX[i], ptr->m_ButtonsY[i]), 2); m_button.push_back(button); spritebatch->addChild(button, -5); } for (int i = 0; i < ptr->m_numButton; i++) { Button * button = Button::create(Vec2(ptr->m_ButtonsX[i], ptr->m_ButtonsY[i]), 3); m_button.push_back(button); spritebatch->addChild(button, -5); } this->addChild(spritebatch, 1, END_SPRITE_BATCH); }
void LevelTwo::createEndGame() { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < ptr->m_numEndGame; i++) { endGame * endGame = endGame::create(Vec2(ptr->m_endGameX[i], ptr->m_endGameY[i]), 1); m_end.push_back(endGame); spritebatch->addChild(endGame, -5); } this->addChild(spritebatch, 1, END_SPRITE_BATCH); }
void Tutorial::createTowerBases()//creates the taxis { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 24; i < ptr->m_numberOfTowerBases; i++)//loops thru the taxi for tutorial { TowerBase * base = TowerBase::create(Vec2(ptr->m_towerBaseX[i], ptr->m_towerBaseY[i]), m_gameState); m_towerBases.push_back(base); spritebatch->addChild(base, 1); } this->addChild(spritebatch, 1, TOWERS_SPRITE_BATCH); }
void LevelTwo::createHiddenPlatforms() { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < ptr->m_numberOfHiddenPlatforms; i++) { TowerBase * HiddenPlat = TowerBase::create(Vec2(380, 470), m_gameState, 4); m_hiddenPlats.push_back(HiddenPlat); spritebatch->addChild(HiddenPlat, -5); } this->addChild(spritebatch, 1, HIDDEN_SPRITE_BATCH); }
void Tutorial::createAmbulances()//creates the ambulances { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 22; i < ptr->m_numberOfAmbulance; i++)//loops thru the ambulances for tutorial { Ambulance * base = Ambulance::create(Vec2(ptr->m_ambulancePosX[i], ptr->m_ambulancePosY[i]), m_gameState); m_ambulances.push_back(base); spritebatch->addChild(base, 1); } this->addChild(spritebatch, 1, COINS_SPRITE_BATCH); }
void Tutorial::createTrucks()//creates the trucks { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < 10; i++)//loops thru the trucks for tutorial { Truck * base = Truck::create(Vec2(ptr->m_truckPosX[i], ptr->m_truckPosY[i]), m_gameState); m_trucks.push_back(base); spritebatch->addChild(base, 1); } this->addChild(spritebatch, 1, COINS_SPRITE_BATCH); }
void Tutorial::createCoins()//creates the coins { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 92; i < 104; i++)//loops thru the coins for tutorial { Coin * base = Coin::create(Vec2(ptr->m_coinPosX[i], ptr->m_coinPosY[i]), m_gameState); m_coins.push_back(base); spritebatch->addChild(base, 1); } this->addChild(spritebatch, 4, COINS_SPRITE_BATCH); }
void LevelTwo::createPlatforms() { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < ptr->m_numberOfLevelTwoPlats; i++) { TowerBase * base = TowerBase::create(Vec2(ptr->m_levelTwoPlatformsX[i], ptr->m_levelTwoPlatformsY[i]), m_gameState, 3); m_levelTwoPlat.push_back(base); spritebatch->addChild(base, -5); } this->addChild(spritebatch, 1, LEVELTWO_SPRITE_BATCH); }
void LevelTwo::createTraps() { std::shared_ptr<GameData> ptr = GameData::sharedGameData(); SpriteBatchNode* spritebatch = SpriteBatchNode::create(ptr->m_textureAtlasImageFile); for (int i = 0; i < 1; i++) { TowerGun * traps = TowerGun::create(Vec2(ptr->m_trapX[i], ptr->m_trapY[i]), 2, 520, 240); m_traps.push_back(traps); spritebatch->addChild(traps, -5); } for (int i = 0; i < 1; i++) { TowerGun * traps = TowerGun::create(Vec2(ptr->m_trapX[i], ptr->m_trapY[i]), 2, 970, 240); m_traps.push_back(traps); spritebatch->addChild(traps, -5); } for (int i = 0; i < 1; i++) { TowerGun * traps = TowerGun::create(Vec2(ptr->m_trapX[i], ptr->m_trapY[i]), 2, 590, 115); m_traps.push_back(traps); spritebatch->addChild(traps, -5); } for (int i = 0; i < 1; i++) { TowerGun * traps = TowerGun::create(Vec2(ptr->m_trapX[i], ptr->m_trapY[i]), 2, 790, 115); m_traps.push_back(traps); spritebatch->addChild(traps, -5); } for (int i = 0; i < 1; i++) { TowerGun * traps = TowerGun::create(Vec2(ptr->m_trapX[i], ptr->m_trapY[i]), 2, 990, 115); m_traps.push_back(traps); spritebatch->addChild(traps, -5); } this->addChild(spritebatch, -1, TRAPS_SPRITE_BATCH); }
void HelloWorld::addBird(cocos2d::Point p) { SpriteBatchNode *parent = SpriteBatchNode::create("bird_hero.png"); m_pSpriteTexture = parent->getTexture();//获取纹理贴图 //将精灵批处理节点添加到层 this->addChild(parent,0,kTagParentNode); // Sprite *sprite = Sprite::createWithTexture(m_pSpriteTexture,Rect(32*idx, 32*idy, 32, 32)); bird = Sprite::createWithTexture(m_pSpriteTexture); parent->addChild(bird); Size birdSize = m_pSpriteTexture->getContentSize(); b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); bodyDef.userData = bird; b2Body *body = world->CreateBody(&bodyDef); //定义盒子形状 b2PolygonShape dynamicBox; dynamicBox.SetAsBox(birdSize.width/2/PTM_RATIO, birdSize.height/2/PTM_RATIO); b2FixtureDef fixtureDef; // fixtureDef.filter.groupIndex = 0; // fixtureDef.filter.maskBits = 0x0002;//掩码 // fixtureDef.filter.categoryBits = 0x0003;//设置分类码 fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.f;//密度 fixtureDef.friction = 0.3f; fixtureDef.restitution = 0.f;//弹性系数 body->CreateFixture(&fixtureDef);//用刚体创建夹具 //事件监听 auto listener1 = EventListenerTouchOneByOne::create(); listener1->setSwallowTouches(true); listener1->onTouchBegan =[=](Touch *touch,Event *event) { //设置线性速度 body->SetLinearVelocity(b2Vec2(0, 7)); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this); }
void gameLevel2::shipExplosions(Vec2 vec,bool scale) { SpriteBatchNode* spritebatch = SpriteBatchNode::create("animations/explosion.png"); SpriteFrameCache* cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("animations/explosion.plist"); auto explosion = Sprite::createWithSpriteFrameName("explosion_01.png"); explosion->setPosition(vec); if (scale) explosion->setScale(0.5); spritebatch->addChild(explosion); this->addChild(spritebatch); Vector<SpriteFrame*> animFrames(48); char str[100] = { 0 }; for (int i = 1; i < 49; i++) { sprintf(str, "explosion_%02d.png", i); SpriteFrame* frame = cache->getSpriteFrameByName(str); animFrames.pushBack(frame); } Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.02); explosion->runAction(Sequence::create(Animate::create(animation),RemoveSelf::create(),NULL)); }
bool TitleScreen::init() { ////////////////////////////// // 1. super init first if ( !BaseLayer::init() ) { return false; } SimpleAudioEngine::sharedEngine()->preloadEffect("sfx_cursor_back.wav"); SimpleAudioEngine::sharedEngine()->preloadEffect("sfx_cursor_move.wav"); SimpleAudioEngine::sharedEngine()->preloadEffect("sfx_cursor_select.wav"); SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("logos.plist"); SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("chars.plist"); SpriteBatchNode *mainBatchNode = SpriteBatchNode::create("chars.png"); Sprite *bg = Sprite::create("bg_menu.png"); bg->getTexture()->setAliasTexParameters(); bg->setAnchorPoint(ccp(0, 0)); bg->setPosition(ccp(0, 0)); Sprite *gameLogo = Sprite::createWithSpriteFrameName("game_logo.png"); gameLogo->getTexture()->setAliasTexParameters(); gameLogo->setAnchorPoint(ccp(0.5f, 0.5f)); gameLogo->setPosition(ccp(80, 103)); pressStartLabel = LabelBMFont::create("PRESS START", "whitefont.fnt", 160, kTextAlignmentCenter); pressStartLabel->setColor(gbLightestColor3); pressStartLabel->getTexture()->setAliasTexParameters(); pressStartLabel->setAnchorPoint(ccp(0.5f, 0.5f)); pressStartLabel->setPosition(ccp(80, 30)); startGameLabel = LabelBMFont::create("Start game", "whitefont.fnt", 160, kTextAlignmentCenter); startGameLabel->setColor(gbLightColor3); startGameLabel->getTexture()->setAliasTexParameters(); startGameLabel->setAnchorPoint(ccp(0.5f, 0.5f)); startGameLabel->setPosition(ccp(80, 30)); creditsLabel = LabelBMFont::create("Credits", "whitefont.fnt", 160, kTextAlignmentCenter); creditsLabel->setColor(gbLightColor3); creditsLabel->setAnchorPoint(ccp(0.5f, 0.5f)); creditsLabel->setPosition(ccp(80, 30)); levelLabel = LabelBMFont::create("Level", "whitefont.fnt", 78, kTextAlignmentRight); levelLabel->setColor(gbLightColor3); levelLabel->setAnchorPoint(ccp(1.0f, 0.5f)); levelLabel->setPosition(ccp(78, 30)); levelNumberLabel = LabelBMFont::create("1", "whitefont.fnt", 78, kTextAlignmentLeft); levelNumberLabel->setColor(gbLightColor3); levelNumberLabel->setAnchorPoint(ccp(0.0f, 0.5f)); levelNumberLabel->setPosition(ccp(82, 30)); arrowUp = Sprite::createWithSpriteFrameName("arrowup.png"); arrowUp->setAnchorPoint(ccp(0.5f, 0.0f)); arrowUp->getTexture()->setAliasTexParameters(); arrowDown = Sprite::createWithSpriteFrameName("arrowdown.png"); arrowDown->setAnchorPoint(ccp(0.5f, 1.0f)); mainBatchNode->addChild(arrowUp); mainBatchNode->addChild(arrowDown); this->addChild(bg); this->addChild(gameLogo); this->addChild(pressStartLabel); this->addChild(startGameLabel); this->addChild(creditsLabel); this->addChild(levelLabel); this->addChild(levelNumberLabel); this->addChild(mainBatchNode); optionIndex = 0; cursorIndex = 0; maxLevel = UserDefault::sharedUserDefault()->getIntegerForKey("MaxLevel", 1); if (maxLevel > kLastLevel) maxLevel = kLastLevel; selectedLevel = maxLevel; this->updateMenu(); if (!SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying()) SimpleAudioEngine::sharedEngine()->playBackgroundMusic("song_title.wav", true); return true; }
// on "init" you need to initialize your instance bool Gameplay::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); // --- box2d definitions b2Vec2 gravity = b2Vec2(0.0f, -1.0f); bool doSleep = false; mWorld = new b2World(gravity); // --- // --- create the first body //b2BodyDef groundBodyDef; //groundBodyDef.position.Set(0.0f, -10.0f); // //b2Body* groundBody = mWorld->CreateBody(&groundBodyDef); //b2PolygonShape groundBox; //groundBox.SetAsBox(50.0f, 10.0f); // --- // load sprite sheet to be used on this scene SpriteBatchNode* spriteSheet = SpriteBatchNode::create("TexturePacker/spritesheet.png"); SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("TexturePacker/spritesheet.plist"); this->addChild(spriteSheet); // --- //instancia manual ou toda classe filha (MainCharacter, Platform, etc) tem que ter seus overrides próprios pra instanciar seu próprio tipo mMainCharacter = new MainCharacter(); mMainCharacter->initWithInitialState("main_character.png", mWorld, new TestStateB(), b2_dynamicBody); //mMainCharacter->SetYVelocity(1); mMainCharacter->setCollisionObjectPosition(Point(00,300)); spriteSheet->addChild(mMainCharacter, 0); mPlatform = new Platform(); mPlatform->initWithInitialState("platform.png", mWorld, new TestStateB(), b2_staticBody); mPlatform->setCollisionObjectPosition(Point(100,100)); spriteSheet->addChild(mPlatform, 0); mPlatform2 = new Platform(); mPlatform2->initWithInitialState("platform.png", mWorld, new TestStateB(), b2_staticBody); mPlatform2->setCollisionObjectPosition(Point(238,100)); spriteSheet->addChild(mPlatform2, 0); //moveAction = CCMoveTo::create( 1, Point( mMainCharacter->getPositionX(), -120)); //mMainCharacter->runAction(moveAction); this->schedule( schedule_selector(Gameplay::update)); float teste0 = this->getPositionX(); float teste1 = mMainCharacter->getPositionX(); float teste2 = visibleSize.width/2; float teste4 = teste1 - teste2; this->setTouchEnabled(true); this->setPositionX(mMainCharacter->getPositionX() + visibleSize.width/2 /*+ mMainCharacter->getContentSize().width*/); return true; }
//实现PianoLayer类中的init方法,初始化布景 bool PianoLayer::init() { //调用父类的初始化 if ( !Layer::init() ) { return false; } degree = 0; musicFlag = false; pauseFlag = false; instrumentName = "piano"; instrumentNumber = 1; changeFlag = true; musicNum = 0; //获取可见区域尺寸 Size visibleSize = Director::getInstance()->getVisibleSize(); //获取可见区域原点坐标 Point origin = Director::getInstance()->getVisibleOrigin(); Sprite* volume = Sprite::create(pic_RESOURE_PATH + "volume_cn.png"); //音量 volumeSize = volume->getContentSize(); volume->setScale(1.2); volume->setPosition(Point(volumeSize.width/2 + 20, visibleSize.height - volumeSize.height/2)); this->addChild(volume, 2); exit = Sprite::create(pic_RESOURE_PATH + "exit.png"); //退出 Size exitSize = exit->getContentSize(); exit->setScale(1.2); exit->setPosition(Point(visibleSize.width - exitSize.width/2, visibleSize.height - exitSize.height/2)); this->addChild(exit, 2); slider = Slider::create(); slider->loadBarTexture(pic_RESOURE_PATH + "soundBackGround.png"); slider->loadSlidBallTextures(pic_RESOURE_PATH + "transparent.png",pic_RESOURE_PATH + "transparent.png", ""); slider->loadProgressBarTexture(pic_RESOURE_PATH + "sound.png"); slider->setAnchorPoint(Point(0, 0.5)); sliderSize = slider->getContentSize(); slider->setPercent(CocosDenshion::SimpleAudioEngine::getInstance()->getEffectsVolume()*100); slider->setPosition(Point(volumeSize.width*1.2 + 20, visibleSize.height - volumeSize.height/2)); slider->addEventListener(CC_CALLBACK_2(PianoLayer::sliderEvent, this)); this->addChild(slider, 5); instrument = Sprite::create(pic_RESOURE_PATH + "piano_cn.png"); //选择 Size selectSize = instrument->getContentSize(); instrument->setPosition( Point( visibleSize.width - exitSize.width - selectSize.width/2 -20, visibleSize.height - selectSize.height/2 )); this->addChild(instrument, 2); selection = Sprite::create(pic_RESOURE_PATH + "back.png"); //背景 selection->setPosition( Point( visibleSize.width - exitSize.width - selectSize.width/2 -20, visibleSize.height - selectSize.height/2 )); this->addChild(selection, 1); Sprite* left = Sprite::create(pic_RESOURE_PATH + "left.png"); //左键 leftSize = left->getContentSize(); left->setScale(SCALE); left->setPosition( Point( leftSize.width/2*SCALE + 10, visibleSize.height - volumeSize.height - leftSize.height*SCALE/2 - 5 )); this->addChild(left, 2); Sprite* right = Sprite::create(pic_RESOURE_PATH + "right.png"); //右键 rightSize = right->getContentSize(); right->setScale(SCALE); right->setPosition( Point( visibleSize.width - rightSize.width*SCALE/2 - 10, visibleSize.height - volumeSize.height - rightSize.height*SCALE/2 - 5 )); this->addChild(right, 2); //第二个参数为最大储存数 SpriteBatchNode* batchNode = SpriteBatchNode::create(pic_RESOURE_PATH + "white_small.jpg", 50); //小键盘 batchNode->setAnchorPoint(Point(0, 0)); batchNode->setPosition( Point( leftSize.width*SCALE + LEFT_INTERVAL, visibleSize.height - volumeSize.height - ABOVE_INTERVAL )); this->addChild(batchNode); for(int i = 0; i<PIANO_KEY; i++) //小键盘 { Sprite* whiteSmall = Sprite::createWithTexture(batchNode->getTexture(), Rect(0, 0, 34, 57)); whiteSmall->setScale(SCALE); whiteSmall->setAnchorPoint(Point(0, 0)); smallSize = whiteSmall->getContentSize(); whiteSmall->setPosition(Point(i*smallSize.width*SCALE,0)); batchNode->addChild(whiteSmall, 2); } selectBack = Sprite::create(pic_RESOURE_PATH + "selectBack.png"); backSize = selectBack->getContentSize(); selectBack->setPosition( Point( leftSize.width*SCALE + LEFT_INTERVAL + smallSize.width*SCALE*7 + backSize.width/2, visibleSize.height - volumeSize.height - 37 )); this->addChild(selectBack, 4); float width = PIANO_KEY*smallSize.width*SCALE - backSize.width; //总长 percent = (selectBack->getPosition().x - backSize.width/2 - leftSize.width*SCALE - LEFT_INTERVAL) / width; float positionX = -percent*WHITE_INTERVAL*13; // for(int i=0; i<PIANO_KEY; i++) { Sprite* white = Sprite::create(pic_RESOURE_PATH + "white.png"); white->setScale(1.12); white->setAnchorPoint(Point(0, 0)); white->setPosition(Point(positionX + i*WHITE_INTERVAL, 0)); string tempNumber = StringUtils::format("%d", i); whiteSize = white->getContentSize(); piano.insert(pair<string,Sprite*>(tempNumber, white)); } for(int i = PIANO_KEY,j=0;i<38; i++) { Sprite* black = Sprite::create(pic_RESOURE_PATH + "black.png"); black->setScale(1.12); black->setAnchorPoint(Point(0.5, 0)); blackSize = black->getContentSize(); black->setPosition(Point(positionX + (i-21+j)*WHITE_INTERVAL, (whiteSize.height - blackSize.height)*1.12)); string tempNumber = StringUtils::format("%d", i); if((i-21)%5==0||(i-21)==2||(i-21)==7||(i-21)==12) { j++; } piano_black.insert(pair<string,Sprite*>(tempNumber, black)); } map<string, Sprite*>::iterator iter; for(iter=piano.begin(); iter != piano.end();iter++) { this->addChild(iter->second, 5); } for(iter=piano_black.begin(); iter != piano_black.end(); iter++) { this->addChild(iter->second, 6); } for(int i = PIANO_KEY,j = 0; i<38; i++) { Sprite* blackSmall; float positionX; if(i==37) { blackSmall = Sprite::create(pic_RESOURE_PATH + "black_small_half.jpg"); Size size = blackSmall->getContentSize(); positionX = leftSize.width*SCALE + LEFT_INTERVAL + (i-21+j)*smallSize.width*SCALE-size.width/2; }else { blackSmall = Sprite::create(pic_RESOURE_PATH + "black_small.jpg"); positionX = leftSize.width*SCALE + LEFT_INTERVAL + (i-21+j)*smallSize.width*SCALE; } blackSmall->setScale(SCALE); blackSmall->setAnchorPoint(Point(0.5, 0)); blackSmall->setPosition(Point( positionX, visibleSize.height - volumeSize.height - ABOVE_INTERVAL + smallSize.height*SCALE/2)); if((i-21)%5==0||(i-21)==2||(i-21)==7||(i-21)==12) { j++; } this->addChild(blackSmall, 2); } //设置定时回调指定方法干活 auto director = Director::getInstance(); schedRound = director->getScheduler(); //创建退出单点触摸监听 EventListenerTouchOneByOne* listenerExit = EventListenerTouchOneByOne::create(); //开始触摸时回调onTouchBegan方法 listenerExit->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchExitBegan, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerExit, exit); //创建乐器单点触摸监听 EventListenerTouchOneByOne* listenerSelect = EventListenerTouchOneByOne::create(); listenerSelect->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerSelect->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchBegan, this); listenerSelect->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerSelect, instrument); //创建左箭头单点触摸监听 EventListenerTouchOneByOne* listenerLeft = EventListenerTouchOneByOne::create(); listenerLeft->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerLeft->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchLeftBegan, this); listenerLeft->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchLeftEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerLeft, left); //创建右箭头单点触摸监听 EventListenerTouchOneByOne* listenerRight= EventListenerTouchOneByOne::create(); listenerRight->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerRight->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchRightBegan, this); listenerRight->onTouchEnded = CC_CALLBACK_2(PianoLayer::onTouchRightEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerRight, right); //创建单点触摸监听 EventListenerTouchOneByOne* listenerBack = EventListenerTouchOneByOne::create(); //设置下传触摸 listenerBack->setSwallowTouches(true); //开始触摸时回调onTouchBegan方法 listenerBack->onTouchBegan = CC_CALLBACK_2(PianoLayer::onTouchSelectBegan, this); listenerBack->onTouchMoved = CC_CALLBACK_2(PianoLayer::onTouchSelectMoved, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerBack, selectBack); //---------------------------------单点触控,用于选择钢琴键----------------------------- //创建单点触摸监听 EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create(); //开始触摸时回调onTouchBegan方法 listener->onTouchBegan = CC_CALLBACK_2(PianoLayer::onMyTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //---------------------------------多点触控,用于点击钢琴键------------------------------ //创建一个多点触摸监听 auto listenerTouch = EventListenerTouchAllAtOnce::create(); //开始触摸时回调onTouchBegan方法 listenerTouch->onTouchesBegan = CC_CALLBACK_2(PianoLayer::onMyTouchesBegan, this); //触摸移动时回调onTouchMoved方法 listenerTouch->onTouchesMoved = CC_CALLBACK_2(PianoLayer::onMyTouchesMoved, this); //触摸结束时回调onTouchEnded方法 listenerTouch->onTouchesEnded = CC_CALLBACK_2(PianoLayer::onMyTouchesEnded, this); //添加到监听器 _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch, this); return true; }
bool GameScene::init() { if (!Layer::init()) { return false; } gameScene = this;//用于获取游戏场景对象 winSize = Director::getInstance()->getWinSize(); floorCount = 16;//设置地面和金币的个数 forceX = 300; forceY = 30; xSpeed = 5; ySpeed = 15;//决定跳的高度 constXspeed = xSpeed; constYspeed = ySpeed; scaleFix = winSize.width/480; speedFix = 0.01; accelerate = 0.94;//加速度 sizep = Director::getInstance()->getWinSizeInPixels(); scaleFix = sizep.width/480; isJump = false; isPlaySound=GameFatherLayer::allowSound();//是否播放音效 isCollistionWithFloor =false; //创建背景地图 BackMap *sky = BackMap::createMap("back_1.png"); this->addChild(sky,0,0); BackMap *backMap = BackMap::createMap("back_5.png"); this->addChild(backMap,0,1); //创建主角精灵 player = Player::createPlayer(); SpriteBatchNode *batchNode = SpriteBatchNode::create("run.png"); batchNode->addChild(player, 2, 10); playerWidth = player->getContentSize().width; playerHeight = player->getContentSize().height; this->addChild(batchNode); player->setPosition(Point(-20, winSize.height/2 -100)); MoveBy *move = MoveBy::create(1, Point(80, 0)); player->runAction(move); Floor *temp = Floor::createFloor(); floorWidth = temp->getContentSize().width; floorHeight = temp->getContentSize().height; SimpleAudioEngine::getInstance()->preloadBackgroundMusic("background.caf"); SimpleAudioEngine::getInstance()->preloadEffect("eatcoin.caf"); if (isPlaySound) { SimpleAudioEngine::getInstance()->playBackgroundMusic("background.caf"); } //进度条 coinNeededNum = 30; coinUsed = 0; isBarFull = false; progressInterval = 100.0/(float)coinNeededNum; Sprite *barSprite = Sprite::create("powerbar.png"); progress = ProgressTimer::create(barSprite); progress->setPercentage(0.0f); progress->setMidpoint(Point(0, 0)); progress->setBarChangeRate(Point(1, 0)); progress->setScaleY(1.2); progress->setType(kCCProgressTimerTypeBar); progress->setAnchorPoint(Point(0, 0.5)); progress->setPosition(Point(20, winSize.height-18)); Sprite *proFrame = Sprite::create("powerui.png"); proFrame->setAnchorPoint(Point(0, 0.3)); progress->addChild(proFrame); this->addChild(progress); //添加返回主页按钮 MenuItemImage *menuItem = MenuItemImage::create("btnmenu.png", "btnmenu.png",CC_CALLBACK_1(GameScene::menuButton, this)); menuItem->setAnchorPoint(Point(1, 1)); Menu *menu = Menu::create(menuItem, NULL); menu->setPosition(Point(winSize.width-20, winSize.height - 20)); this->addChild(menu); this->initFloorsAndCoins(); this->scheduleUpdate(); return true; }
bool StartScreen::init() { if (!Layer::init()) { return false; } auto rootNode = CSLoader::createNode("Screens/StartScreen.csb"); addChild(rootNode); this->scheduleUpdate(); auto winSize = Director::getInstance()->getVisibleSize(); //Animated Santa Sprite SpriteBatchNode* spritebatch = SpriteBatchNode::create("Assets/Animation/Idle.png"); SpriteFrameCache* cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("Assets/Animation/Idle.plist"); _santa = Sprite::createWithSpriteFrameName("Assets/Santa/idle_0001.png"); spritebatch->addChild(_santa); addChild(spritebatch); _santa->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.7f)); _background = (Sprite*)rootNode->getChildByName("Background"); _background->setPosition(Vec2(winSize.width * 0.5, winSize.height * 0.5)); Vector<SpriteFrame*> animFrames; char str[100] = { 0 }; for (int i = 1; i <= 12; i++) { sprintf(str, "Assets/Santa/idle_%04d.png", i); SpriteFrame* frame = cache->getSpriteFrameByName(str); animFrames.pushBack(frame); } Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f); _santa->runAction(RepeatForever::create(Animate::create(animation))); //TOUCHES //Set up a touch listener. auto touchListener = EventListenerTouchOneByOne::create(); //Set callbacks for our touch functions. touchListener->onTouchBegan = CC_CALLBACK_2(StartScreen::onTouchBegan, this); touchListener->onTouchEnded = CC_CALLBACK_2(StartScreen::onTouchEnded, this); touchListener->onTouchMoved = CC_CALLBACK_2(StartScreen::onTouchMoved, this); touchListener->onTouchCancelled = CC_CALLBACK_2(StartScreen::onTouchCancelled, this); //Add our touch listener to event listener list. _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); //BUTTONS //Start button _btnStart = static_cast<ui::Button*>(rootNode->getChildByName("btnStart")); _btnStart->addTouchEventListener(CC_CALLBACK_2(StartScreen::StartButtonPressed, this)); _btnStart->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.43f)); //Highscore button. _btnHighscore = static_cast<ui::Button*>(rootNode->getChildByName("btnHighscore")); _btnHighscore->addTouchEventListener(CC_CALLBACK_2(StartScreen::HighscoreButtonPressed, this)); _btnHighscore->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.32f)); //Options button _btnOptions = static_cast<ui::Button*>(rootNode->getChildByName("btnOptions")); _btnOptions->addTouchEventListener(CC_CALLBACK_2(StartScreen::OptionsButtonPressed, this)); _btnOptions->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.21f)); //Exit button _btnExit = static_cast<ui::Button*>(rootNode->getChildByName("btnExit")); _btnExit->addTouchEventListener(CC_CALLBACK_2(StartScreen::ExitButtonPressed, this)); _btnExit->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.1f)); return true; }