Pergunta

I have a node which is placed under my screen and works as a platform. As soon as another node touches the node a boolean is set to NO. When I define my node with the SKPhysicsBody properties for the collision the node ignores the affectedByGravity property.

My code:

+ (void)addNewNodeTo:(SKNode *)parentNode
{
    //Correct image size
    SKSpriteNode *desertBottom = [SKSpriteNode node];
    desertBottom = [[SKSpriteNode alloc] initWithImageNamed:@"Giraffe.png"];

    desertBottom.position = CGPointMake(0, -200);
    desertBottom.zPosition = 2;


    desertBottom.physicsBody.collisionBitMask = lionType;
    desertBottom.physicsBody.categoryBitMask = terrainType;
    desertBottom.physicsBody.contactTestBitMask = lionType;


    desertBottom.zPosition = 2;
    desertBottom.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(desertBottom.size.width, desertBottom.size.height)];
    desertBottom.physicsBody.dynamic = YES;
    desertBottom.physicsBody.affectedByGravity = NO;


    [parentNode addChild:desertBottom];
}

My collision methods:

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

    if (collision == (lionType | terrainType)) {
        self.lionNode.lionIsJumping = NO;
        NSLog(@"%i", self.lionNode.lionIsJumping);
    }
}

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

    if (collision == (lionType | terrainType)) {
        self.lionNode.lionIsJumping = YES;
        NSLog(@"%i", self.lionNode.lionIsJumping);
    }
}
Foi útil?

Solução 3

I have created a new class with subclass of SKSpriteNode for the bottom and added it to my GameScene.

Outras dicas

A physics body has a velocity. Gravity changes velocity over time. If the body is already traveling at a certain velocity due to gravity, and you disable gravity, it will continue to move according to its current velocity but no longer gain additional speed from gravity.

My guess is that you expect the body to stop when disabling gravity. If that's what you want you can do this manually by setting the y component of velocity to zero:

SKPhysicsBody* body = desertBottom.physicsBody;
body.velocity = CGVectorMake(body.velocity.x, 0.0);

If the platform should not move after a collision you have to set the dynamic property to false.

Maybe you have an issue with you bit masks?

Have you tried to log outside your if statement of the didBeginContact method?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top