问题描述
我需要在两个物体之间创建连接。关节将在物理世界中添加。我如何在层中访问物理世界?
I need to create joint between two bodies in layer. Joints are to be added in physics world. How can i access physics world in layer?
推荐答案
您有四个选择:
1)-覆盖Layer :: onEnter()或onEnterTransitionDidFinish()
方法,然后从这样的主题进行访问:
1)- override Layer::onEnter() or onEnterTransitionDidFinish ()methods then access from theme like this:
Layer::onEnter(){
Layer::onEnter();
auto world = _director->getRunningScene()->getPhysicsWorld();
}
2)-创建自定义图层:: createScene()方法然后从Layer :: init()进行访问,如下所示:
2)- create a custom Layer::createScene() method then access from Layer::init() like this:
Scene* GameScene::createScene()
{
auto layer = new (std::nothrow) GameScene;
if (layer)
{
auto scene = Scene::createWithPhysics();
layer->autorelease();
scene->addChild(layer);
if (layer->init()) return scene;
}
CC_SAFE_DELETE(layer);
return nullptr;
}
// then in Layer::init() you do this:
Layer::init(){
auto world = _director->getRunningScene()->getPhysicsWorld();
}
3)-将PhysicsWorld getter方法添加到导演(涉及一些库代码定制):
3)- add a PhysicsWorld getter method to the director (involves some library code customization's):
首先转到CCDirector.h,然后在NS_CC_BEGIN下添加场景类的前向声明,如下所示:
first go to CCDirector.h and then add a forward declaration of the scene class under NS_CC_BEGIN like this:
NS_CC_BEGIN
class cocos2d::Scene;
然后转到Director类的private部分,并添加如下两行:
then go to the private section of the Director class and add these two lines like this:
private:
cocos2d::PhysicsWorld* _myPhysicsWorld;
friend class Scene;
然后转到Director类的public部分,并添加如下这样的getter方法:
then go to the public section of the Director class and add this getter method like this:
public:
cocos2d::PhysicsWorld* getWorld() const{
return _myPhysicsWorld;
};
这是CCDirector.h文件中所做更改的概述:
this is an overview of the changes made in CCDirector.h file:
// CCDirector.h
NS_CC_BEGIN
// 1
class cocos2d::Scene;
class Director : public Ref{
public:
//2
cocos2d::PhysicsWorld* getWorld() const{
return _myPhysicsWorld;
};
private:
// 3
cocos2d::PhysicsWorld* _myPhysicsWorld;
friend class cocos2d::Scene;
};
NS_CC_END
所以之后,我们进入CCScene.cpp方法带有此签名(bool Scene :: initWithPhysics())的您会发现对PhysicsWorld :: construct(this)的调用在该方法之后立即添加此行:
so after that we go to CCScene.cpp into a method with this signature (bool Scene::initWithPhysics()) in it you will find a call to PhysicsWorld::construct(this) add just right after that method call this line:
_director->_myPhysicsWorld = _physicsWorld;
这应该是我们在CCScene.cpp中所做的概述:
and this should be the overview of what we did in CCScene.cpp:
// CCScene.cpp
class Scene : public Node{
bool Scene::initWithPhysics(){
_physicsWorld = PhysicsWorld::construct(this);
// 4 since Director is a singleton class, we can access its instance from anywhere
_director->_myPhysicsWorld = _physicsWorld;
}
};
现在,只要Scene :: createWithPhysics()被调用,导演就会得到副本的副本。 PhysicsWorld指针,可随时使用我们的导演getWorld()方法进行访问! (因为您所知道的是导演是一个单身人士),仅此而已,就不会将_myPhysicsWorld暴露给用户,这意味着他只能从外部读取_myPhysicsWorld !!
now as soon as Scene::createWithPhysics() gets called anywhere the director will get a copy of the PhysicsWorld pointer which can be accessed using our director getWorld() method anywhere anytime!! (because the director is a singleton as you know) and thats all without exposing _myPhysicsWorld to the user meaning he can only read _myPhysicsWorld from the outside!!
4 )-制作自己的Custom PhysicsWorld类,由于PhysicsWorld :: construct()受保护,因此可以轻松继承。
4)- make your own Custom PhysicsWorld class and since PhysicsWorld::construct() is protected which means it can be easily inherited.
// PhysicsManager.h
class PhysicsManager : public cocos2d::PhysicsWorld
{
public:
static PhysicsManager* createWorld(cocos2d::Scene* scene) {
_myPhysicsWorld = PhysicsWorld::construct(scene);
}
PhysicsWorld* getWorld() const { return _myPhysicsWorld; }
private:
static cocos2d::PhysicsWorld* _myPhysicsWorld;
};
//PhysicsManager.cpp
PhysicsManager::PhysicsWorld* _myPhysicsWorld;
现在您可以像这样使用它:
now you can use it like this:
Layer::init(){
auto physicsManager = PhysicsManager::createWorld(this->getScene());
auto world = physicsManager->getWorld();
}
请记住,如果您甚至可以将其设置为单例课程
remember that you can even make it a singleton class if you want to!
编辑:
我忘了还有另一个此处的好解决方案:
I forgot to mention that there is another good solution in here:
这篇关于如何在层中访问物理世界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!