2014-01-29 4 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과 비교하기 때문에 접촉하거나 충돌하지 않으며 결과가 0이 아니면 접촉/충돌이 발생합니다.

즉, didBeginContact 메시지를 수신하려면 최소한 동일한 접촉 비트 마스크에 넣으십시오.

+0

감사하지만 모든 비트 마스크가 동일하더라도 여전히 작동하지 않는 것 같습니다. – user3249418

+3

오, 나는 단지 비트 마스크를 할당 한 시점에서 비트 마스크를 변경 한 후에 만 ​​바디를 생성 했으므로 물리 본문이 여전히 nil이라는 사실을 알고 있었기 때문에 디폴트 값을가집니다. – LearnCocos2D

+0

와우. 그것은 완벽한 감각을 만듭니다! 나는 그것을 시도하고 최대한 빨리 알려 드리겠습니다! – user3249418

관련 문제