认识 Cocos2dx 3.x

认识 Cocos2dx 3.x,第1张

概述cpp11: 支持了auto标识符, 这样可以大大简化了声明变量的类型 auto标识符: 泛类定义 nullptr 代替 NULL, NULL只代表内存地址位为0, 这样做有时会出问题, cpp11后尽量用nullptr static_cast< Class >obj相当于c语言的(Class)obj, dynamic_cast更智能, 无法转换会返回nullptr 设置指针型布尔 std::sh cpp11: 支持了auto标识符,这样可以大大简化了声明变量的类型 auto标识符: 泛类定义 nullptr 代替 NulL,NulL只代表内存地址位为0,这样做有时会出问题,cpp11后尽量用nullptr static_cast< Class >obj相当于c语言的(Class)obj,dynamic_cast更智能,无法转换会返回nullptr 设置指针型布尔 std::shared_ptr<bool> enable(new bool(true));
获取和赋值 (*enable) = false; 7. 创建工程 下载最新的cocos2dx 3.x版本,解压缩 打开terminal,cd 找到3.x解压缩的路径位置/tools/cocos2d-console/bin 执行 ./cocos new Projectname -l cpp -p Packagename -d ~/Desktop 自行替换上面的Projectname和Packagename内容,以及对应语言,我使用c++语言,保存到桌面上 std::nothrow 在内存不足时,new (std::nothrow)并不抛出异常,而是将指针置NulL

[相对cocos2dx2.x来看待]

基础 设置z层级 setLocalZOrder(0); 获得z层级 getGlobalZOrder(); 层级参考链接
globalZOrder 是用于 渲染器 中用来给“绘制命令”排序的 localZOrder 是用于父节点的子节点数组中给 节点 对象排序的 设置颜色 setcolor(color3B::RED); 清空颜色 setcolor(color3B::WHITE); 获取锚点对应坐标 Vec2::ANCHOR_BottOM_left 菜单按钮绑定 CC_CALLBACK_1(MyClass::func,this) 获取屏幕大小 Director::getInstance()->getWinSize() 获取屏幕可视区域 Director::getInstance()->getopenGLVIEw()->getVisibleRect() ActionManager: VisibleRect 屏幕可以获得屏幕的指定位置点,可以获得屏幕的大小 同时可并行执行多条 action CC_CALLBACK_0 调用函数指针,执行参数为0的函数 CC_CALLBACK_1 执行参数为1的函数 removeChild 可以remove 自身 Director::getInstance()->getActionManager()->addAction(action,target,true); 另外一种添加动作的方式,第三个参数为不自动执行 Director::getInstance()->getActionManager()->resuMetarget(target); 回复执行对象动作 Director::getInstance()->getActionManager()->pauseTarget(target); 设置后对象即使添加新的动作也无法执行 schedule(CC_SCHEDulE_SELECTOR(MyClass::funct),3); 每三秒执行一次
8.unschedule(CC_SCHEDulE_SELECTOR(MyClass::funct)); 停止循环执行函数 在执行动作的过程中调用函数,并终止动作,不会导致crash action的tag可以重复,可通过sprite->stopAllActionsByTag(kTag); 删除多个动作,而且可以保留其他动作继续执行 Action actionBy->reverse() 可以创建相对运动的逆运动 target->setposition3D(Vec3(40,40,0)); 伪3d,z负责缩放,moveBy moveto 同样支持 SkewTo可以创造3d翻转 target->ignoreAnchorPointForposition(false); 不忽略锚点 JumpBy::create(time,targetposition,high,jumpTimes); PointArray->addControlPoint(Vec2(0,0)); 点数组存储点 DrawNode->drawCardinalSpline(pointArray,100,color4F(1.0,0.0,1.0,1.0)); CardinalSplineBy::create(3,array,1); 按照点以及点的张力运动 CatmullRomBy::create(3,array); 非封闭点运动 CallFunc::create(std::bind(&TargetClass::funct,this,param) ) 绑定函数,这样可以传递参数,也支持数值类型
CallFuncN::create( CC_CALLBACK_1(ActionCallFuncN::callback,this))
CallFunc::create( std::bind(&ActionCallFunction::callback1,this) )
CallFunc::create( std::bind(&ActionCallFunction::callback2,_tamara) )
CallFunc::create( std::bind(&ActionCallFunction::callback3,_kathia,42) ) CallFuncND 不再需要,已经被std::bind()替代 sequence 序列 可以支持reverse翻转 OrbitCamera 轨道动作 this->runAction(Follow::create(target,Rect(0,s.wIDth * 2 - 100,s.height))); 舞台镜头跟踪对象 TargetedAction::create(target,jump2) 混合其他对象动作到当前动作中,支持reverse this->schedule([&](float dt) { log(“dealing”);},5,false,“key”); 延迟函数(函数内容,周期间距,重复次数,延迟,key) scheduleOnce([&](float dt) { log(“dealing”);},2,”key”); auto actionfloat = Actionfloat::create(2.f,3,[this](float value) { target->setScale(value); }); 无需自定义变量,设置临时数值变化 action->clone() 再也不需要去写autorelease,已经被包含在clone内部,非常方便 Scheduler 的 timeScale 可以正倒放动作 Progressbar 已支持SpriteFrame方式进度条
auto s = Director::getInstance()->getWinSize();    auto to = Sequence::createWithTwoActions(Progressto::create(6,100),Progressto::create(0,0));    SpriteFrameCache::getInstance()->addSpriteFramesWithfile("zwoptex/grossini.pList");    auto left = Progresstimer::create(Sprite::createWithSpriteFramename("grossini_dance_01.png"));    //Progresstimer::Type::RADIAL/bar    left->setType(Progresstimer::Type::bar);    //radial模式设置为逆时针旋转    //left->setReverseProgress(true);    left->setMIDpoint(Vec2(0.5f,0.5f));    left->setbarChangeRate(Vec2(1,0));    addChild(left);    left->setposition(100,s.height/2);    left->runAction(RepeatForever::create(to->clone()));@H_502_247@  sound & music     我能了解到的是v2.x版本的苦恼,原因是2.x版本的sfx在loop的时候100%无法回收停止音效,应该在v3.x版本里面有所修正,待证实    camera     创建camera: auto camera = Camera::create(); 
camera->setCameraFlag(CameraFlag::USER1);
addChild(camera);
默认当前场景会有一个defaultCamera camera对象虽然也是被添加在node环节中,但是他的层级关系只跟设置的CameraFlag有关系,数值越大越在前端显示 在设置一个node节点整体为一个camera的内容时,如果想把子节点都设置为此camera的内容,则最好在所有addchild完成后再设置setCameraMask((unsigned short)CameraFlag::USER1) 参考链接 cocos2d 信息 读取pList信息 Configuration::getInstance()->loadConfigfile("configs/config-test-ok.pList") 获取信息 Configuration::getInstance()->getInfo() cocos2dx版本 Configuration::getInstance()->getValue("cocos2d.x.version") //cocos2d-x-3.7 openGL版本 Configuration::getInstance()->getValue("gl.version") //OpenGL ES 2.0 imgSGX543-113.3.1 获取无效值后会得到默认值 Configuration::getInstance()->getValue("invalID.key",Value("defaultStrValue")) 获取国家 Application::getInstance()->getCurrentLanguage() //LanguageType::CHInesE 获取国家码 Application::getInstance()->getCurrentLanguageCode() //zh touch 触摸绑定 EventListenertouch 触摸事件侦听 EventListenerKeyboard 键盘事件侦听 EventListeneracceleration 加速事件侦听 EventListenMouse 鼠标事件侦听 EventListenerCustom 自定义事件侦听 回调函数
lambda方式cpp11
[](int x,int y) { return x + y; } // 隐式返回类型 [](int& x) { ++x; } // 没有return语句 -> lambda 函数的返回类型是'voID' []() { ++global_x; } // 没有参数,仅访问某个全局变量 []{ ++global_x; } // 与上一个相同,省略了() [] //未定义变量.试图在Lambda内使用任何外部变量都是错误的. [x,&y] //x 按值捕获,y 按引用捕获. [&] //用到的任何外部变量都隐式按引用捕获 [=] //用到的任何外部变量都隐式按值捕获 [&,x] //x显式地按值捕获. 其它变量按引用捕获 [=,&z] //z按引用捕获. 其它变量按值捕获 以上参考链接 Listener->ontouchBegan = [=](touch* touch,Event* event){
return true;
};
Listener->ontouchended = [=](touch* touch,Event* event){
};
CC_CALLBACK_N宏回调方式
10. 事件侦听器派发
_eventdispatcher是Node的一个属性,使用它来管理当前节点(Scene,Layer和Sprite)不同事件的分配 使用clone()方法是因为每一个事件监听器只能被添加一次 如果是固定优先值的监听器添加到一个节点(addEventListenerWithFixedPriority),那当这个节点被移除时必须同时手动移除这个监听器,但是添加场景图优先监听器到节点(addEventListenerWithSceneGraPHPriority)就不用这么麻烦,监听器和节点是绑定好的,一旦节点的析构函数被调用,监听器也会同时被移除 _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener1,sprite1);
_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener1->clone(),sprite2);
_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener1->clone(),sprite3);
FixedPriority和SceneGraPHPriority
FixedPriority 整形值。低权值的事件监听器将优于高权值的事件监听器 SceneGraPHPriority Node的指针。Node的z顺序高的(绘制于顶部的)节点将优于z顺序低的节点。这将保证了诸如触碰事件的自顶向下传播

