您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x simpleGame 1 --管理背景音乐和旋转炮台

2014-01-04 21:46 169 查看
1 很多游戏有关闭游戏背景音乐的功能。我参考了郑州boy的博客,增加在simpleGame里面

在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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: