Question

I thought I understand the concepts of bitmasks, CategoryBitMasks and CollisionBitMasks, but it turns out I am not ;-( but I dont understand why. I set the bit Masks for collision detection, I add the category Bit mask to the frame, and I add the categoryBitMask to my object (in this case a taxi). But the taxi just drops down the screen :-/

Any ideas why this is like that?

#import "MyScene.h"
#import "SKSpriteNode+DebugDraw.h"

// Define Bit Masks for Collision Detection
typedef NS_OPTIONS(uint32_t, CNPhysicsCategory) {
    CNPhysicsCategoryEdge = 1 <<0,
    CNPhysicsCategoryTaxi = 1 <<1,

};

@interface MyScene() <SKPhysicsContactDelegate>
@end

@implementation MyScene{
    SKNode *_gameNode;
    SKSpriteNode *_taxiNode;
}

-(instancetype)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        [self initializeScene];
    }
    return self;
}

-(void)initializeScene{

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    self.physicsWorld.contactDelegate = self;
    self.physicsBody.categoryBitMask = CNPhysicsCategoryEdge;

    SKSpriteNode* bg = [SKSpriteNode spriteNodeWithImageNamed:@"background.png"];
    bg.position = CGPointMake(self.size.width/2, self.size.height/2);
    [self addChild: bg];

    [self addTaxi];

    _gameNode = [SKNode node];
    [self addChild:_gameNode];
}

-(void)addTaxi{
    _taxiNode = [SKSpriteNode spriteNodeWithImageNamed:@"taxi.png"];
    _taxiNode.position = CGPointMake(self.size.width/2, self.size.height/2);

    [self addChild:_taxiNode];

    CGSize contactSize = CGSizeMake(_taxiNode.size.width, _taxiNode.size.height);
    _taxiNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: contactSize];
    [_taxiNode attachDebugRectWithSize:contactSize];

    _taxiNode.physicsBody.categoryBitMask = CNPhysicsCategoryTaxi;

}
Was it helpful?

Solution

I finally managed it - the problem was not the collision detection, but it was the layout of the phone, since I did not replace the viewDidLoad method in the ViewController with viewWillLayoutSubviews method. Now everything works fine.

The significant post was the following: bodyWithEdgeLoopFromRect not working in landscape

Thanks for your support!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top