移动父SKNode后,Sprite工具包找不到nodeAtPoint

时间:2014-05-07 14:54:22

标签: ios objective-c collision-detection sprite-kit

嗨。

我在Sprite Kit中遇到了这个奇怪的问题。我正在使用nodeAtPointcategoryBitMask来检测玩家在调用跳转方法时是否正在接触地面。

一切都在发挥作用。但接下来 - 为了在抽屉中显示一些可选按钮 - 当我用SKAction moveTo:CGPoint移动父节点时(我有地面和玩家作为SKNode的子节点),玩家不会跳。我NSLog pointBelowPlayer,它和以前一样,但blockNode.physicsBody为空!这可能是Sprite Kit中的一个错误,还是我遗漏了一些关于继承和位置的基本知识?

跳跃的方法:

-(void)playerJump {

    // Player jump if on ground
    CGPoint pointBelowPlayer = CGPointMake(_player.position.x, _player.position.y-_player.size.height);
    SKNode *blockNode = [self nodeAtPoint:pointBelowPlayer];

    if (blockNode.physicsBody.categoryBitMask == groundCategory){
        [_player.physicsBody applyImpulse:CGVectorMake(0, 120.0f)];
    }
}

移动父节点的方法:

-(void)toggleDrawer {
    if (bDrawerVisible) {
        [_actionLayer runAction:[SKAction moveTo:CGPointMake(0, 0) duration:1]];
        bDrawerVisible = false;
    } else {
        [_actionLayer runAction:[SKAction moveTo:CGPointMake(0, 200) duration:1]];
        bDrawerVisible = true;
    }
}

创建SpriteNodes:

-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {

        _actionLayer = [SKNode new];

        _player = [self createPlayer];
        [_actionLayer addChild:_player];

        _ground = [self createGround];
        [_actionLayer addChild:_ground];
}

-(SKSpriteNode *)createPlayer {

    SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"redSquare.png"];
    player.name = @"player";
    player.scale = 0.5;
    player.position = CGPointMake(screenWidth/3, player.size.height*2);
    player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
    player.physicsBody.categoryBitMask = playerCategory;
    player.physicsBody.collisionBitMask = groundCategory;
    player.physicsBody.contactTestBitMask = noteCategory;

    return player;
}

-(SKSpriteNode *)createGround {

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithImageNamed:@"ground.png"];
    ground.name = @"ground";
    ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
    ground.physicsBody.dynamic = NO;
    ground.physicsBody.categoryBitMask = groundCategory;
    ground.physicsBody.collisionBitMask = playerCategory;
    ground.xScale = screenWidth/ground.size.width;;
    ground.position = CGPointMake(self.size.width/2, 0);

    return ground;
}

2 个答案:

答案 0 :(得分:5)

好的,我解决了这个问题...... 如果您有多个节点,则在您点击的位置上,如果最深的节点没有名称或最深的节点的名称,您将获得null。

所以使用它来遍历所有节点:

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
NSArray *buttonNodes = [self nodesAtPoint:location];
for (SKNode *node in buttonNodes)
{
    NSLog(@"%@", node.name);
}

另外,您可以使用NSLog(@"%f", node.zPosition);来获取zPosition以查看其中一个是最重要的。

答案 1 :(得分:1)

点上的节点可能是另一个节点(也许是你的背景?) 使用node.name来命名节点,您可以通过比较名称和equalToString:method来检查节点是否相同。