Cocos2d iphone box2d实体向上而不是向下下落

Cocos2d iphone box2d实体向上而不是向下下落,cocos2d-iphone,box2d-iphone,Cocos2d Iphone,Box2d Iphone,我正在使用cocos2dv1.0.1和相应的Box2d版本。在模拟器中,一切正常。它只是一个身体,在一个触碰的位置上产生,然后落到地上。但我在设备iphone4上运行了它,物体向上漂浮 为什么会发生这种情况 重力设置为-0.3f。我在场景中有另一个动态身体,它应该出现在地面的底部。它只是漂浮起来的触摸物体。睡眠是真的 我刚刚将sleep设置为false,现在漫游者也漂浮起来。但它不应该浮起来。以下是我的世界创建方法: - (void)setupWorld { b2Vec2 gravity

我正在使用cocos2dv1.0.1和相应的Box2d版本。在模拟器中,一切正常。它只是一个身体,在一个触碰的位置上产生,然后落到地上。但我在设备iphone4上运行了它,物体向上漂浮

为什么会发生这种情况

重力设置为-0.3f。我在场景中有另一个动态身体,它应该出现在地面的底部。它只是漂浮起来的触摸物体。睡眠是真的

我刚刚将sleep设置为false,现在漫游者也漂浮起来。但它不应该浮起来。以下是我的世界创建方法:

- (void)setupWorld {
    b2Vec2 gravity = b2Vec2(0.0f, -0.3f);
    bool doSleep = false;
    world = new b2World(gravity, doSleep);
}
这是我从init创建的身体:

Box2DSprite *roverSprite = [Box2DSprite spriteWithSpriteFrameName:@"rover.png"];
        [self createBoxAtLocation:ccp(100,15) withSize:CGSizeMake(50, 50) forSprite:roverSprite isBox:TRUE];
        [sceneSpriteBatchNode addChild:roverSprite];
下面是createBox方法:

- (void)createBoxAtLocation:(CGPoint)location withSize:(CGSize)size forSprite:(Box2DSprite *)sprite isBox:(BOOL)isBox{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
    b2Body *body = world->CreateBody(&bodyDef);
    //
    body->SetUserData(sprite);
    sprite.body = body;


    b2FixtureDef fixtureDef;

    //
    if (isBox) {
        b2PolygonShape shape;
        shape.SetAsBox(sprite.contentSize.width/3/PTM_RATIO,
                       sprite.contentSize.height/3/PTM_RATIO);
        fixtureDef.shape = &shape;
    } else {
        b2CircleShape shape;
        shape.m_radius = sprite.contentSize.width/2/PTM_RATIO;
        fixtureDef.shape = &shape;
    }

    fixtureDef.density = 1.0;
    fixtureDef.friction = 1.0;
    fixtureDef.restitution = 0.5;
    body->CreateFixture(&fixtureDef);
}
同样的方法也适用于通过以下线创建的触摸创建的对象:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
    //b2Vec2 locationWorld = b2Vec2(touchLocation.x/PTM_RATIO, touchLocation.y/PTM_RATIO);

    Box2DSprite *sprite = [Box2DSprite spriteWithSpriteFrameName:@"koko1.png"];
    [self createBoxAtLocation:touchLocation withSize:CGSizeMake(50, 50) forSprite:sprite isBox:TRUE];
    [sceneSpriteBatchNode addChild:sprite];
    return TRUE;
}

您处于纵向模式还是横向模式?如果是横向的,可能是将设备向左旋转而不是向右旋转。设备方向可以交换您身上加速计的y值。不确定这是否与您的问题有关。

结果是我将目标设置旋转到右侧而不是左侧。

这是加速计方法:-(无效)加速计:(UIAccelerator*)加速计didAccelerate:(UIAcceleration*)加速度{b2Vec2重力(-acceleration.y*15,acceleration.x*15);世界->设置重力(重力);}但我不知道为什么?我关掉了加速计,它的东西又浮了下来。