游戏开发——cocos2dx v3.2随笔三

Home / C++ MrLee 2016-1-11 2979

一、VS里开发Cocos2dx 3.2时代码折叠。 C++不像C#那样直接#region可以折叠,很苦恼。度娘谷歌,发现可以这样:
#pragma region name  
#pragma endregion
欣喜万分! 二、获取本层所有精灵
auto sp = this->getChildren();//获取这个层中所有的孩子,也就是所有的精灵
比较完整的方法,获取本层所有精灵,并且使所有精灵向下移动:
voidSecond::myupdate(float f){//注意有一个float f 形参  
    auto sp = this->getChildren();//获取这个层中所有的孩子,也就是所有的精灵。  
    for (auto a: sp)  //这个类似于迭代器的遍历 你可以改成for(auto a= sp.begin();a!=sp.end();a++){}
    {  
       a->setPosition(a->getPosition().x, a->getPosition().y - 2);  
    }  
}  
三、切换场景切换场景有两个切换方式,一种是replacescene,一种是pushscene。这两种切换方式的区别在于,前者释放了当前场景,后者把当前场景压入栈中保存。pushscene的场景要切换回来,只需在子场景中调用popscene即可。 相当于原来的场景暂停了一会。这里有个问题,暂时记住:PopScene回来失效。 四、方法报错"没有找到重载的成员函数"时。1.检查是否在.h中声明。2.检查方法是否填写cocos2d::。3.上面2,如果.h中#include "cocos2d.h"后面有USING_NS_CC,可不添加cocos2d::。
多分辨率适应
glview->setDesignResolutionSize(435, 640, ResolutionPolicy::SHOW_ALL);
目前只加了官方默认的这句,先这么用着,等界面完备时再调整。 三、关于.plist文件。加载plist文件时,可以同时加载,但是.plist文件里拆分图片时,不同.plist名字不能一样,不然你就杯具去吧。下面异步加载示例【单个文件】:
void LoadingScene::onEnter(){
	auto size=Director::getInstance()->getVisibleSize();
	//添加背景
	auto splash=Sprite::create("load.png");
	splash->setPosition(Point(size.width/2,size.height/2));
	this->addChild(splash);
	//图片资源的异步加载
	TextureCache::getInstance()->addImageAsync("ui/colorball.png",CC_CALLBACK_1(LoadingScene::loadComplete,this));
}
//图片加载完成后的回调函数
void LoadingScene::loadComplete(Texture2D *texture){
	//texture是加载完的图片资源
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/colorball.plist",texture);
	log("load complete");
	//界面切换
	/*auto gameLayer=GameScene::create();
	Director::getInstance()->replaceScene(TransitionRotoZoom::create(3.0f,gameLayer));*/
	auto scene = MainScene::createScene();
	Director::getInstance()->replaceScene(TransitionFade::create(3.0f, scene));
}
然后在.h里声明函数:
	void onEnter() override;
	void loadComplete(Texture2D *texture);
加载多个:
	//png加入全局cache中	
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/colorball.plist", "ui/colorball.png");
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ui/colorballPress.plist", "ui/colorballPress.png");
	auto cache = SpriteFrameCache::getInstance();//共享的帧序列
使用时,按设定名字使用即可。由于加载到一个cache里,所以重名就乱套了。
Cocox2dx3.2重力方向设定。
    this->getPhysicsWorld()->setGravity(Vect(x,y));

本文链接:https://www.it72.com/7546.htm

推荐阅读
最新回复 (0)
返回