2014-01-29 35 views
0
#import "collisionTestMyScene.h" 
const static int nodeBitMask = 0x1 << 0; 
const static int node1BitMask = 0x1 << 1;; 

@implementation collisionTestMyScene 

-(id)initWithSize:(CGSize)size {  
    if (self = [super initWithSize:size]) { 
     /* Setup your scene here */ 
     self.physicsWorld.contactDelegate = self; 
     w = 0; 


      } 
    return self; 
} 
-(void) didBeginContact:(SKPhysicsContact *)contact { 
    NSLog(@"Contact Begin"); 

    if (contact.bodyA.categoryBitMask == nodeBitMask) { 
     NSLog(@"Node is Body A"); 
    } 
    if (contact.bodyA.categoryBitMask == node1BitMask) { 
     NSLog(@"Node is Body B"); 
    } 
} 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    /* Called when a touch begins */ 

    for (UITouch *touch in touches) { 
     CGPoint location = [touch locationInNode:self]; 
     node = [SKSpriteNode spriteNodeWithImageNamed:@"block.jpg"]; 
     node.position = location; 
     [node setScale:0.07]; 
     node.physicsBody.contactTestBitMask = node1BitMask; 
     node.physicsBody.categoryBitMask = nodeBitMask; 
     node.physicsBody.collisionBitMask = nodeBitMask; 
     //node.physicsBody.collisionBitMask = 0; 
     node1 = [SKSpriteNode spriteNodeWithImageNamed:@"block2.jpg"]; 
     node1.position = CGPointMake(200, 200); 
     node1.physicsBody.categoryBitMask = node1BitMask; 
     node1.physicsBody.contactTestBitMask = nodeBitMask; 
     node1.physicsBody.collisionBitMask = node1BitMask; 
     //node1.physicsBody.collisionBitMask = 0; 
     [node1 setScale:0.07]; 


     [self addChild:node]; 
     [self addChild:node1]; 
     node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(node.size.width, node.size.height)]; 
     node1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(node1.size.width, node1.size.height)]; 
     SKAction *moveUp = [SKAction moveToX:100 duration:3]; 
     node1.physicsBody.affectedByGravity = NO; 
     node.physicsBody.affectedByGravity = NO; 
     [node1 runAction:moveUp]; 
     w = 1; 

    } 



} 

它從來沒有NSLogging任何東西。我已經嘗試改變位掩碼,等等。 CGRectIntersects函數可以工作,但它不夠準確。另外,這兩個節點都是完美的盒子形狀。我可能做錯了什麼?先謝謝你!- (void)didBeginContact沒有被調用。思考?

+0

你確定調用了initWithSize嗎?嘗試在該方法中放置一個斷點並確保代理已設置。 – DrKey

+0

是的,我剛剛檢查過,它叫做 – user3249418

+0

你在哪裏調用/調用didBeginContact?你錯過了一個插座連接? – BriOnH

回答

2

這裏的問題是位掩碼。這兩個節點位於不同的類別中,即聯繫人和衝突組(位掩碼)。因此,它們不會接觸也不會發生衝突,因爲將掩碼與AND進行比較,並且只有在結果爲非零時纔會發生接觸/衝突。

總之,爲了接收didBeginContact消息,至少要把它們放在同一個接觸位掩碼中。

+0

謝謝,但是即使所有的位掩碼都一樣,它仍然不起作用。 – user3249418

+3

哦,我剛剛注意到,在分配位掩碼的時候,物理體仍然是零,因爲只有在更改位掩碼後才創建主體,所以它們將具有默認值。 – LearnCocos2D

+0

哇。這使得PERFECT Sense!我會盡力而爲,讓你儘快知道! – user3249418

相關問題