Question

So I created a little game with SpriteKit. Whenever the Human hits the BlockCategory, he should be able to jump 1 time. With my current code, the Human jumps 1 time after the beginning, and then switching every 0.1s between Contact and Contact lost. So he isn't really able to jump again after hitting the ground again. My Code :

-(void)Mensch{

Human = [SKSpriteNode spriteNodeWithTexture:HumanTexture1];
[Human setScale:1.0];
Human.position = CGPointMake(50 , 225);
[Human runAction:Run];

SKSpriteNode * HumanBody = [SKSpriteNode spriteNodeWithTexture:HumanTexture1];
HumanBody.size = CGSizeMake(30, 50);

Human.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:MenschKörper.size];
Human.physicsBody.dynamic = NO;
Human.physicsBody.allowsRotation = NO;
Human.physicsBody.usesPreciseCollisionDetection = YES;

Human.physicsBody.categoryBitMask = HumanCategory;
Human.physicsBody.collisionBitMask = BlockCategory | StoneCategory;
Human.physicsBody.contactTestBitMask = StoneCategory | BlockCategory;

[self addChild:Human];



-(void)spawnBlocks {


Block1 = [SKSpriteNode spriteNodeWithTexture:BlockTexture1];
[Block1 setScale:1];
Block1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Block1.size];
Block1.physicsBody.dynamic = NO;
Block1.physicsBody.usesPreciseCollisionDetection = YES;
Block1.name = @"Blocks";
[self addChild:Block1];

Block1.physicsBody.categoryBitMask = BlockCategory;
Block1.physicsBody.collisionBitMask = HumanCategory;
Block1.physicsBody.contactTestBitMask = HumanCategory;

// and for the following 3 Blocks the same code




- (void)didBeginContact:(SKPhysicsContact *)contact
{

uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

if (collision == (HumanCategory | StoneCategory))
{
    [self GameOver];
    [self runAction:[SKAction playSoundFileNamed:@"Collision.mp3" waitForCompletion:NO]];
}

if (collision == (HumanCategory | BlockCategory))
{
    PlayerBlockContact = YES;
    NSLog(@"Contact");
}


}

-(void)didEndContact:(SKPhysicsContact *)contact
{

uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask);

if (collision == (HumanCategory | BlockCategory))
{
    PlayerBlockContact = NO;
    NSLog(@"Contact Lost");
}

}



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


if(PlayerBlockContact){
Human.physicsBody.velocity = CGVectorMake(0, 0);
[Human.physicsBody applyImpulse:CGVectorMake(0, 35)];
[self runAction:[SKAction playSoundFileNamed:@"Jump.mp3" waitForCompletion:NO]];
}
Was it helpful?

Solution

The contact and contact lost in rapid succession sounds like an issue with your objects' restitution property. This can sometimes result in an unending series of bounces.

If objects make and lose contact in very quick succession, sometimes SpriteKit loses track of what the right condition is (objects that have contact show as not having contact).

Try setting the restitution on both objects to zero.

(yourObjectName).physicsBody.restitution = 0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top