Note that there are some explanatory texts on larger screens.

plurals
  1. POclone an existing ball spirte and the object in box2d cocos2d
    primarykey
    data
    text
    <p>So what I want is the following: I have 1 ball that bounces around on my screen, this al goes well + I can kick it in a random direction left or right. I am new to box2d and I want multiple balls in my app. I followed a good tutorial on the net and I pretty much understand what I have made but I am trying for a while now to copy everything from ball 1 including the body en just "2" after it so I have 2 identical balls that bounce around the screen. No succes though.</p> <p>Is there an easy way to simple "clone" a sprite + it's object?</p> <p>This is the code :)</p> <pre><code>#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-&gt;CreateBody(&amp;ballBodyDef); b2CircleShape circle; circle.m_radius = 26.0/PTM_RATIO; b2FixtureDef ballShapeDef; ballShapeDef.shape = &amp;circle; ballShapeDef.density = 1.0f; ballShapeDef.friction = 0.2f; ballShapeDef.restitution = 0.4f; _body-&gt;CreateFixture(&amp;ballShapeDef); [self schedule:@selector(tick:)]; // Create edges around the entire screen b2BodyDef groundBodyDef; groundBodyDef.position.Set(0,0); b2Body *groundBody = _world-&gt;CreateBody(&amp;groundBodyDef); b2EdgeShape groundEdge; b2FixtureDef boxShapeDef; boxShapeDef.shape = &amp;groundEdge; //WALL DEFINITIONS //floor wall groundEdge.Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0)); groundBody-&gt;CreateFixture(&amp;boxShapeDef); //roof wall groundEdge.Set(b2Vec2(0,0), b2Vec2(0,winSize.height/PTM_RATIO)); groundBody-&gt;CreateFixture(&amp;boxShapeDef); //left wall groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO)); groundBody-&gt;CreateFixture(&amp;boxShapeDef); //right wall groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0)); groundBody-&gt;CreateFixture(&amp;boxShapeDef); //Do the kick every 5 seconds //[self schedule:@selector(kick) interval:5.0]; [self setTouchEnabled:YES]; } return self; } - (void)tick:(ccTime) dt { _world-&gt;Step(dt, 10, 10); for(b2Body *b = _world-&gt;GetBodyList(); b; b=b-&gt;GetNext()) { if (b-&gt;GetUserData() != NULL) { CCSprite *ballData = (CCSprite *)b-&gt;GetUserData(); ballData.position = ccp(b-&gt;GetPosition().x * PTM_RATIO, b-&gt;GetPosition().y * PTM_RATIO); ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b-&gt;GetAngle()); } } } //do the kick every 5 seconds /*- (void)kick { b2Vec2 force = b2Vec2(30, 30); _body-&gt;ApplyLinearImpulse(force,_body-&gt;GetPosition()); }*/ - (void)ccTouchesBegan:(UITouch *)touch withEvent:(UIEvent *)event { randomGetal = ((arc4random() % 20) - 10); b2Vec2 force = b2Vec2(randomGetal, 10); _body-&gt;ApplyLinearImpulse(force, _body-&gt;GetPosition()); } - (void)dealloc { delete _world; _body = NULL; _world = NULL; [super dealloc]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload