其它信息
- 下载: http://cdn.cocos2d-x.org/cocos2d-x-3.0alpha1.zip
- 完整的更新日志: https://github.com/cocos2d/cocos2d-x/blob/cocos2d-x-3.0alph1/CHANGELOG
- API 参考:http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha1/index.html
要求
运行环境要求:
- Android2.3或以上
- ios 5.0 或以上
- OS X 10.7 或以上
- Windows 7 或以上
- Linux Ubuntu 12.04 (或以上)
- Marmalade 部分不适用
- lackBerry 部分不适用
编译器要求:
- mac和ios 上用 xcode4.6
- Linux 或者 Android 上用 gcc4.7。Android要求 Androidndk-r8e或以上。
- Window 上用 Visual Studio 2012
V3.0.0的特色
- 用c++(c++11)的最佳实践代替Objective-C的多余部分。
- 改良 Labels
- 改良 renderer
- 新的事件调度
- 集成物理
- 新的GUI
- javascript 远程调试
- 控制台模式
- 重构图像——及时释放内存和统一所支持文件格式的API
- 自动生成lua绑定,LuaJavaBridge,和LuaObjcBridge.
- 模板库
细节特性
c++11特性
在v3.0.0初期测试版上加上了这个特性
c++11 的一部分特性开始在cocos2d-x上使用。
- std::function.加入lambda对象作为回调
- 强类型枚举,在cocos2d-x中的枚举和常量中大量的使用。
- std::thread 用于线程
- override 关键字,检测重载方法。
Std::function
- CallFunc 可以用 std::function<void()>来创建
- CallFuncN 可以用 std::function<void(Node*)>来创建
- CallFuncND 和 CallFuncO都被移除了,这可以用CallFuncN和CallFunc来模拟,在ActionsTest.cpp中有更多的例子可以查看。
- MenuItem 支持 std::function<void(Node*)>回调
CallFunC 例子。
// in v2.1
CCCallFunc *action1 = CCCallFunc::create( this, callfunc_selector( MyClass::callback_0 ) );
// in v3.0 (short version)
auto action1 = CallFunc::create( CC_CALLBACK_0(MyClass::callback_0,this));
auto action2 = CallFunc::create( CC_CALLBACK_0(MyClass::callback_1,this, additional_parameters));
// in v3.0 (long version)
auto action1 = CallFunc::create( std::bind( &MyClass::callback_0, this));
auto action2 = CallFunc::create( std::bind( &MyClass::callback_1, this, additional_parameters));
// in v3.0 you can also use lambdas or any other "Function" object
auto action1 = CallFunc::create(
[&](){
auto s = Director::sharedDirector()->getWinSize();
auto label = LabelTTF::create("called:lambda callback", "Marker Felt", 16);
label->setPosition(ccp( s.width/4*1,s.height/2-40));
this->addChild(label);
} );
MenuItem 例子
// in v2.1
CCMenuItemLabel *item = CCMenuItemLabel::create(label, this, menu_selector(MyClass::callback));
// in v3.0 (short version)
auto item = MenuItemLabel::create(label, CC_CALLBACK_1(MyClass::callback, this));
// in v3.0 (long version)
auto item = MenuItemLabel::create(label, std::bind(&MyClass::callback, this, std::placeholders::_1));
// in v3.0 you can use lambdas or any other "Function" object
auto item = MenuItemLabel::create(label,
[&](Object *sender) {
// do something. Item "sender" clicked
});
强类型枚举
在V3.0初期测试版中加入
常量和枚举都以k开头,它们经常被定义为int和普通的enum的地方都被置换为强类型枚举类型(enum class) 以避免冲突和类型错误。
新的格式为:
v2.1 | v3.0 |
kTypeValue | Type::VALUE |
例子:
v2.1 | v3.0 |
kCCTexture2DPixelFormat_RGBA8888 | Texture2D::PixelFormat::RGBA8888 |
kCCDirectorProjectionCustom | Director::Projection::CUSTOM |
ccGREEN | Color3B::GREEN |
CCPointZero | Point::ZERO |
CCSizeZero | Size::ZERO |
旧的类型仍然可以使用,但是不赞成使用。
override
这个可以在重载方法时捕捉到可能发生的错误,在子类的重载方法中加上了override关键字
例如:
class Sprite : public Node {
bool isFlipY(void) const;
void setFlipY(bool bFlipY);
// Overrides
virtual void setTexture(Texture2D *texture) override;
virtual Texture2D* getTexture() const override;
inline void setBlendFunc(const BlendFunc &blendFunc) override;
inline const BlendFunc& getBlendFunc() const override;
}
移除Objective-c模式。
在v3.0.0初期测试版中加入这个特性。
c++ 类和一般函数都移除了‘CC’前缀
在类中的改变
当cocos2d-x使用cocos2d名称空间时,就不必要为所有的类加‘CC’前缀了。
例如:
v2.1 | v3.0 |
CCSprite | Sprite |
CCNode | Node |
CCDirector | Director |
etc... |
v2.1中的类名仍然可以使用,不过它们都被标记为过时。
在一般函数中的改变
在基本画图中:
- 他们都加到了DrawPrimitives名称空间中。
- 都移除了 CC前缀。
在 gk的代理函数中:
- 他们都加到了 GL名称空间中。
- 都移除了 ccGL前缀。
例如:
v2.1 | v3.0 |
ccDrawPoint() | DrawPrimitives::drawPoint() |
ccDrawCircle() | DrawPrimitives::drawCircle() |
ccGLBlendFunc() | GL::blendFunc() |
ccGLBindTexture2D() | GL::bindTexture2D() |
etc... |
v2.1 中的一般函数仍然可以使用,不过它们都被标记为过时。
clone()代替 copy()
clone() 返回一个自动释放的复制对象。
copy() 不再支持。如果你使用了它,编译可以通过,但是程序会崩溃。
例子:
// v2.1 CCMoveBy *action = (CCMoveBy*) move->copy(); action->autorelease(); // v3.0 // No need to do autorelease, no need to do casting. auto action = move->clone();
单例中用getInstance() 和 destroyInstance()
所有的单例都用 getInstance()和 destroyInstance()来获取和销毁实例(如果可以的话)
例子:
v2.1 | v3.0 |
CCDirector->sharedDirector() | Director->getInstance() |
CCDirector->endDirector() | Director->destroyInstance() |
etc... |
v2.1中的方法仍然有效,但是他们被标记为过时。
getters
现在Getters 使用get前缀。
例子:
v2.1 | v3.0 |
node->boundingBox() | node->getBoundingBox() |
sprite->nodeToParentTransform() | sprite->getNodeToParentTransform() |
etc... |
添加 getters 时一般都会在其声明中标记为const。例如:
// v2.1 virtual float getScale(); // v3.0 virtual float getScale() const;
v2.1中的方法仍然有效,但是他们被标记为过时。
POD 类型
当方法收到POD类型的参数时(例如:TexParams,Point,Size,等等)都开始用 const引用了。
例如:
// v2.1 void setTexParameters(ccTexParams* texParams); // v3.0 void setTexParameters(const ccTexParams& texParams);
新的渲染器
暂无介绍
改良LabelTTF/LabelBMFont
在V3.0测试版加上此特性。
新的事件调度
在V3.0测试版加上此特性。
所有像触摸(touch)事件,键盘(keyboard)事件,加速器(acceleration)事件,还有自定义(costom)事件,都派遣自EventDispatcher
.TouchDispatcher
, KeypadDispatcher
, KeyboardDispatcher
, AccelerometerDispatcher
的都被移除了。
添加触摸(touch)事件监听(Listener)
auto sprite = Sprite::create("file.png"); ... auto listener = EventListenerTouch::create(Touch::DispatchMode::ONE_BY_ONE); listener->setSwallowTouch(true); listener->onTouchBegan = [](Touch* touch, Event* event) { do_some_thing(); return true; }; listener->onTouchMoved = [](Touch* touch, Event* event) { do_some_thing(); }; listener->onTouchEnded = [](Touch* touch, Event* event) { do_some_thing(); }; listener->onTouchCancelled = [](Touch* touch, Event* event) { do_some_thing(); }; //这个触摸监听的优先级是基于在显示这个精灵的轴的排序确定的 EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, sprite); // 或者你可以自己指定一个优先级 EventDispatcher::getInstance()->addEventListenerWithFixedPriority(listener, 100); // 100 is a fixed value
在3.0alpha1 中EventListenerTouch这个类已经被删掉了。代替的是 EventListenerTouchOneByOne 和 EventListenerTouchAllAtOnce 这两个类这样也就简化了创建了。
添加键盘事件监听
auto listener = EventListenerKeyboard::create(); listener->onKeyPressed = CC_CALLBACK_2(SomeClass::onKeyPressed, this); listener->onKeyReleased = CC_CALLBACK_2(SomeClass::onKeyReleased, this); EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
添加加速器事件监听
auto listener = EventListenerAcceleration::create(CC_CALLBACK_2(SomeClass::onAcceleration, this)); EventDispatcher::getInstance()->addEventListenerWithSceneGraphPriority(listener, this);
添加自定义事件监听
auto listener = EventListenerCustom::create("game_custom_event", [=](EventCustom* event){ void* userData= event->getUserData(); do_some_with_user_data(); }); dispatcher->addEventListenerWithFixedPriority(listener, 1);
调度自定义事件
EventCustom event("game_custom_event"); event.setUserData(some_data); dispatcher->dispatchEvent(&event);
为监听(Listener)设置合适的优先级
dispatcher->setPriority(fixedPriorityListener, 200);
移除事件监听(Listener)
dispatcher->removeEventListener(listener);
移除同一类型的所有监听
dispatcher->removeListenersForEventType("event_name");
移除所有监听
dispatcher->removeAllListeners();
物理集成
此特性在v3.0的前期测试版加入
物理集成有5个概念: PhysicsWorld
, PhysicsBody
, PhysicsShape
, PhysicsJoint
和PhysicsContact
.
你必须在ccConfig.h中定义 CC_USER_PHYSICS宏才能使用这些物理 API。
物理世界(PhysicsWorld)
一个物理世界对象模拟碰撞和其他的物理属性。你不可以直接的去创建(create)它,你可以在带有物理集成所创建的scene中获得这个对象
Scene* scene = Scene::createWithPhysics(); PhysicsWorld* world = scene->getPhysicsWorld();
物理身体(PtysicsBody)
物理身体(PtysicsBody)对象用来为一个节点提供物理模拟。如果你创建了一个物理身体(PtysicsBody)并把它赋给一个节点,还胡把这个节点添加到了一个带有物理集成所创建的场景(scene)中的话,他将会在更新(update)的时候执行物理模拟。
PhysicsBody* body = PhysicsBody::createCircle(5.0f); Node* node = Node::create(); node->setPhysicsBody(body); scene->addChild(node);
物理形状(PhysicsShape)
一个物理形状(PhysicsShape)可以让一个身体(body)发生碰撞。你可以向一个物理身体(PhysicsBody)中添加一个或多个物理形状(PhysicsShape)
形状类:
:
PhysicsShapeCircle
,PhysicsShapeBox
,PhysicsShapePolygon
,PhysicsShapeEdgeSegment
,PhysicsShapeEdgeBox
,PhysicsShapeEdgePolygon
,PhysicsShapeEdgeChain
.PhysicsShape* shape = PhysicsShapeBox::create(Size(5.0f, 10.0f); body->addShape(shape);
物理关节(PhysicsJoint)
一个物理关节(PhysicsJoint)对象把两个物理身体(PhysicsBody)连接到一起,所以在物理世界(PhysicsWorld)中会一起被模拟。
关节类:
PhysicsJointFixed
,PhysicsJointLimit
,PhysicsJointPin
,PhysicsJointDistance
,PhysicsJointSpring
,PhysicsJointGroove
,PhysicsJointRotarySpring
,PhysicsJointRotaryLimit
,PhysicsJointRatchet
,PhysicsJointGear
,PhysicsJointMotor
.PhysicsJoint* joint = PhysicsJointDistance::construct(bodyA, bodyB, Point::ZERO, Point::ZERO); world->addJoint(joint);
物理接触(PhysicsContact)
在物理世界中物理接触是自动创建用来描述两个物理身体间的接触描述。你可以从物理接触事件监听中控制接触的行为。
包含接触信息的其他类:
PhysicsContactPreSolve
,PhysicsContactPostSolve
.物理引擎中的事件监听:
EventListenerPhysicsContact
,EventListenerPhysicsContactWithBodies
,EventListenerPhysicsContactWithShapes
,EventListenerPhysicsContactWithGroup
.auto contactListener = EventListenerPhysicsContactWithBodies::create(bodyA, bodyB); contactListener->onContactBegin = [](EventCustom* event, const PhysicsContact& contact) -> bool { doSomething(); return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);
其他的API更改
ccTypes.h
在ccTypes.h 中的结构体名字都移除了 cc前缀。
所有的函数都变为静态成员函数,所有的常量都变为静态的成员常量。
structure name before changing |
structure name after changing |
ccColor3B |
Color3B |
ccColor4B |
Color4B |
ccColor4F |
Color4F |
ccVertex2F |
Vertex2F |
ccVertex3F |
Vertex3F |
ccTex2F |
Tex2F |
ccPointSprite |
PointSprite |
ccQuad2 |
Quad2 |
ccQuad3 |
Quad3 |
ccV2F_C4B_T2F |
V2F_C4B_T2F |
ccV2F_C4F_T2F |
V2F_C4F_T2F |
ccV3F_C4B_T2F |
V3F_C4B_T2F |
ccV2F_C4B_T2F_Triangle |
V2F_C4B_T2F_Triangle |
ccV2F_C4B_T2F_Quad |
V2F_C4B_T2F_Quad |
ccV3F_C4B_T2F_Quad |
V3F_C4B_T2F_Quad |
ccV2F_C4F_T2F_Quad |
V2F_C4F_T2F_Quad |
ccBlendFunc |
BlendFunc |
ccT2F_Quad |
T2F_Quad |
ccAnimationFrameData |
AnimationFrameData |
总体改变例子:
// in v2.1 ccColor3B color3B = ccc3(0, 0, 0); ccc3BEqual(color3B, ccc3(1, 1, 1)); ccColor4B color4B = ccc4(0, 0, 0, 0); ccColor4F color4F = ccc4f(0, 0, 0, 0); color4F = ccc4FFromccc3B(color3B); color4F = ccc4FFromccc4B(color4B); ccc4FEqual(color4F, ccc4F(1, 1, 1, 1)); color4B = ccc4BFromccc4F(color4F); color3B = ccWHITE; // in v3.0 Color3B color3B = Color3B(0, 0, 0); color3B.equals(Color3B(1, 1, 1)); Color4B color4B = Color4B(0, 0, 0, 0); Color4F color4F = Color4F(0, 0, 0, 0); color4F = Color4F(color3B); color4F = Color4F(color4B); color4F.equals(Color4F(1, 1, 1, 1)); color4B = Color4B(color4F); color3B = Color3B::WHITE;
过时的函数和全局变量。
old name |
new name |
ccp |
Point |
ccpNeg |
Point::- |
ccpAdd |
Point::+ |
ccpSub |
Point::- |
ccpMult |
Point::* |
ccpMidpoint |
Point::getMidpoint |
ccpDot |
Point::dot |
ccpCrosss |
Point::cross |
ccpPerp |
Point::getPerp |
ccpRPerp |
Point::getRPerp |
ccpProject |
Point::project |
ccpRotate |
Point::rotate |
ccpUnrotate |
Point::unrotate |
ccpLengthSQ |
Point::getLengthSq() |
ccpDistanceSQ |
Point::getDistanceSq |
ccpLength |
Point::getLength |
ccpDistance |
Point::getDistance |
ccpNormalize |
Point::normalize |
ccpForAngle |
Point::forAngle |
ccpToAngle |
Point::getAngle |
ccpClamp |
Point::getClampPoint |
ccpFromSize |
Point::Point |
ccpCompOp |
Point::compOp |
ccpLerp |
Point::lerp |
ccpFuzzyEqual |
Point::fuzzyEqual |
ccpCompMult |
Point::Point |
ccpAngleSigned |
Point::getAngle |
ccpAngle |
Point::getAngle |
ccpRotateByAngle |
Point::rotateByAngle |
ccpLineInersect |
Point::isLineIntersect |
ccpSegmentIntersect |
Point::isSegmentIntersect |
ccpIntersectPoint |
Point::getIntersectPoint |
CCPointMake |
Point::Point |
CCSizeMake |
Size::Size |
CCRectMake |
Rect::Rect |
PointZero |
Point::ZERO |
SizeZero |
Size::ZERO |
RectZero |
Rect::ZERO |
TiledGrid3DAction::tile |
TiledGrid3DAction::getTile |
TiledGrid3DAction::originalTile |
TiledGrid3DAction::getOriginalTile |
TiledGrid3D::tile |
TiledGrid3D::getTile |
TiledGrid3D::originalTile |
TiledGrid3D::getOriginalTile |
Grid3DAction::vertex |
Grid3DAction::getVertex |
Grid3DAction::originalVertex |
Grid3DAction::getOriginalVertex |
Grid3D::vertex |
Grid3D::getVertex |
Grid3D::originalVertex |
Grid3D::getOriginalVertex |
Configuration::sharedConfiguration |
Configuration::getInstance |
Configuration::purgeConfiguration |
Configuration::destroyInstance() |
Director::sharedDirector() |
Director::getInstance() |
FileUtils::sharedFileUtils |
FileUtils::getInstance |
FileUtils::purgeFileUtils |
FileUtils::destroyInstance |
EGLView::sharedOpenGLView |
EGLView::getInstance |
ShaderCache::sharedShaderCache |
ShaderCache::getInstance |
ShaderCache::purgeSharedShaderCache |
ShaderCache::destroyInstance |
AnimationCache::sharedAnimationCache |
AnimationCache::getInstance |
AnimationCache::purgeSharedAnimationCache |
AnimationCache::destroyInstance |
SpriteFrameCache::sharedSpriteFrameCache |
SpriteFrameCache::getInstance |
SpriteFrameCache:: purgeSharedSpriteFrameCache |
SpriteFrameCache::destroyInstance |
NotificationCenter::sharedNotificationCenter |
NotificationCenter::getInstance |
NotificationCenter:: purgeNotificationCenter |
NotificationCenter::destroyInstance |
Profiler::sharedProfiler |
Profiler::getInstance |
UserDefault::sharedUserDefault |
UserDefault::getInstance |
UserDefault::purgeSharedUserDefault |
UserDefault::destroyInstance |
Application::sharedApplication |
Application::getInstance |
ccc3() |
Color3B() |
ccc3BEqual() |
Color3B::equals() |
ccc4() |
Color4B() |
ccc4FFromccc3B() |
Color4F() |
ccc4f() |
Color4F() |
ccc4FFromccc4B() |
Color4F() |
ccc4BFromccc4F() |
Color4B() |
ccc4FEqual() |
Color4F::equals() |
ccWHITE |
Color3B::WHITE |
ccYELLOW |
Color3B::YELLOW |
ccBLUE |
Color3B::BLUE |
ccGREEN |
Color3B::GREEN |
ccRED |
Color3B::RED |
ccMAGENTA |
Color3B::MAGENTA |
ccBLACK |
Color3B::BLACK |
ccORANGE |
Color3B::ORANGE |
ccGRAY |
Color3B::GRAY |
kBlendFuncDisable |
BlendFunc::BLEND_FUNC_DISABLE |
Lua 中的改变
使用绑定器(bindings-generator)工具进行绑定Lua
只需要在tool/tolua文件夹内配置*.ini文件就可以了,不用去写大量的*.pkg文件了。
使用ScriptHandlerMgr来管理Lua函数的注册和移除。
当我们想在类里面注册或移除Lua函数时,我们需要修改其声明和定义来绑定到Lua.
在3.0版本中我们用ScriptHandleMgr来代替这个工作。
我们用MenuItem 类来做例子:
在2.x版本中我们需要在MenuItem类的 .h文件中添加声明
virtual void registerScriptTapHandler(int nHandler); virtual void unregisterScriptTapHandler(void);
然后在 .cpp 文件中实现。在Lua中我们要这样调用
menuItem:registerScriptTapHandler(luafunction)
在3.0中我们只要把 HandlerType绑定到 ScriptHanderMgr就可以了。然后在Lua中我们这样实现
ScriptHandlerMgr:getInstance():registerScriptHandler(menuItem, luafunction,cc.HANDLERTYPE_MENU_CLICKED)
使用‘cc’和‘ccs’作为模块名
// v2.x CCSprite:create(s_pPathGrossini) CCEaseIn:create(createSimpleMoveBy(), 2.5) UILayout:create() // v3.0 cc.Director:getInstance():getWinSize() cc.EaseIn:create(createSimpleMoveBy(), 2.5) ccs.UILayer:create()
过时的 函数(funtions),表(tables),和类(classes)
尽可能的添加过时的函数,表,类来支持2.x版本。
使用Lua表(Lua table)来代替一些结构体和类的绑定。
Point、Size、Rect、Color3b、Color4b、Color4F、AffineTransform、FontDefinition、Array、Dictionary、PointArray 都没有限制。
不同的有
// v2.x local pt = CCPoint(0 , 0) local rect = CCRect(0, 0, 0, 0) // v3.0 local pt = cc.p(0, 0) local rect = cc.rect(0,0,0,0)
这些类的全局函数的改变
// in v2.x local pt = ccp(0,0) local color3B = ccc3(0, 0, 0) local color4B = ccc4(0, 0, 0, 0) // in v3.0 local pt = cc.p(0,0) local color3B = cc.c3b(0,0,0) local color4B = cc.c4b(0,0,0,0)
通过 LuaBasicConversion文件的功能,当他们作为参数传到绑定器(bindings generator)的时候就会改变Lua表(Lua table)。
存在问题:
你可以在这里找到存在的问题:here.
原文地址:http://www.cocos2d-x.org/wiki/Release_Notes_for_Cocos2d-x_v300#getters