2014-03-04 83 views
0

我正在爲iOS執行一個簡單的遊戲。 我正在嘗試使用Sprite Kit進行開發。 但是,我不知道爲什麼檢測聯繫沒有發生。 任何人都可以幫我解決這個問題嗎?爲什麼SpriteKit PhysicContactDelegate沒有工作?

這裏是我不能得到預期的結果代碼:

#import "TesttingScene.h" 

@interface TesttingScene()<SKPhysicsContactDelegate> 

@property (nonatomic) SKTexture *ballText; 

@end 

@implementation TesttingScene 

-(id)initWithSize:(CGSize)size{ 
    self = [super initWithSize:size]; 
    if (self) { 
     self.physicsWorld.gravity = CGVectorMake(0, 0); 
     self.physicsWorld.contactDelegate = self; 

     SKSpriteNode *hitBoxx = [[SKSpriteNode alloc] initWithColor:[UIColor clearColor] size:CGSizeMake(self.frame.size.width/3, self.frame.size.height/3)]; 
     hitBoxx.anchorPoint = CGPointMake(0, 0); 
     hitBoxx.position = CGPointMake(self.size.width/2, self.size.height/2); 
     hitBoxx.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:hitBoxx.frame.size]; 
     hitBoxx.physicsBody.dynamic = YES; 
     hitBoxx.physicsBody.categoryBitMask = abPlayerHitBoxCategory; 
     hitBoxx.physicsBody.contactTestBitMask = adsViewCategory; 
     hitBoxx.physicsBody.collisionBitMask = 0; 
     hitBoxx.physicsBody.usesPreciseCollisionDetection = YES; 

     self.ballText = [SKTexture textureWithImageNamed:@"FinalBossSkill1SS"]; 

    } 
    return self; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    SKTexture *t1 = [SKTexture textureWithRect:CGRectMake(0, 0, 0.5, 1) inTexture:self.ballText]; 
    SKTexture *t2 = [SKTexture textureWithRect:CGRectMake(0.5, 0, 0.5, 1) inTexture:self.ballText]; 
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithTexture:t1]; 
    ball.position = CGPointMake(self.size.width, self.size.height/2); 
    ball.name = @"FinalBossSkill1Ball"; 
    ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ball.size]; 
    ball.physicsBody.dynamic = YES; 
    ball.physicsBody.categoryBitMask = adsViewCategory; 
    ball.physicsBody.contactTestBitMask = abPlayerAttackBoxCategory | abPlayerHitBoxCategory; 
    ball.physicsBody.collisionBitMask = 0; 
    ball.physicsBody.usesPreciseCollisionDetection = YES; 
    SKAction *moveTo = [SKAction moveToX:-ball.size.width duration:1.0]; 
    SKAction *flash = [SKAction animateWithTextures:@[t1,t2] timePerFrame:0.1]; 
    SKAction *moveBall = [SKAction repeatAction:flash count:moveTo.duration/flash.duration]; 
    SKAction *group = [SKAction group:@[moveTo, moveBall]]; 
    [self addChild:ball]; 
    [ball runAction:[SKAction sequence:@[group, [SKAction removeFromParent]]]]; 
} 

-(void)didBeginContact:(SKPhysicsContact *)contact{ 
    NSLog(@"Contact"); // <~~~~this msg doesn't appear in console when the ball pass the hitboxx. 
} 
+0

聯繫什麼和什麼?從我所看到的屏幕上只有一個對象? – Fogmeister

+0

從這兩種類型的節點中刪除將collisionbitsmask設置爲0的行 – ZeMoon

+0

我希望SkpriteNote「Ball」與hitboxx聯繫,然後msg [NSLog(@「contact」)]將運行。 – user3370097

回答

0

你需要定義位掩碼類別,下方的進口。

static const uint32_t abPlayerHitBoxCategory = 0x1 << 0; 
static const uint32_t adsViewCategory = 0x1 << 1; 
+0

謝謝你的回覆,我已經實施了這些。 我發現如果移動速度太快,物理代理將無法檢測到它。我認爲這是一個限制,當我使用SKAction的運動。 – user3370097

相關問題