Question

//in Class A.h -> CODE:

#import "B.h";
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import "LevelHelperLoader.h"
#import "ContactListener.h"
#import "KKGameKitHelper.h"
#import "LevelHelper.h"

@interface A : CCLayer 
{
b2World* world;
GLESDebugDraw* debugDraw;
LevelHelperLoader * lh;

B * b;  //another class
}

+(id) scene;
@end

In Class A.m -> CODE:

@implementation A

+(id) scene
{
CCScene *scene = [CCScene node];
A *layer = [A node];
[scene addChild: layer];

//add another layer
  B * anotherLayer=[B node];
  [scene addChild:anotherLayer];
  layer.b=anotherLayer;
  [layer.b createBullets];

return scene;
}

-(id) init
{
   if ((self = [super init]))
   {
            self.isTouchEnabled = YES;
             glClearColor(0.1f, 0.0f, 0.2f, 1.0f);

      // Construct a world object, which will hold and simulate the rigid bodies.
      b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
      world = new b2World(gravity);
      world->SetAllowSleeping(YES);
      //world->SetContinuousPhysics(YES);

           NSString* currentLevel = [[LevelManager sharedInstance] activeLevel];
         lh = [[LevelHelperLoader alloc] initWithContentOfFile:currentLevel];

        //creating the objects
        [lh addObjectsToWorld:world cocos2dLayer:self];

        if([lh hasPhysicBoundaries])
            [lh createPhysicBoundaries:world];

        if(![lh isGravityZero])
            [lh createGravity:world];

      [self scheduleUpdate];
   }
  return self;
}

-(LevelHelperLoader *) getLh
{
return lh;
}

//in B.h CODE:

@interface B : CCLayer
{
    LHSprite * sprite1;
    b2Body *body;
}

//in B.m CODE:

#import "B.h"
#import "A.h"

@implementation B

-(id) init
{
   if ((self=[super init]) ) 
   {
        CCLOG(@" B init...");
   }
return self;
}

-(void ) createBullets 
{
    A * mainClass=[[A alloc]init];
    sprite1=  [[mainClass getLh] createPhysicalSpriteWithUniqueName:@"img"];
    body=[sprite1 body];
    [self addChild:sprite1];
    [sprite1 transformPosition:ccp(150,250)];
}

@end

By doing, all this stuff i am able to add LHSprite from another class (i.e class B) but not it's body. What Changes should i make,to add the body for LHSprite i added?

Kind Regards, Stack.

Was it helpful?

Solution

Got the solution. Just, need to override Node method in class A,as follows:

-(id) node
{
  return [A sharedInstance];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top