2014-07-20 7 views
1

무엇이 여기에 있습니까? Log는 "Ball이 프레임 테두리 또는 추가 된 경우 바닥 오브젝트와 충돌 할 때마다"플레이어가 충돌한다고 말합니다.볼은 플레이어와 접촉하지만 더 가까이 있지 않습니다.

(미안 매우 스프라이트 키트 새로운이 나에게 미친하고있다 충돌 작업 내 처음이다. D)를

#import "MyScene.h" 

static const int starHitCategory = 1; 
static const int playerHitCategory = 2; 
static const int ballHitCategory = 3; 

@interface MyScene() 

@end 

@implementation MyScene{ 

    SKSpriteNode *player; 
    SKSpriteNode *redball; 
    SKSpriteNode *star; 
} 

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

     self.physicsWorld.gravity = CGVectorMake(0.0f, -3.0f); 
     SKPhysicsBody* borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]; 
     self.physicsBody = borderBody; 
     self.physicsBody.friction = 0.0f; 

     //initalizing player node 
     player = [SKSpriteNode spriteNodeWithImageNamed:@"player.png"]; 

     player.physicsBody.categoryBitMask = playerHitCategory; 
     player.physicsBody.contactTestBitMask = playerHitCategory; 
     player.physicsBody.collisionBitMask = playerHitCategory; 

     player.position = CGPointMake(75,101); 
     player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size]; 
     player.physicsBody.friction = 0.0f; 
     player.physicsBody.linearDamping = 0.5f; 
     player.physicsBody.mass = 10; 
     player.physicsBody.dynamic = YES; 
     player.physicsBody.usesPreciseCollisionDetection = YES; 
     player.physicsBody.allowsRotation = NO; 
     player.name = @"player"; 
     [self addChild:player]; 

     //initalizing Redball node 
     redball = [SKSpriteNode spriteNodeWithImageNamed:@"redbdall.png"]; 
     redball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:redball.frame.size.width/2]; 
     redball.position = CGPointMake(290,200); 
     redball.physicsBody.usesPreciseCollisionDetection = YES; 
     redball.physicsBody.categoryBitMask = ballHitCategory; 
     redball.physicsBody.contactTestBitMask = ballHitCategory; 
     redball.physicsBody.collisionBitMask = ballHitCategory; 
     redball.physicsBody.friction = 2.0f; 
     redball.physicsBody.restitution = 1.0f; 
     redball.physicsBody.linearDamping = 0.0f; 
     redball.physicsBody.allowsRotation = NO; 
     [self addChild:redball]; 

    //initalizing Star node 
    star = [SKSpriteNode spriteNodeWithImageNamed:@"star.png"]; 
    star.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:star.size]; 
    star.physicsBody.categoryBitMask = starHitCategory; 
    star.physicsBody.contactTestBitMask = starHitCategory; 
    star.physicsBody.collisionBitMask = starHitCategory; 
    star.position = CGPointMake(205,125); 
    [self addChild:star]; 

    } 
    return self; 
} 


-(void)didBeginContact:(SKPhysicsContact *)contact 
{ 
    SKPhysicsBody *firstBody, *secondBody; 

    firstBody = contact.bodyA; 
    secondBody = contact.bodyB; 


    if(firstBody.categoryBitMask == playerHitCategory || secondBody.categoryBitMask == ballHitCategory) 
    { 
     NSLog(@"ball hits player"); 
    } 
} 

@end

미리 감사드립니다

답변

0

플레이어/볼 오브젝트가 충돌 할 수있는 오브젝트와 충돌 할 경우 if 오브젝트가 만족 될 것입니다. 왜냐하면 다른 오브젝트가 필수 카테고리 인지도 확인하지 않기 때문입니다.

당신은 또한 공 또는 플레이어 또는 바닥 인이 어떤 목적을 알고하지 않기 때문에 문이 더 엄격 할 경우 당신은 당신을 변경해야합니다

:

if((firstBody.categoryBitMask == playerHitCategory && secondBody.categoryBitMask == ballHitCategory) || 
     (firstBody.categoryBitMask == ballHitCategory && secondBody.categoryBitMask == playerHitCategory)) 
     { 
      NSLog(@"ball hits player"); 
     } 

이 방법은 당신이 사이의 충돌을 분리해서 할 수 있습니다 개체. 충돌을 감지하고 싶은 다른 물체에 대해서도 똑같이하십시오.

각 물리 본문 개체에 대해 비트 마스크를 잘못 설정한다는 점에 유의하십시오. 섹션에 대한 here 모양을 읽어 "충돌 및 연락처 예 : 로켓 공간에서"

을 충돌이 정확하게 감지 할 수 있도록 각 마스크 속성이 사용되는 것을 이해하는 것이 매우 중요합니다. (그렇지 않으면 참으로 너는 그것에 대해 열망 할 수있다;))

+0

Giorashc에게 대답 할 시간을내어 주셔서 감사합니다 :) 작성한 코드는 작동하지 않습니다. 로그 메시지도 표시되지 않습니다 (( – sone3d

+0

). Apple의 예와 같이 마스크 비트를 올바르게 설정해야하기 때문에이 충돌 마스크는 충돌 할 수있는 다른 객체를 정의합니다. 시도해 보셨습니까? – giorashc

+1

대단히 감사합니다. 그것은 일단 내가 마스크를 설정하면 작동합니다. – sone3d

관련 문제