ClassA childNodeWithName:@" name"不在ClassB内部工作

时间:2015-06-30 18:17:04

标签: ios objective-c sprite-kit

我正试图在我的RPG地图中创建一个无形路径的战略性SKSpriteNodes,让我的NPC步行往返(使地图看起来更加生动)。然后我想在我的NPC类中创建一个循环方法,将它从这些不可见的SKSpriteNode中的一个移到另一个。除了NPC的实际运动外,一切都很好。更准确地说,我无法从我的NPC类(SKSpriteNode)中访问我的SKScene的不可见节点的位置。我创建了几个隐形路径节点,以下是我在SKScene中完成的方法:

TMXObjectGroup *group = [_tileMap groupNamed:@"WalkingPath"];

NSDictionary *s1node1 = [group objectNamed:@"street1Node1"];
if (s1node1)
{
    CGFloat x = [s1node1[@"x"] floatValue];
    CGFloat y = [s1node1[@"y"] floatValue];
    CGFloat w = [s1node1[@"width"] floatValue];
    CGFloat h = [s1node1[@"height"] floatValue];

    SKSpriteNode *object = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(w, h)];
    object.name = @"strt1N1";
    object.position = CGPointMake(x + w/2, y + h/2);

    object.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(w, h)];

    object.physicsBody.categoryBitMask       = terrainCategory;
    object.physicsBody.contactTestBitMask    = playerCategory | npcCategory;
    object.physicsBody.collisionBitMask      = 0;
    object.physicsBody.dynamic = NO;
    object.physicsBody.friction = 0;
    object.hidden = YES;

    [_worldNode addChild:object];
}

一旦上述所有代码编译并且应用程序运行,一切都按原样创建。这很好。但是,当我尝试从我的NPC类中访问这个确切的节点时,没有任何反应。我使用IF语句来检查这样的节点是否存在。控制台说它没有... vwaAt ??这是NPC类中的移动方法,试图访问SKScene的childNode:

-(void)beginMovement
{
    //_oTC is a @property (@property OTC *oTC;).
    //_oTC = (OTridentCity*)self.scene; //<---DOESN'T DO ANYTHING.
    //_oTC = [[OTridentCity alloc] init]; //<---DESTROYS THE APP :D
    //[_oTC childNodeWithName:@"strt1N1"]; //<---Lazy reference.

    if ([_oTC childNodeWithName:@"strt1N1"]) //<---VY NO WERK????
    {
        NSLog(@"EXISTS!"); //<---VANT DIS!!!!!!
    }
    else
    {
        NSLog(@"DOESN'T EXIST"); //<---POPS OUT EVERY TIME.
    }


    CGPoint aPoint      = CGPointMake([_oTC childNodeWithName:@"strt1N1"].position.x, [_oTC childNodeWithName:@"strt1N1"].position.y);
    SKAction *move      = [SKAction moveTo:CGPointMake(aPoint.x, aPoint.y) duration:3.0];
    SKAction *wait      = [SKAction waitForDuration:4.0 withRange:4.0];


    //NPC does move, but it seems towards coordinates (0, 0).
    [self runAction:[SKAction sequence:@[move, wait]] completion:^{ /*[self beginMovement];*/ NSLog(@"Moving"); }];
}

我是否忘记了在SKSpriteNode类中从SKScene访问childNodes的事情?

0 个答案:

没有答案