从另一个类访问数组 - cocos2d

时间:2012-06-05 11:56:59

标签: cocos2d-iphone nsmutablearray box2d-iphone

这听起来非常简单。我在一个类(Sprites.mm)中填充了一个带有PhysicsSprites的数组,该类返回自身(我在init方法HelloWorldLayer.mm类中初始化)。如何在HelloWorldLayer.mm的更新方法中访问数组(来自Sprites.mm)?我想在update方法中对sprite进行一些限制。请帮助。

2 个答案:

答案 0 :(得分:1)

首先,您需要在两个类中共享相同的b2World,然后才能访问Sprites.mm世界。

为了更多的理解,我已经创建了一个可行的演示。我正在添加Sprites.mm代码的代码,我称之为Spritese.h& Spritese.mm

以下是Spritese.h代码

    @interface Spritese : CCLayer {
        NSMutableArray *arrSprite;
        b2World* world;
    }
    @property (nonatomic,retain) NSMutableArray *arrSprite;
    -(id)initWithArrayOfSprites : (b2World *)_world;
    @end

.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 up 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 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() > .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 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->CreateBody(&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 = &dynamicBox; 
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body->CreateFixture(&fixtureDef);

    [arrSprite addObject:sprite];
}


@end

在HelloWorld.h文件中导入Class Spritese并添加

@property(nonatomic,retain) Spritese *sprit;

并在.mm文件中合成

现在在HelloWorld的init方法中添加此代码

sprit = [[Spritese alloc] initWithArrayOfSprites:world];
[self addChild:sprit];

最后在tick OR update方法中你需要添加

-(void) tick: (ccTime) dt
{

    int32 velocityIterations = 8;
    int32 positionIterations = 1;
    world->Step(dt, velocityIterations, positionIterations);

    for (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; i < [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");
                }
            }
        }   
    }
}

我希望代码示例能在您身边发挥作用。

答案 1 :(得分:0)

在Sprites.h文件中创建属性

@property (nonatomic, readonly) NSArray* physicSprites;

然后在.mm文件中

@synthesize physicSprites = m_physicSprites;

如果您的数组实例名为m_physicSprites;

然后你就可以像

那样访问它了
[spritesInstance physicSprites];

spriteInstance.physicSprites