cocos2d-x simpleGame 1 --管理背景音乐和旋转炮台
2014-01-04 21:46
169 查看
1 很多游戏有关闭游戏背景音乐的功能。我参考了郑州boy的博客,增加在simpleGame里面
在HelloWorldScene.h里面添加回调的声明。
在HelloWorldScene.cpp的 HelloWorld::init()添加下面的代码
里面主要创建了CCMenuItemSprite 感觉和android的imageButton类似。
创建了CCMenuItemToggle,和获取音乐状态。
在HelloWorldScene.cpp添加回调函数
2 炮台旋转
我们在HelloWorldScene.h里面添加
在HelloWorldScene.cpp里面实现finishShoot方法。
修改
关于这个if(nextProjectile !=NULL) {return;}地方真是让人疑惑啊。
参考http://dongk.iteye.com/blog/1493896
在HelloWorldScene.h里面添加回调的声明。
void vedioOnAndOffCallBack(CCObject* pSend);
在HelloWorldScene.cpp的 HelloWorld::init()添加下面的代码
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true); //CCMenuItemSprite * CCMenuItemSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite) //根据定义已经很明显了,就不解释了 CCMenuItemSprite *pitemVoff=CCMenuItemSprite::create(CCSprite::create("gmme/button_sound_off.png"),CCSprite::create("gmme/button_sound_off.png")); CC_BREAK_IF(!pitemVoff); CCMenuItemSprite *pitemVon=CCMenuItemSprite::create(CCSprite::create("gmme/button_sound_on.png"),CCSprite::create("gmme/button_sound_on.png")); CC_BREAK_IF(!pitemVon); CCMenuItemToggle * pVedioTo = NULL; // 当现在 音乐是 播放的时候界面上显示的按钮应该是 暂停音乐按钮 反之 则显示播放按钮 if(CCUserDefault::sharedUserDefault()->getBoolForKey("isplay",false)){ pVedioTo=CCMenuItemToggle::createWithTarget(this,menu_selector(HelloWorld::vedioOnAndOffCallBack),pitemVoff,pitemVon,NULL); }else { pVedioTo=CCMenuItemToggle::createWithTarget(this,menu_selector(HelloWorld::vedioOnAndOffCallBack),pitemVon,pitemVoff,NULL); } CC_BREAK_IF(!pVedioTo); //我随便设置的位置 pVedioTo->setPosition(30,50); CCMenu* pMenu1=CCMenu::create(pVedioTo,NULL); CC_BREAK_IF(!pMenu1); pMenu1->setPosition(CCPointZero); this->addChild(pMenu1,2);
里面主要创建了CCMenuItemSprite 感觉和android的imageButton类似。
创建了CCMenuItemToggle,和获取音乐状态。
在HelloWorldScene.cpp添加回调函数
void HelloWorld::vedioOnAndOffCallBack(CCObject* pSend){ if(CCUserDefault::sharedUserDefault()->getBoolForKey("isplay",false)){ CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); CCLOG("music is stop"); CCUserDefault::sharedUserDefault()->setBoolForKey("isplay",false); }else { CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); CCUserDefault::sharedUserDefault()->setBoolForKey("isplay",true); CCLOG("music is play"); } }
2 炮台旋转
我们在HelloWorldScene.h里面添加
cocos2d::CCSprite * player; cocos2d::CCSprite * nextProjectile; void finishShoot();
在HelloWorldScene.cpp里面实现finishShoot方法。
void HelloWorld::finishShoot(){ // Ok to add now - we've finished rotation! this->addChild(nextProjectile); _projectiles->addObject(nextProjectile); // Release nextProjectile->release(); nextProjectile = NULL; }
修改
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) { //if(nextProjectile !=NULL) {return;} // Choose one of the touches to work with CCTouch* touch = (CCTouch*)( touches->anyObject() ); CCPoint location = touch->getLocation(); CCLog("++++++++after x:%f, y:%f", location.x, location.y); // Set up initial location of projectile CCSize winSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); //CCSprite *projectile = CCSprite::create("Projectile.png", CCRectMake(0, 0, 20, 20)); nextProjectile = CCSprite::create("Projectile2.png"); nextProjectile->retain(); nextProjectile->setPosition( ccp(origin.x+20, origin.y+winSize.height/2) ); // Determinie offset of location to projectile float offX = location.x - nextProjectile->getPosition().x; float offY = location.y - nextProjectile->getPosition().y; // Bail out if we are shooting down or backwards if (offX <= 0) {return;} CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav"); // Ok to add now - we've double checked position //this->addChild(nextProjectile); // Determine where we wish to shoot the projectile to float realX = origin.x+winSize.width + (nextProjectile->getContentSize().width/2); float ratio = offY / offX; float realY = (realX * ratio) + nextProjectile->getPosition().y; CCPoint realDest = ccp(realX, realY); // Determine the length of how far we're shooting float offRealX = realX - nextProjectile->getPosition().x; float offRealY = realY - nextProjectile->getPosition().y; float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY)); float velocity = 480/1; // 480pixels/1sec float realMoveDuration = length/velocity; //弧度 2π弧度=360角度 float angleRadians =(float)std::atan(offRealY / offRealX); //float angleDegrees = (float)(angleRadians*(180/PI)) float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); float cocosAngle = -1 * angleDegrees; //add float rotateSpeed=0.5/M_PI;//Would take 0.5 seconds to rotate 0.5 radians, or half a circle float rotateDuration = fabs(angleRadians * rotateSpeed); //player->setRotation (cocosAngle); player->runAction( CCSequence::create(CCRotateTo::create(rotateDuration,cocosAngle), CCCallFunc::create(this,callfunc_selector(HelloWorld::finishShoot)), NULL )); // Move projectile to actual endpoint nextProjectile->runAction( CCSequence::create( CCMoveTo::create(realMoveDuration, realDest), CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) ); // Add to projectiles array nextProjectile->setTag(2); //_projectiles->addObject(projectile); }
关于这个if(nextProjectile !=NULL) {return;}地方真是让人疑惑啊。
参考http://dongk.iteye.com/blog/1493896
相关文章推荐
- cocos2d-x SimpleGame(5)如何播放背景音乐和音效
- cocos2d-x-2.2/samples/Cpp/SimpleGame/ 问题
- cocos2d-x SimpleGame制作三部曲 -- 学习笔记
- cocos2d-x SimpleGame(1)如何添加一个精灵
- How To Make A Simple Android Game with Cocos2D
- cocos2d-x SimpleGame(6)百尺竿头,更进一步
- 使用cocos2d的SimpleAudioEngine播放背景音乐和音效
- raywenderlich-Rotating Turrets: How To Make A Simple iPhone Game with Cocos2D Part 2
- raywenderlich—Harder Monsters and More Levels: How To Make A Simple iPhone Game with Cocos2D Part 3
- cocos2d-x SimpleGame(3)如何发射子弹
- cocos2d-x SimpleGame(7)关于第六章的第二种解决方案
- Cocos2d-x中SimpleAudioEngine播放多个背景音乐时被覆盖的问题
- How To Make A Simple iPhone Game with Cocos2D 3.0 Tutorial
- 使用cocos2d的SimpleAudioEngine播放背景音乐和音效
- cocos2d-x 添加背景音乐和音效-SimpleAudioEngine
- 学习cocos2d-x自带的SimpleGame
- cocos2d-x 添加背景音乐和音效-SimpleAudioEngine
- [置顶] [cocos2d-x]捕鱼达人炮台射击角度的旋转实现
- How To Make A Simple WoPhone Game With Cocos2d-x : How To Run Hello World
- Cocos2D-X SimpleGame & 初学者教程