2012-08-02 87 views
1

//在类阿 - > CODE:通信CClayers之间2

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

//在了Bh CODE:

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

//在家蚕 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 

通过这样做,所有这些东西我能够从另一个类(即B类)添加LHSprite,但不是它的正文。 什么样的变化,我应该做,以增加对LHSprite我增加了身体?

亲切的问候, 堆栈。

+0

我也遇到了同样的问题..!你有没有发现任何东西..? – Shailesh 2012-08-02 13:01:41

回答

0

得到了解决。 只是,需要覆盖A类中的Node方法,如下所示:

-(id) node 
{ 
    return [A sharedInstance]; 
}