Sprite set does not find nodeAtPoint after moving parent SKNode

Hey.

I have such a strange problem in the Sprite Kit. I use nodeAtPointand categoryBitMaskto determine whether the concerns touchdown player in the call transfer method.

Everything works as it should. But then - to reveal some additional buttons in the box - when I move the parent node with SKAction moveTo:CGPoint(I have both the ground and the player as children SKNode), the player does not jump. I am an NSLog pointBelowPlayerand it is the same as before, but blockNode.physicsBodyit is null! Could this be a mistake in the Sprite set, or did I miss something basic about inheritance and position?

Jump Method:

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

Node parent move method:

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

Creating sprites:

-(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;
}
+4
2

, ... Node, , , null, node Node.

, , :

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, , .

+5

Node node ( , ?).
node.name, , , node, , equalToString:.

+1

Source: https://habr.com/ru/post/1539594/


All Articles