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


    1 很多游戏有关闭游戏背景音乐的功能。我参考了郑州boy的博客,增加在simpleGame里面

    在HelloWorldScene.h里面添加回调的声明。

    1 void vedioOnAndOffCallBack(CCObject* pSend);

    在HelloWorldScene.cpp的 HelloWorld::init()添加下面的代码

     1 CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true);
     2         //CCMenuItemSprite * CCMenuItemSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
     3         //根据定义已经很明显了,就不解释了
     4         CCMenuItemSprite *pitemVoff=CCMenuItemSprite::create(CCSprite::create("gmme/button_sound_off.png"),CCSprite::create("gmme/button_sound_off.png"));
     5         CC_BREAK_IF(!pitemVoff);
     6 
     7         CCMenuItemSprite *pitemVon=CCMenuItemSprite::create(CCSprite::create("gmme/button_sound_on.png"),CCSprite::create("gmme/button_sound_on.png"));
     8         CC_BREAK_IF(!pitemVon);
     9 
    10         CCMenuItemToggle * pVedioTo = NULL;
    11         // 当现在 音乐是 播放的时候界面上显示的按钮应该是 暂停音乐按钮 反之 则显示播放按钮
    12         if(CCUserDefault::sharedUserDefault()->getBoolForKey("isplay",false)){
    13             pVedioTo=CCMenuItemToggle::createWithTarget(this,menu_selector(HelloWorld::vedioOnAndOffCallBack),pitemVoff,pitemVon,NULL);
    14         }else {
    15             pVedioTo=CCMenuItemToggle::createWithTarget(this,menu_selector(HelloWorld::vedioOnAndOffCallBack),pitemVon,pitemVoff,NULL);
    16         }
    17         
    18         
    19         CC_BREAK_IF(!pVedioTo);
    20         //我随便设置的位置
    21         pVedioTo->setPosition(30,50);
    22 
    23         CCMenu* pMenu1=CCMenu::create(pVedioTo,NULL);
    24         CC_BREAK_IF(!pMenu1);
    25         pMenu1->setPosition(CCPointZero);
    26         this->addChild(pMenu1,2);

    里面主要创建了CCMenuItemSprite 感觉和android的imageButton类似。
    创建了CCMenuItemToggle,和获取音乐状态。

    在HelloWorldScene.cpp添加回调函数

     1 void HelloWorld::vedioOnAndOffCallBack(CCObject* pSend){
     2     if(CCUserDefault::sharedUserDefault()->getBoolForKey("isplay",false)){
     3         CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
     4         CCLOG("music is stop");
     5         CCUserDefault::sharedUserDefault()->setBoolForKey("isplay",false);
     6     }else {
     7         CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();        
     8         CCUserDefault::sharedUserDefault()->setBoolForKey("isplay",true);
     9         CCLOG("music is play");
    10     }
    11 
    12 }

    2 炮台旋转

    我们在HelloWorldScene.h里面添加

    1 cocos2d::CCSprite * player;
    2     cocos2d::CCSprite * nextProjectile;
    3     void finishShoot();

    在HelloWorldScene.cpp里面实现finishShoot方法。

    1 void HelloWorld::finishShoot(){
    2     // Ok to add now - we've finished rotation!  
    3 this->addChild(nextProjectile);  
    4 _projectiles->addObject(nextProjectile);  
    5   
    6 // Release  
    7 nextProjectile->release();  
    8 nextProjectile = NULL; 
    9 }

    修改

     1 void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
     2 
     3 {   
     4     //if(nextProjectile !=NULL) {return;}
     5     // Choose one of the touches to work with
     6     CCTouch* touch = (CCTouch*)( touches->anyObject() );
     7     CCPoint location = touch->getLocation();
     8      
     9     CCLog("++++++++after  x:%f, y:%f", location.x, location.y);
    10 
    11     // Set up initial location of projectile
    12     CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
    13     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    14     //CCSprite *projectile = CCSprite::create("Projectile.png", CCRectMake(0, 0, 20, 20));
    15     nextProjectile = CCSprite::create("Projectile2.png");
    16     nextProjectile->retain();
    17     nextProjectile->setPosition( ccp(origin.x+20, origin.y+winSize.height/2) );
    18 
    19     // Determinie offset of location to projectile
    20     float offX = location.x - nextProjectile->getPosition().x;
    21     float offY = location.y - nextProjectile->getPosition().y;
    22 
    23     // Bail out if we are shooting down or backwards
    24     if (offX <= 0) {return;}
    25     CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
    26     // Ok to add now - we've double checked position
    27     //this->addChild(nextProjectile);
    28 
    29     // Determine where we wish to shoot the projectile to
    30     float realX = origin.x+winSize.width + (nextProjectile->getContentSize().width/2);
    31     float ratio = offY / offX;
    32     float realY = (realX * ratio) + nextProjectile->getPosition().y;
    33     CCPoint realDest = ccp(realX, realY);
    34 
    35     // Determine the length of how far we're shooting
    36     float offRealX = realX - nextProjectile->getPosition().x;
    37     float offRealY = realY - nextProjectile->getPosition().y;
    38     float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY));
    39     float velocity = 480/1; // 480pixels/1sec
    40     float realMoveDuration = length/velocity;
    41     //弧度     2π弧度=360角度
    42     float angleRadians =(float)std::atan(offRealY / offRealX);
    43     //float  angleDegrees = (float)(angleRadians*(180/PI))
    44     float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
    45     float cocosAngle = -1 * angleDegrees;
    46     //add
    47     float rotateSpeed=0.5/M_PI;//Would take 0.5 seconds to rotate 0.5 radians, or half a circle  
    48     float rotateDuration = fabs(angleRadians * rotateSpeed);   
    49 
    50 
    51 
    52     //player->setRotation (cocosAngle);
    53      
    54     player->runAction(
    55         CCSequence::create(CCRotateTo::create(rotateDuration,cocosAngle),
    56          CCCallFunc::create(this,callfunc_selector(HelloWorld::finishShoot)),  
    57                                        NULL  
    58                                        )); 
    59 
    60     // Move projectile to actual endpoint
    61     nextProjectile->runAction( CCSequence::create(
    62         CCMoveTo::create(realMoveDuration, realDest),
    63         CCCallFuncN::create(this, 
    64                             callfuncN_selector(HelloWorld::spriteMoveFinished)), 
    65         NULL) );
    66 
    67     // Add to projectiles array
    68     nextProjectile->setTag(2);
    69     //_projectiles->addObject(projectile);
    70 
    71     
    72 }

    关于这个if(nextProjectile !=NULL) {return;}地方真是让人疑惑啊。

    参考http://dongk.iteye.com/blog/1493896

  • 相关阅读:
    flush()方法
    多对一关联映射(manytoone)
    Hibernate配置数据库解决插入乱码问题
    lazy
    一对多关联映射(单向)
    属性类的映射
    多对多关联(双向)
    多对多关联映射(单向)
    多态查询
    Visual C# 2008+SQL Server 2005 数据库与网络开发11.2.2 LINQ的基本查询操作
  • 原文地址:https://www.cnblogs.com/aosting/p/3505347.html
Copyright © 2020-2023  润新知