没有调用spritekit didBeginContact

时间:2014-09-03 08:07:46

标签: ios objective-c xcode sprite-kit skphysicsbody

didBeginContact由于某种原因没有被调用。我怎样才能解决这个问题?谢谢!我设置了类别位掩码和skphysicsContactDelegate,但它仍然没有注册联系人。我已经坚持了一段时间了。

#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

2 个答案:

答案 0 :(得分:0)

为了启用接触检测,您需要设置

node.physicsBody.dynamic = YES; 

同时查看documentation

答案 1 :(得分:0)

固定!在创建播放器方法中,我错误地设置了玩家精灵的位置,而不是设置玩家节点的位置!

相关问题