tile节点的NSMutablearray在另一个类中调用它时返回null

时间:2013-11-26 13:56:17

标签: objective-c nsmutablearray

我有一个tmxTileMapLayer类,它有这个方法来制作tile节点。

在tmxTileMapLayer.h中我有一个这样的数组。

@property (nonatomic,strong)NSMutableArray *tileArray;

在tmxTileMapLayer.m

-(void)createNodesFromLayer:(TMXLayer *)layer
{
JSTileMap *map = layer.map;
//1
for (int w = 0 ; w < self.gridSize.width; ++w) {
    for(int h = 0; h < self.gridSize.height; ++h) {

        CGPoint coord = CGPointMake(w, h);
        NSInteger tileGid =
        [layer.layerInfo tileGidAtCoord:coord];
        if(!tileGid)
            continue;
if ([map propertiesForGid:tileGid] [@"grapple"]) {
            grapple = [SKSpriteNode node];
            grapple = [layer tileAtCoord:coord];
            grapple.position = CGPointMake(grapple.position.x, grapple.position.y);
            grapple.physicsBody.dynamic = NO;
            grapple.physicsBody.restitution = 0.0;

            [_tileArray addObject:grapple];

            NSLog(@"%@",_tileArray);
        }

如果我在这里Nslog我得到了瓷砖的位置。

现在我的主要课程名为MyScene

  -(id)initWithSize:(CGSize)size {    
   if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    self.scene.view.multipleTouchEnabled = YES;

    [self createWorld];

    tileArrayCopy = [[TmxTileMapLayer alloc] init];
    NSMutableArray *newArray = [tileArrayCopy.tileArray mutableCopy];

    [_worldNode addChild:tileArrayCopy];

    NSLog(@"%@",newArray);

       }
return self;
}

如果我现在使用newArray的NSLong,我会得到Null。 :/

这是创建世界方法。

 - (TileMapLayer *)createScenery
 {
_tileMap = [JSTileMap mapNamed:@"level.tmx"];
return [[TmxTileMapLayer alloc]
        initWithTmxLayer:[_tileMap layerNamed:@"circle"]];
 }

 - (TileMapLayer *)grappleStuff
 {
return [[TmxTileMapLayer alloc]
        initWithTmxLayer:[_tileMap layerNamed:@"grapple"]];
 }


- (void)createWorld {
_bgLayer = [self createScenery];
_fgLayer = [self bouncDynamic];

_worldNode = [SKNode node];
[self addChild:_worldNode];

if (_tileMap) {
    [_worldNode addChild:_tileMap];
}


[_worldNode addChild:_bgLayer];
[_worldNode addChild:_fgLayer];

SKNode *bounds = [SKNode node];
bounds.physicsBody =
[SKPhysicsBody bodyWithEdgeLoopFromRect:
 CGRectMake(0, 0,

            _bgLayer.layerSize.width,
            _bgLayer.layerSize.height)];
bounds.physicsBody.categoryBitMask = PCBoundaryCategory;
bounds.physicsBody.contactTestBitMask = PCPlayerCategory | PCDestruction;
[_worldNode addChild:bounds];

_bgLayer.zPosition = 1;
_tileMap.zPosition = 1;

[self grappleStuff];
[self hazardStuff];
[self destructionStuff];


}

谢谢你的帮助!!我被困住了!

1 个答案:

答案 0 :(得分:0)

您应该在init方法TmxTileMapLayer类中初始化Mutable Array,或者您可以执行此操作。

   tileArrayCopy = [[TmxTileMapLayer alloc] init];
   tileArrayCopy.tileArray = [[NSMutableArray alloc] init];
   NSMutableArray *newArray = tileArrayCopy.tileArray;

我认为当你这样做时会发生什么

NSMutableArray *newArray = [tileArrayCopy.tileArray mutableCopy];

tileArray为零,当你调用mutableCopy时,猜测会发生什么(将消息传递给nil以及objective-c的作用是什么?)。