2014-09-03 85 views
1

didBeginContact不會因某種原因而被調用。我怎樣才能解決這個問題?謝謝!我設置了類別位掩碼和skphysicsContactDelegate,但它仍然沒有註冊聯繫人。我一直堅持這一段時間。spritekit didBeginContact不叫

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

typedef NS_OPTIONS(uint32_t, CollisionCategory) { 
    CollisionCategoryPlayer  = 1 << 0, 
    CollisionCategoryFuel  = 1 << 1, 
}; 

@interface MyScene() <SKPhysicsContactDelegate> 

@end 

@implementation MyScene 

{ 
    SKNode *_playerNode; 
    SKNode *_backgroundNode; 
    SKNode *_foreGround; 
} 

-(id)initWithSize:(CGSize)size 
{ 
    if (self = [super initWithSize:size]) { 

     self.physicsWorld.contactDelegate = self; 

     _backgroundNode = [self createBackground]; 
     [self addChild:_backgroundNode]; 

     _foreGround = [SKNode node]; 
     [self addChild:_foreGround]; 

     //add a fuelNode 
     FuelNode *fuel = [self createFuelAtPosition:CGPointMake(160, 440)]; 
     [_foreGround addChild:fuel]; 

     _playerNode = [self createPlayer]; 
     [_foreGround addChild:_playerNode]; 

     SKAction *actionMove = [SKAction moveToY:-100 duration:3.0]; 
     [fuel runAction:actionMove]; 

     NSLog(@"yea"); 
    } 

    return self; 
} 

-(SKNode *)createPlayer 
{ 
    CGSize playerPhysicsBody; 
    //Create player 
    SKNode *playerNode = [SKNode node]; 
    SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"wship-3.png"]; 
    player.position = CGPointMake(self.size.width/2, 120); 

    [playerNode addChild:player]; 

    //Add physics 
    playerPhysicsBody = CGSizeMake(player.size.width/2, player.size.height/2); 
    playerNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:playerPhysicsBody]; 
    playerNode.physicsBody.dynamic = NO; 

    //Setup collision settings 
    playerNode.physicsBody.usesPreciseCollisionDetection = YES; 
    playerNode.physicsBody.categoryBitMask = CollisionCategoryPlayer; 
    playerNode.physicsBody.collisionBitMask = 0; 
    playerNode.physicsBody.contactTestBitMask = CollisionCategoryFuel; 

    [player attachDebugRectWithSize:playerPhysicsBody]; 

    return playerNode; 
} 

-(SKNode *)createBackground 
{ 
    //Create background 
    SKNode *bgNode = [SKNode node]; 
    SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:@"purple"]; 
    bg.anchorPoint = CGPointZero; 

    [bgNode addChild:bg]; 
    return bgNode; 
} 

- (FuelNode *)createFuelAtPosition:(CGPoint)position 
{ 
    // 1 
    FuelNode *node = [FuelNode node]; 
    [node setPosition:position]; 
    [node setName:@"NODE_FUEL"]; 

    // 2 
    SKSpriteNode *sprite; 
    sprite = [SKSpriteNode spriteNodeWithImageNamed:@"fuelBlue"]; 
    [node addChild:sprite]; 

    // 3 
    CGSize contactSize = CGSizeMake(sprite.size.width/2, sprite.size.height/2); 
    node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:contactSize]; 

    // 4 
    node.physicsBody.dynamic = NO; 

    //Setup collision settings 
    node.physicsBody.categoryBitMask = CollisionCategoryFuel; 
    node.physicsBody.collisionBitMask = 0; 
    //node.physicsBody.contactTestBitMask = CollisionCategoryPlayer; 

    [sprite attachDebugRectWithSize:contactSize]; 

    //SKAction *actionMove = [SKAction moveToY:-100 duration:3.0]; 
    //[node runAction:actionMove]; 

    return node; 
} 

-(void)didBeginContact:(SKPhysicsContact *)contact 
{ 
    BOOL fuelCollision = NO; 

    SKNode *other = (contact.bodyA.node != _playerNode) ? contact.bodyA.node : contact.bodyB.node; 

    NSLog(@"collision"); 

    fuelCollision = [(GameObjectNode *)other collisionWithPlayer:_playerNode]; 

} 


@end 
+0

固定!在創建玩家的方法中,我錯誤地設置了玩家精靈的位置,而不是設置玩家節點的位置! – mistakeNot 2014-09-06 00:14:22

回答

0

固定!在創建玩家的方法中,我錯誤地設置了玩家精靈的位置,而不是設置玩家節點的位置!

0

爲了使接觸檢測,你需要設置

node.physicsBody.dynamic = YES; 

看一看在documentation爲好。

+0

其實我事先試過,這不能解決問題。 – mistakeNot 2014-09-04 20:27:32

+0

你仍然需要physicsBody是動態的。嘗試設置燃料節點的contactTestBitMask以及... – ZeMoon 2014-09-05 08:59:32

+0

我認爲他們需要動態碰撞工作。即使身體未設置爲動態,也應觸發聯繫人消息。無論如何,如果我將它們設置爲動態或不動態,它們中的一個或兩者都不重要,因爲它仍然不觸發該方法。 – mistakeNot 2014-09-05 18:22:45