2013-05-30 55 views
0

所以我想要的是以下內容:我有一個球在我的屏幕上彈跳,這個球順利運行+我可以隨意向左或向右踢球。 我是新來的box2d,我想在我的應用程序多個球。 我在網上跟着一個很好的教程,我非常理解我做了什麼,但我現在試圖複製一切從球1包括身體只是「2」後,所以我有2個相同的球彈跳在屏幕周圍。 雖然沒有成功。克隆一個現有的球spirte和box2d中的對象cocos2d

有沒有簡單的方法來簡單地「克隆」一個精靈+它的對象?

這是代碼:)

#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]; 

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

     // Create ball 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 = 1.0f; 
     ballShapeDef.friction = 0.2f; 
     ballShapeDef.restitution = 0.4f; 
     _body->CreateFixture(&ballShapeDef); 

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

     // 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()); 
     } 
    } 

} 

//do the kick every 5 seconds 
/*- (void)kick { 
    b2Vec2 force = b2Vec2(30, 30); 
    _body->ApplyLinearImpulse(force,_body->GetPosition()); 
}*/ 

- (void)ccTouchesBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    randomGetal = ((arc4random() % 20) - 10); 
    b2Vec2 force = b2Vec2(randomGetal, 10); 
    _body->ApplyLinearImpulse(force, _body->GetPosition()); 
} 

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

@end 
+0

難道你不能只使用相同的代碼更多的球? – iforce2d

回答

1

在代碼中,您創建了一個單刀球。我認爲你想要做的是創建多個球,所有這些球在物理模擬中都有單獨的b2Body。

有幾種方法可以做到這一點,但我現在要堅持使用「蠻力方法」。

創建一個靜態函數是這樣的:

static b2Body* CreateBall(b2World* world, CCNode* node, CCPoint position) 
{ 
    // Create sprite and add it to the layer 
    CCSprite* ballSprite = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)]; 
    ballSprite.position = ccp(position.x, position.y); 
    [node addChild:ballSprite]; 

    // Create ball body and shape 
    b2BodyDef ballBodyDef; 
    ballBodyDef.type = b2_dynamicBody; 
    ballBodyDef.position.Set(position.x/PTM_RATIO, position.y/PTM_RATIO); 
    ballBodyDef.userData = ballSprite; 
    b2Body* body = world->CreateBody(&ballBodyDef); 

    b2CircleShape circle; 
    circle.m_radius = 26.0/PTM_RATIO; 

    b2FixtureDef ballShapeDef; 
    ballShapeDef.shape = &circle; 
    ballShapeDef.density = 1.0f; 
    ballShapeDef.friction = 0.2f; 
    ballShapeDef.restitution = 0.4f; 
    body->CreateFixture(&ballShapeDef); 

    return body; 

} 

注:我在輸入這個我沒編譯它。它可能需要一點調整。

每當你想創造一個新球並在世界傳球並且你想讓球進入你的位置時就調用它。

這是你所需要的?

我在我的網站http://www.NonlinearIdeas.com上使用Box2D和使用圖形等更大的示例(和代碼)。