nsmutablearray - accessing array from one class in another - cocos2d -


this might sound pretty straightforward. have populated array physicssprites in class (sprites.mm) returns (which initialize in init method helloworldlayer.mm class). how can access array (from sprites.mm) in update method of helloworldlayer.mm? want put restrictions on sprites in update method. please help.

first of need share same b2world in both classes , can access sprites.mm world.

for more understanding i've created demo work. adding code of sprites.mm code called spritese.h & spritese.mm

the below spritese.h code

    @interface spritese : cclayer {         nsmutablearray *arrsprite;         b2world* world;     }     @property (nonatomic,retain) nsmutablearray *arrsprite;     -(id)initwitharrayofsprites : (b2world *)_world;     @end 

the below 1 .mm

    @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 sprite      ccspritebatchnode *batch = [ccspritebatchnode batchnodewithfile:@"blocks.png" capacity:150];     [self addchild:batch z:0 tag:ktagbatchnode];      for(int i=0; i<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 64x64 sprite sheet 4 different 32x32 images.  following code     //just randomly picking 1 of images     int idx = (ccrandom_0_1() > .5 ? 0:1);     int idy = (ccrandom_0_1() > .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 dynamic body.     //set 1m squared box in 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->createbody(&bodydef);      // define box shape our dynamic body.     b2polygonshape dynamicbox;     dynamicbox.setasbox(.5f, .5f);//these mid points our 1m box      // define dynamic body fixture.     b2fixturedef fixturedef;     fixturedef.shape = &dynamicbox;      fixturedef.density = 1.0f;     fixturedef.friction = 0.3f;     body->createfixture(&fixturedef);      [arrsprite addobject:sprite]; }   @end 

in helloworld.h file import class spritese , add

@property(nonatomic,retain) spritese *sprit; 

and synthesize in .mm file

now in init method of helloworld add code

sprit = [[spritese alloc] initwitharrayofsprites:world]; [self addchild:sprit]; 

and in tick or update method need add

-(void) tick: (cctime) dt {      int32 velocityiterations = 8;     int32 positioniterations = 1;     world->step(dt, velocityiterations, positioniterations);      (b2body* b = world->getbodylist(); b; b = b->getnext())     {         if (b->getuserdata() != null) {             ccsprite *myactor = (ccsprite*)b->getuserdata();              myactor.position = cgpointmake( b->getposition().x * ptm_ratio, b->getposition().y * ptm_ratio);             myactor.rotation = -1 * cc_radians_to_degrees(b->getangle());              spritese *s = (spritese *)b->getuserdata();             for(int i=0; < [sprit.arrsprite count]; i++){                 if(s == [sprit.arrsprite objectatindex:i]){                     s.position = cgpointmake( b->getposition().x * ptm_ratio, b->getposition().y * ptm_ratio);                     s.rotation = -1 * cc_radians_to_degrees(b->getangle());                     nslog(@"process sprite here");                 }             }         }        } } 

i hope code example work @ side.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -