1

//在类 Ah -> 代码中:

#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

在 Am 类 -> 代码中:

@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;
}

//在 Bh 代码中:

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

//在 Bm 代码中:

#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

通过这样做,所有这些东西我都可以从另一个类(即 B 类)添加 LHSprite,但不是它的主体。我应该做些什么改变来添加我添加的 LHSprite 的主体?

亲切的问候,堆栈。

4

1 回答 1

0

得到了解决方案。只是,需要重写A类中的Node方法,如下:

-(id) node
{
  return [A sharedInstance];
}
于 2012-08-03T05:53:18.963 回答