键盘

`auto Listener = EventListenerKeyboard::create();
Listener->onKeypressed = CC_CALLBACK_2(KeyboardTest::onKeypressed,this);
Listener->onkeyreleased = CC_CALLBACK_2(KeyboardTest::onkeyreleased,this);

_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);

// Implementation of the keyboard event callback function prototype
voID KeyboardTest::onKeypressed(EventKeyboard::KeyCode keyCode,Event* event)
{
log(“Key with keycode %d pressed”,keyCode);
}

voID KeyboardTest::onkeyreleased(EventKeyboard::KeyCode keyCode,Event* event)
{
log(“Key with keycode %d released”,keyCode);
} `

加速计事件

`auto Listener = EventListeneracceleration::create(CC_CALLBACK_2(AccelerometerTest::onacceleration,this));
_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);

// Implementation of the accelerometer callback function prototype
voID AccelerometerTest::onacceleration(acceleration* acc,Event* event)
{
// Processing logic here
}`

鼠标事件(v3新增)

`_mouseListener = EventListenerMouse::create();
_mouseListener->onMouseMove = CC_CALLBACK_1(MouseTest::onMouseMove,this);
_mouseListener->onmouseup = CC_CALLBACK_1(MouseTest::onmouseup,this);
_mouseListener->onMouseDown = CC_CALLBACK_1(MouseTest::onMouseDown,this);
_mouseListener->onMouseScroll = CC_CALLBACK_1(MouseTest::onMouseScroll,this);

_eventdispatcher->addEventListenerWithSceneGraPHPriority(_mouseListener,this);`

自定义事件
_Listener = EventListenerCustom::create("game_custom_event1",[=](EventCustom* event){
std::string str("Custom event 1 received,");
char* buf = static_cast<char*>(event->getUserData());
str += buf;
str += " times";
statusLabel->setString(str.c_str());
});
_eventdispatcher->addEventListenerWithFixedPriority(_Listener,1);
事件触发
static int count = 0;
++count;
char* buf = new char[10];
sprintf(buf,"%d",count);
EventCustom event("game_custom_event1");
event.setUserData(buf);
_eventdispatcher->dispatchEvent(&event);
CC_SAFE_DELETE_ARRAY(buf);
事件移除
移除单独侦听器 _eventdispatcher->removeEventListener(Listener); 移除所有侦听起,如果在同一语句下新增了新的事件,那么全部删除语句并不会影响到下一步 *** 作 _eventdispatcher->removeAllEventListeners(); 创建单一事件侦听器
EventListenertouchOneByOne::create() 其他
触碰点转换为位置点 Vec2 locationInNode = this->convertToNodeSpace(touch->getLocation()); 检测是否点击区域 Size s = target->getContentSize();
Rect rect = Rect(0,s.wIDth,s.height);
if (rect.containsPoint(locationInNode))
拖动物体跟随移动 auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setposition(target->getposition() + touch->getDelta());
事件优先级
如果设置了优先级,那么这个触碰就和addchild层级没关系了,不设置是默认随层级走的,层级越高,越靠近camera越容易被触碰,而如果设置了优先级,不管你child层级如何,都按照指定优先级来,优先级priority是按照从小到大数字来排列先后点击的 使用优先级调用的是addEventListenerWithFixedPriority方法,所以必须手动回收 //使用优先级
_eventdispatcher->addEventListenerWithFixedPriority(Listener,_fixedPriority);
//不使用优先级
_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);
移除和添加事件
移除 _eventdispatcher->removeEventListener(Listener1); 移除指定类型 _eventdispatcher->removeEventListenersForType(EventListener::Type::touch_ONE_BY_ONE); 添加 _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener1,sprite1); 在添加事件后,对象retain并移除,再次出现时是可以继续执行触碰 *** 作的. 给的例子来看,在隐藏后显示,未松开手指拖动,还是会直接触碰touchmove事件,这点需要注意 direct事件
更新 Director::EVENT_AFTER_UPDATE 访问 Director::EVENT_AFTER_VISIT 绘制 Director::EVENT_AFTER_DRAW 投影 Director::EVENT_PROJECTION_CHANGED 通过 Director::getInstance()->getEventdispatcher() 的 addCustomEventListener 方式来添加事件 绑定函数 std::bind(&DirectorEventTest::onEvent1,std::placeholders::_1) 绑定匿名函数 [&](EventCustom *event) {} 多点触摸
EventListenertouchAllAtOnce 获得第一个点击的点 touches[0] 暂停和恢复事件
暂停 _eventdispatcher->pauseEventListenersForTarget(this,true); //参数2为是否递归 恢复 _eventdispatcher->resumeEventListenersForTarget(this,true); //参数2为是否递归 一次性事件 removeListenerOntouchended(true); //触摸结束不再接收触摸 引擎扩展 动态下载 参照 AssetsManagerExTest 事例 滑动条 参照 CCControlSlIDerTest 事例 取色器 参照 ControlColourPickerTest 事例 开关 参照 ControlSwitchTest 事例 色彩文字按钮 参照 ControlbuttonTest 事例 音量旋钮 参照 ControlPotentiometerTest 事例 加减按钮 参照 CCControlStepperTest 事例 ccbi界面 参照 CocosBuilderTest 事例 滚动界面 参照 tableVIEwTest 事例 图片加密 参照 Texure Atlas Encryption 事例 材质特效 创建滤镜
auto propertIEs = PropertIEs::createNonRefCounted("Materials/2d_effects.material#sample");
Material *mat1 = Material::createWithPropertIEs(propertIEs);
模糊特效
spriteBlur->setnormalizedposition(Vec2(0.2f,0.5f));
spriteBlur->setGLProgramState(mat1->getTechniqueByname("blur")->getpassByIndex(0)->getGLProgramState());
外发光
spriteOutline->setnormalizedposition(Vec2(0.4f,0.5f));
spriteOutline->setGLProgramState(mat1->getTechniqueByname("outline")->getpassByIndex(0)->getGLProgramState());
噪点
spriteNoise->setnormalizedposition(Vec2(0.6f,0.5f));
spriteNoise->setGLProgramState(mat1->getTechniqueByname("noise")->getpassByIndex(0)->getGLProgramState());
边缘检测
7.spriteEdgeDetect->setnormalizedposition(Vec2(0.8f,0.5f));
spriteEdgeDetect->setGLProgramState(mat1->getTechniqueByname("edge_detect")->getpassByIndex(0)->getGLProgramState());
效果拖尾
参考 Node: MotionStreak 支持色彩和贴图拖尾 物理引擎 参考 Node: Physics / 3D
屋里引擎 关节 参考 Joints action和物理引擎的互相影响 Actions 只处理单面碰撞 One Way Platform 切割 Slice 数据存储

参考
cocos2d-x中几种存储数据的方式

向量等

参考:
总结数学类Vec2/Size/Rect

参考链接:

总结Cocos2d-x 3.x版本的一些变化

Value、Vector和Map的认识

ps: v4都出了泪奔了~~~

ValueMap的一般使用

总结

以上是内存溢出为你收集整理的认识 Cocos2dx 3.x全部内容,希望文章能够帮你解决认识 Cocos2dx 3.x所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1070310.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存