Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all you need to share the same b2World in both the classes and then you can access the Sprites.mm world.</p> <p>For more understanding I've created a demo which would work. I am adding the code of the Sprites.mm code which I called as Spritese.h &amp; Spritese.mm</p> <p>The below is the Spritese.h code </p> <pre><code> @interface Spritese : CCLayer { NSMutableArray *arrSprite; b2World* world; } @property (nonatomic,retain) NSMutableArray *arrSprite; -(id)initWithArrayOfSprites : (b2World *)_world; @end </code></pre> <p>The Below one for the .mm</p> <pre><code> @implementation Spritese @synthesize arrSprite; #define PTM_RATIO 32 #define kTagBatchNode 1 -(id)initWithArrayOfSprites :(b2World *)_world{ if((self = [super init])){ CGSize screenSize = [CCDirector sharedDirector].winSize; world = _world; //Set up sprite CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"blocks.png" capacity:150]; [self addChild:batch z:0 tag:kTagBatchNode]; for(int i=0; i&lt;3; i++)//creating 3 objects [self addNewSpriteWithCoords:ccp(screenSize.width/2, screenSize.height/2)]; } return self; } -(void) addNewSpriteWithCoords:(CGPoint)p { CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y); CCSpriteBatchNode *batch = (CCSpriteBatchNode*) [self getChildByTag:kTagBatchNode]; //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is //just randomly picking one of the images int idx = (CCRANDOM_0_1() &gt; .5 ? 0:1); int idy = (CCRANDOM_0_1() &gt; .5 ? 0:1); CCSprite *sprite = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(32 * idx,32 * idy,32,32)]; [batch addChild:sprite]; sprite.position = ccp( p.x, p.y); // Define the dynamic body. //Set up a 1m squared box in the physics world b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO); bodyDef.userData = sprite; b2Body *body = world-&gt;CreateBody(&amp;bodyDef); // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &amp;dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body-&gt;CreateFixture(&amp;fixtureDef); [arrSprite addObject:sprite]; } @end </code></pre> <p>In the HelloWorld.h file import the Class Spritese and add</p> <pre><code>@property(nonatomic,retain) Spritese *sprit; </code></pre> <p>and synthesize it in .mm file</p> <p>Now in the init method of the HelloWorld add this code</p> <pre><code>sprit = [[Spritese alloc] initWithArrayOfSprites:world]; [self addChild:sprit]; </code></pre> <p>And finally in the tick OR update method you need to add </p> <pre><code>-(void) tick: (ccTime) dt { int32 velocityIterations = 8; int32 positionIterations = 1; world-&gt;Step(dt, velocityIterations, positionIterations); for (b2Body* b = world-&gt;GetBodyList(); b; b = b-&gt;GetNext()) { if (b-&gt;GetUserData() != NULL) { CCSprite *myActor = (CCSprite*)b-&gt;GetUserData(); myActor.position = CGPointMake( b-&gt;GetPosition().x * PTM_RATIO, b-&gt;GetPosition().y * PTM_RATIO); myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b-&gt;GetAngle()); Spritese *s = (Spritese *)b-&gt;GetUserData(); for(int i=0; i &lt; [sprit.arrSprite count]; i++){ if(s == [sprit.arrSprite objectAtIndex:i]){ s.position = CGPointMake( b-&gt;GetPosition().x * PTM_RATIO, b-&gt;GetPosition().y * PTM_RATIO); s.rotation = -1 * CC_RADIANS_TO_DEGREES(b-&gt;GetAngle()); NSLog(@"Process Sprite Here"); } } } } } </code></pre> <p>I hope the code example would work at your side.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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