0

我正在使用 cocos2d v1.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;
}
4

2 回答 2

1

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

于 2012-12-12T12:13:02.367 回答
0

原来我将目标设置旋转到右而不是左。

于 2012-12-12T15:15:28.590 回答