通过Plist得到每一个小图

来源:互联网 发布:穿裙子骑自行车 知乎 编辑:程序博客网 时间:2024/05/21 09:36


各位大哥大姐,小兄弟,小mm,你们在学习过程中是否遇到了和小弟一样的问题,没资源啊。

好像有个美工啊可怜,自己找的都是大图啊,只是想要一个图啊。

so小弟决定把通过plist或其他东东(自己解析)的spriteCache导出来。


又是直接上干货,代码来了。


#include "BigToSmallScene.h"USING_NS_CC;Scene* BigToSmallScene::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease object    auto layer = BigToSmallScene::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool BigToSmallScene::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }        Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();   //创建EditBoxeditBox = EditBox::create(Size(200, 40), Scale9Sprite::create());editBox->setPosition(visibleSize/2);  //输入模式editBox->setInputMode(EditBox::InputMode::ANY);    //设置委托 editBox->setDelegate(this);editBox->setFontColor(Color3B::WHITE);//设置文字颜色      editBox->setText("Please Input FileName");editBox->setFontSize(24);    this->addChild(editBox,100);  //这么create内部帮你建立了一个labelauto button=ControlButton::create("click me","Arial",24);button->setPosition(visibleSize.width/2,visibleSize.height/2-100);//Control都是这么监听事件的。button->addTargetWithActionForControlEvents(this,cccontrol_selector(BigToSmallScene::doExchange),Control::EventType::TOUCH_DOWN);this->addChild(button);    return true;}void BigToSmallScene::menuCloseCallback(Ref* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");    return;#endif    Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif}void BigToSmallScene::doExchange(Ref * ref, Control::EventType controlEvent){//ref果然就是objectauto button=dynamic_cast<ControlButton*>(ref);//内部对plist进行解析 变成一个个texture。SpriteFrameCache::getInstance()->addSpriteFramesWithFile(editBox->getText());//不要问这是什么  , 这是我从addSpriteFramesWithFile中扒出来的代码//plist文件可以看做一个xml文件,里面都是键值对ValueMap dict = FileUtils::getInstance()->getValueMapFromFile("sprite_sheet.plist");      ValueMap& framesDict = dict["frames"].asValueMap();   for (auto iter = framesDict.begin(); iter != framesDict.end(); ++iter)    {      //这就是名字std::string spriteFrameName = iter->first;//通过名字得到spriteSprite *sprite = Sprite::createWithSpriteFrameName(spriteFrameName);//渲染纹理RenderTexture *render = RenderTexture::create(sprite->getContentSize().width,sprite->getContentSize().height);sprite->setAnchorPoint(Vec2(0,0));//原理应该是begin开始记录数据    end停止记录    saveToFile把数据保存到图片中render->begin();sprite->visit();render->end();//内部找一个getWritablePath()的地方存放图片,想改就改吧//FileUtils::getInstance()->getWritablePath() + fileName;render->saveToFile(spriteFrameName,cocos2d::Image::Format::PNG,true);       }}


#ifndef __BIGTOSMALL_SCENE_H__#define __BIGTOSMALL_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h" USING_NS_CC_EXT;class BigToSmallScene : public cocos2d::Layer,public cocos2d::extension::EditBoxDelegate{public:    // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::Scene* createScene();    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();          // a selector callback    void menuCloseCallback(cocos2d::Ref* pSender);        // implement the "static create()" method manually    CREATE_FUNC(BigToSmallScene);//对应EditBoxDelegate的东东void editBoxEditingDidBegin(EditBox* editBox){};  void editBoxEditingDidEnd(EditBox* editBox){};  void editBoxTextChanged(EditBox* editBox, const std::string& text){};  void editBoxReturn(EditBox* editBox){//doExchange(nullptr,Control::EventType::VALUE_CHANGED);};  void doExchange(Ref * ref, Control::EventType controlEvent);   EditBox* editBox;};#endif // __BIGTOSMALL_SCENE_H__


一个输入框,一个按钮竟然让我困惑了许久。下面是我遇到的问题的解决方式。


详情请见

http://my.oschina.net/JeremyOuyang/blog/266019

Cocos2d-x 3.0 Alpha 1开始 对目录结构进行了整合。结果有些附加项目也被在项目中被精简出去。

比如说如果你需要使用CocoStdio导出的JSON、或使用Extensions扩展库,libCocosStudio、libExtensions、libGUI都需要在你手动添加。。。


自己看去吧,主要就是ui控件要添加附加项目,注意添加 $(EngineRoot)有些include路径有些问题。


编辑框主要就是设置委托,很简单明了。

//设置委托 
editBox->setDelegate(this);



按钮主要是监听事件。

//Control都是这么监听事件的。
button->addTargetWithActionForControlEvents(this,cccontrol_selector(BigToSmallScene::doExchange),Control::EventType::TOUCH_DOWN);


注释挺明确的  自己看吧


0 0
原创粉丝点击