为什么我无法在 Cocos2d-x 3.2 中获得重力值
Why I couldn't get gravity value in Cocos2d-x 3.2
我是cocos2d-x的新手。我试图使用cocos2d-x3.2来构建一个简单的物理游戏,但我遇到了一个问题。一开始,我遵循了教程,并在HelloWorld中添加了这些。h:
private:
PhysicsWorld* m_world;
public:
void setPhyWorld(PhysicsWorld* world){ m_world = world; }
然后,我在HelloWorld.cpp中添加了这些:
Scene* HelloWorld::createScene()
{
auto scene = Scene::createWithPhysics();
auto layer = HelloWorld::create();
layer->setPhyWorld(scene->getPhysicsWorld());
scene->addChild(layer);
return scene;
}
然后,我试图在函数init()中获得重力值,如下所示:
bool HelloWorld::init()
{
Vect g=m_world->getGravity();
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
return true;
}
然后,我运行了它,但程序停止在"Vect g=m_world->getGravity();"它说"0x00C53B44有一个未处理的异常",我不得不中断它。以前有人遇到过同样的问题吗?如果有人能帮我,我真的很感激。
请遵守您的代码
auto layer = HelloWorld::create(); //Calling init Method of Hello World Layer
layer->setPhyWorld(scene->getPhysicsWorld()); // Setting PhysicsWorld instance to the layer
首先调用init()方法,然后将setPhyWorld(scene->getPhysicsWorld())设置为m_world=null。
如果你真的想在init()方法中使用physicsWorld实例,你应该自定义create&init方法,并使用create()方法发送physicsWorld实例。
//This code above header class of HelloWorld
#define CUSTOM_CREATE_FUNC(__TYPE__)
static __TYPE__* create(PhysicsWorld* world)
{
__TYPE__ *pRet = new __TYPE__();
if (pRet && pRet->init(world))
{
pRet->autorelease();
return pRet;
}
else
{
delete pRet;
pRet = NULL;
return NULL;
}
}
然后
CUSTOM_CREATE_FUNC(HelloWorld); // in the header class, instead of CREATE_FUNC(HelloWorld)
virtual bool init(PhysicsWorld* world);
和
auto layer = HelloWorld::create(scene->getPhysicsWorld()); // in the createScene() Method
最后是
bool HelloWorld::init(PhysicsWorld* world)
{
m_world = world;
Vect g=m_world->getGravity();
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
return true;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么不;名字在地图上是按顺序排列的吗
- 我的字符计数代码计算错误.为什么
- 为什么精灵在 cocos2d-x 中颤抖
- 为什么Cocos2D-X不一第一次显示精灵
- 为什么 cocos2d 和 libdispatch.dylib 如此频繁地调用 alloc
- 为什么我们更喜欢在 cocos2d 中使用 "CCFollow" 而不是"CCCamera"?
- 为什么我无法在 Cocos2d-x 3.2 中获得重力值
- 为什么cocos2d-x使用来自服务器的多线程加载sprite是黑色的
- 为什么cocos2d-x schedule改变了成员对象