SpriteKit击中地面时跳跃

时间:2014-04-13 14:53:49

标签: sprite-kit intersection touchesbegan

所以我得到了2个SKSpriteNodes,一个人类和一个块。如果人类击中顶部的块,它应该能够跳跃,如果他在空中,他不应该。我正在使用intersectsNode,但是人类只在块的末尾跳转,我不知道为什么。 论坛成员告诉我使用bodyAtPoint,但如果我使用它只显示: “SKSpriteNode没有可见的@interface声明选择器bodyAtPoint”。

我当前的代码有intersectsNode:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

Human.physicsBody.velocity = CGVectorMake(0, 0);
if([Human intersectsNode:Block1]){
[Human.physicsBody applyImpulse:CGVectorMake(0, 40)];
}

1 个答案:

答案 0 :(得分:0)

我建议您使用didBeginContact:代替intersectsNode。在MyScene中,您可以这样设置:

@implementation MyScene
{
    BOOL playerBlockContact
}

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

    if (collision == (CNPhysicsCategoryPlayer | CNPhysicsCategoryBlock))
    {
        playerBlockContact = true;
        NSLog(@"Contact");
    }
}

- (void)didEndContact:(SKPhysicsContact *)contact
{
    uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);
    if (collision == (CNPhysicsCategoryPlayer | CNPhysicsCategoryBlock))
    {
        playerBlockContact = false;
        NSLog(@"Contact lost");
    }
}

然后将if([Human intersectsNode:Block1])替换为If(playerBlockContact)