首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CCSprite不是COCOS2dx中的物理体吗?

CCSprite不是COCOS2dx中的物理体吗?
EN

Stack Overflow用户
提问于 2014-03-12 16:53:53
回答 1查看 871关注 0票数 1

我有以下功能,它在cocos2dx中初始化了场景,据我所知,我做的一切都是正确的。但我的CCSprite仍然不是一个物理体。它保持静止在屏幕的中心,而它应该下降并受到重力的影响。

任何帮助都将不胜感激。提前谢谢。

代码语言:javascript
复制
void HelloWorld::initPhysics()
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
//creating the world
b2Vec2 gravity;
gravity.Set(0.0f, -20.0f);
world = new b2World(gravity);

// Do we want to let bodies sleep?
world->SetAllowSleeping(true);

world->SetContinuousPhysics(true);


CCSprite* bird = CCSprite::create("Harry@2x.png");
bird->setScale(2.0);
bird->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));
addChild(bird);

b2Body *_body;
// Create ball body and shape
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(screenSize.width,screenSize.height);
ballBodyDef.userData = bird;
_body = world->CreateBody(&ballBodyDef);

b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;

b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 100.0f;
ballShapeDef.friction = 0.5f;
ballShapeDef.restitution = 0.7f;
_body->CreateFixture(&ballShapeDef);
}

这是我的更新函数,我将world变量添加为全局变量。

代码语言:javascript
复制
void HelloWorld::update(float dt)
{
int velocityIterations = 8;
int positionIterations = 1;

world->Step(dt, velocityIterations, positionIterations);

//Iterate over the bodies in the physics world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
    if (b->GetUserData() != NULL) {
        //Synchronize the AtlasSprites position and rotation with the corresponding body
        CCSprite* myActor = (CCSprite*)b->GetUserData();
        myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
        myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-12 16:59:03

b2World* world是一个局部变量。这意味着它将超出当前函数末尾的范围,这意味着您无法调用world->Step(..)方法--为了提高物理世界的状态,必须定期调用该方法(通常是每一帧)。不踏入世界,就不会有任何运动。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22358513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档