2014-03-28 37 views

回答

0

好的,所以你需要做幾件事情。

我會假設你有兩個精靈......

SKSpriteNode *sprite1; 
SKSpriteNode *sprite2; 

這些正在飛來飛去場景,並可能會或可能不會與對方接觸。

所以,你需要設置一切,以啓用命中測試和回調。

首先,添加物理機構精靈......

sprite1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10]; 
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10)]; 
// this is a couple of examples, you'll have to set yours up to match the sprite size, shapes 

接下來,您需要添加類別的精靈。這使物理引擎什麼類型的機構,它的一些資料...

sprite1.physicsBody.categoryBitMask = 1 << 0; 
sprite2.physicsBody.categoryBitMask = 1 << 1; 
// again this is an example. You probably want to put these in an NS_OPTIONS typedef. 

然後,你需要告訴你,你被告知有關其接觸物理引擎。在這種情況下,當sprite1(1 < < 0)與sprite2(1 < < 1)接觸時,你會被告知。

所以......

sprite1.physicsBody.contactTestBitMask = 1 << 1; // it will test for contact against sprite 2 
sprite2.physicsBody.contactTestBitMask = 1 << 0; // test against sprite 1. 

現在在你的場景,你需要採取<SKPhysicsContactDelegate>和設置場景是物理學代表...

self.physicsWorld.contactDelegate = self; 

,然後創建方法..

- (void)didBeginContact:(SKPhysicsContact *)contact 
{ 
    // these two physics bodies have come into contact! 

    SKSpriteNode *firstContactNode = contact.bodyA.node; 
    SKSpriteNode *secondContactNode = contact.bodyB.node; 

    // now remove them... 

    [firstContactNode removeFromParent]; 
    [secondContactNode removeFromParent]; 
} 
+0

謝謝,真的很有幫助! – user3394385

+0

不用擔心,樂意幫忙:) – Fogmeister

0
SKAction* _vanishAction =[SKAction fadeOutWithDuration: 1]; 

    [sprite runAction:_vanishAction completion:^(void){ 
      [sprite removeFromParent];  
    }]; 
相關問題