0

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

4

2 回答 2

1

First of all you need to share the same b2World in both the classes and then you can access the Sprites.mm world.

For more understanding I've created a demo which would work. I am adding the code of the Sprites.mm code which I called as Spritese.h & Spritese.mm

The below is the Spritese.h code

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

The Below one for the .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

In the HelloWorld.h file import the Class Spritese and add

@property(nonatomic,retain) Spritese *sprit;

and synthesize it in .mm file

Now in the init method of the HelloWorld add this code

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

And finally in the tick OR update method you need to add

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

I hope the code example would work at your side.

于 2012-06-05T12:55:44.350 回答
0

在 Sprites.h 文件中创建属性

@property (nonatomic, readonly) NSArray* physicSprites;

然后在你的 .mm 文件中

@synthesize physicSprites = m_physicSprites;

如果您的数组实例被称为 m_physicSprites;

那么你就可以像

[spritesInstance physicSprites];

或者

spriteInstance.physicSprites
于 2012-06-05T12:21:52.960 回答