为啥我的 cocos2d box2d 项目中我的第二个球没有弹跳?

Posted

技术标签:

【中文标题】为啥我的 cocos2d box2d 项目中我的第二个球没有弹跳?【英文标题】:Why isn't my second ball bouncing in my cocos2d box2d project?为什么我的 cocos2d box2d 项目中我的第二个球没有弹跳? 【发布时间】:2013-05-30 15:35:22 【问题描述】:

我打了第二个球,但它从地板上掉了下来。 谁能弄清楚为什么? 我真的找不到其他需要复制或适应第一个球的第二个球的东西。

这是我的代码:

#import "HelloWorldLayer.h"

@implementation HelloWorldLayer

+ (id)scene 

    CCScene *scene = [CCScene node];
    HelloWorldLayer *layer = [HelloWorldLayer node];
    [scene addChild:layer];
    return scene;



- (id)init 



    if ((self=[super init])) 

        CGSize winSize = [CCDirector sharedDirector].winSize;

        // Create sprite and add it to the layer
        _ball = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
        _ball.position = ccp(100, 300);
        [self addChild:_ball];

        _ball2 = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
        _ball2.position = ccp(300, 300);
        [self addChild:_ball2];

        // Create a world
        b2Vec2 gravity = b2Vec2(0.0f, -8.0f);
        _world = new b2World(gravity);


        // Create BALL 1 body and shape
        b2BodyDef ballBodyDef;
        ballBodyDef.type = b2_dynamicBody;
        ballBodyDef.position.Set(100/PTM_RATIO, 300/PTM_RATIO);
        ballBodyDef.userData = _ball;
        _body = _world->CreateBody(&ballBodyDef);

        b2CircleShape circle;
        circle.m_radius = 26.0/PTM_RATIO;

        b2FixtureDef ballShapeDef;
        ballShapeDef.shape = &circle;
        ballShapeDef.density = 2.0f;
        ballShapeDef.friction = 0.1f;
        ballShapeDef.restitution = 0.4f;
        _body->CreateFixture(&ballShapeDef);

        [self schedule:@selector(tick:)];


        // Create BALL 2 body and shape
        b2BodyDef ballBodyDef2;
        ballBodyDef2.type = b2_dynamicBody;
        ballBodyDef2.position.Set(300/PTM_RATIO, 300/PTM_RATIO);
        ballBodyDef2.userData = _ball2;
        _body2 = _world->CreateBody(&ballBodyDef2);

        b2CircleShape circle2;
        circle2.m_radius = 26.0/PTM_RATIO;

        b2FixtureDef ballShapeDef2;
        ballShapeDef2.shape = &circle2;
        ballShapeDef2.density = 2.0f;
        ballShapeDef2.friction = 0.1f;
        ballShapeDef2.restitution = 0.4f;
        _body->CreateFixture(&ballShapeDef2);

        // Create edges around the entire screen
        b2BodyDef groundBodyDef;
        groundBodyDef.position.Set(0,0);

        b2Body *groundBody = _world->CreateBody(&groundBodyDef);
        b2EdgeShape groundEdge;
        b2FixtureDef boxShapeDef;
        boxShapeDef.shape = &groundEdge;

        //WALL DEFINITIONS
        //floor wall
        groundEdge.Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
        groundBody->CreateFixture(&boxShapeDef);
        //roof wall
        groundEdge.Set(b2Vec2(0,0), b2Vec2(0,winSize.height/PTM_RATIO));
        groundBody->CreateFixture(&boxShapeDef);
        //left wall
        groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO),
                       b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
        groundBody->CreateFixture(&boxShapeDef);
        //right wall
        groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO),
                       b2Vec2(winSize.width/PTM_RATIO, 0));
        groundBody->CreateFixture(&boxShapeDef);


         //Do the kick every 5 seconds
         //[self schedule:@selector(kick) interval:5.0];

        [self setTouchEnabled:YES];

    
    return self;



- (void)tick:(ccTime) dt 

    _world->Step(dt, 10, 10);
    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) 
        if (b->GetUserData() != NULL) 
            CCSprite *ballData = (CCSprite *)b->GetUserData();
            ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO);
            ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
        
    




- (void)dealloc 
    delete _world;
    _body = NULL;
    _world = NULL;
    [super dealloc];


@end

【问题讨论】:

【参考方案1】:

改一下

_body->CreateFixture(&ballShapeDef2);

到这里

_body2->CreateFixture(&ballShapeDef2);

【讨论】:

很好,我以前自己做过,在 box2d 上复制/粘贴/重复任务的危险。我讨厌在编程论坛上幼稚,但你的标题真的让我笑了起来。

以上是关于为啥我的 cocos2d box2d 项目中我的第二个球没有弹跳?的主要内容,如果未能解决你的问题,请参考以下文章

单面平台(Box2D/Cocos2D)

cocos2d + box2d:向点旋转

Cocos2D Box2D 错误?

创建身体 - Cocos2d/Box2d

边缘 - Cocos2d/Box2D

克隆一个现有的球体和 box2d cocos2d 中的对象