纯粹娱乐:了解cocos2d-x

前端之家收集整理的这篇文章主要介绍了纯粹娱乐:了解cocos2d-x前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • 设置资源查找路径

默认是Resources下面查找

std::vector<std::string> searchPath;
searchPath.push_back("sd");
//searchPath.push_back("/");
FileUtils::getInstance()->setSearchPaths(searchPath);

如果这样设置那么查找的目录是变成: Resources/sd/

  • 音乐声音

预加载

SimpleAudioEngine::getInstance()->preloadBackgroundMusic("**.mp3"); 
SimpleAudioEngine::getInstance()->preloadEffect("**.wav");

播放声音:

SimpleAudioEngine::getInstance()->playBackgroundMusic("**.wav");  
SimpleAudioEngine::getInstance()->playBackgroundMusic("**.wav",true);  
SimpleAudioEngine::getInstance()->playEffect("**.mp3",false);

创建菜单按钮

auto backItem = MenuItemImage::create(
                                           "**.png",//默认图片
                                           "**.png",//选中图片
                                           CC_CALLBACK_1(**::menuBackCallback//按钮点击事件触发回调,this));

开关菜单按钮

MenuItemImage *_trnOn,*_trnOff;
    _trnOn = MenuItemImage@H_403_114@::create(  
        "btn_**On.png","btn_**On.png");  
    _trnOff = MenuItemImage@H_403_114@::create(  
        "btn_**Off.png","btn_**Off.png");
MenuItemToggle *toggleItem = MenuItemToggle@H_403_114@::createWithCallback(
                            CC_CALLBACK_1(**@H_403_114@::toggleMenuCallback,this),_turnOn,_turnOff,NULL);
toggleItem->setSelectedIndex(1);//设置初始值 

创建菜单

auto menu = Menu::create(backItem,toggleItem,**Item,NULL);

加进场景(之前设置显示位置等)//比如backItem->setPosition(Vec2(* *,* *));

this -> addChild( menu );//*->removeFromParent();移除
  • 本地存储

获取值:

UserDefault::getInstance()->getBoolForKey("**")

存储值:

UserDefault::getInstance()->setBoolForKey("**",false);
  • 场景转跳
auto scene = MainScene@H_403_114@::createScene();
TransitionScene* reScene = TransitionPageTurn@H_403_114@::create(1.0f,scene,true);
Director@H_403_114@::getInstance()->replaceScene(reScene);
auto sprite = Sprite@H_403_114@::create("scene_end.png");
sprite->setPosition(Point(GAME_SCREEN_WIDTH/2,GAME_SCREEN_HEIGHT/2));
this->addChild(sprite);
TTFConfig config("yuehei.otf",48);
auto labelscore = Label@H_403_114@::createWithTTF(config," 0 ");
labelscore -> setPosition(Point( GAME_SCREEN_WIDTH/2,GAME_SCREEN_HEIGHT/1.4));
this->addChild(labelscore);
  • 交互

添加监听

auto touchListener = EventListenerTouchOneByOne@H_403_114@::create();
touchListener->onTouchBegan = CC_CALLBACK_2(GameScene@H_403_114@::onTouchBegan,this);
touchListener->onTouchMoved = CC_CALLBACK_2(GameScene@H_403_114@::onTouchMoved,this);
_eventDispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this);

获取点击点

auto location = touch->getLocation();

判断点击点是否在某区域
(可以根据点击点 获取sprite 也可以根据结束点几点判断左滑右滑 )

auto  upRect = Rect(*,*,*,*);
if (upRect.containsPoint(location)) { }
  • 简单动画
staSprite->runAction(MoveTo::create(time,posOfDest));//移动位置

staSprite->runAction(Sequence::create(
                                      MoveTo::create(time,posOfDest),MoveTo::create(time,posOfSrc),NULL));//先后连续移动两次位置
  • 节点位置 position 节点位置 anchorpoint 锚点,默认节点中心,position的位置的参考点,scale缩放的参考点,rotation旋转的参考点

猜你在找的Cocos2d-x相关文章