2017-02-08 12 views
0

ここに私のコードはGameSceneです。私は、重力の下に落ちるシンプルなスプライトを持っていて、スクリーンのエッジとパーティクルエミッタの跳ね返りをしています。私が必要とするのは、SKNodeと物理学者、つまりフレームとの間の衝突を検出できるはずです。SpriteKitで境界フレームとSKNodeの衝突を検出しない

コードは、これが私の最初の時間がSpriteKitで作業している

#import "GameScene.h" 
@interface GameScene() <SKPhysicsContactDelegate> 
@property (strong) UITouch *rightTouch; 
@property (strong) UITouch *leftTouch; 
@property (strong) SKNode *spinnyNode; 
@property (assign, nonatomic) int count; 
@end 
@implementation GameScene { 
} 
    static const uint32_t birdCategory = 1 << 0; 
    static const uint32_t worldCategory = 1 << 1; 
- (void)didMoveToView:(SKView *)view { 
    self.backgroundColor = [UIColor colorWithRed:134/255.0 green:50/255.0 blue:148/255.0 alpha:1.0]; 
    self.scaleMode = SKSceneScaleModeAspectFit; 
    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame]; 
    self.physicsBody.categoryBitMask = worldCategory; 
    NSString *burstPath = [[NSBundle mainBundle] pathForResource:@"fire" ofType:@"sks"]; 
    SKEmitterNode *burstNode = [NSKeyedUnarchiver unarchiveObjectWithFile:burstPath]; 
    burstNode.position = CGPointMake(0, 0); 
    [self addChild:burstNode]; 
    self.spinnyNode = [self childNodeWithName:@"ball"]; 
    self.spinnyNode.physicsBody.angularVelocity = 10.0; 
    self.spinnyNode.hidden = YES; 
    self.spinnyNode.physicsBody.categoryBitMask = birdCategory; 
    self.spinnyNode.physicsBody.contactTestBitMask = 0x0; 
// self.spinnyNode = ball; 
    SKNode *leftPaddle = [self childNodeWithName:@"bottom"]; 
    leftPaddle.physicsBody.angularVelocity = 10.0; 
} 
- (void)touchDownAtPoint:(CGPoint)pos { 
    self.count = self.count+1; 
    CGPoint p = pos; 
    NSLog(@"/n %f %f %f %f", p.x, p.y, self.frame.size.height, self.frame.size.width); 
    if (self.count == 1) 
    { 
     self.spinnyNode.position = p; 
     self.spinnyNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:40]; 
     self.spinnyNode.physicsBody.velocity=CGVectorMake(203, 10); 
     self.spinnyNode.hidden = NO; 
     self.spinnyNode.physicsBody.restitution = 0.8; 
     self.spinnyNode.physicsBody.linearDamping = 0.0; 
     self.spinnyNode.physicsBody.angularDamping = 0.3; 
     self.spinnyNode.physicsBody.friction = 0.1; 
     self.spinnyNode.physicsBody.angularVelocity = -100.0; 
    } 
} 
-(void)didBeginContact:(SKPhysicsContact *)contact 
{ 
    NSLog(@"contact detected"); 
    NSString *nameA = contact.bodyA.node.name; 
    NSString *nameB = contact.bodyB.node.name; 
    SKPhysicsBody *firstBody; 
    SKPhysicsBody *secondBody; 
    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) 
    { 
     firstBody = contact.bodyA; 
     secondBody = contact.bodyB; 
    } 
    else 
    { 
     firstBody = contact.bodyB; 
     secondBody = contact.bodyA; 
    } 
    //Your first body is the block, secondbody is the player. 
    //Implement relevant code here. 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    for (UITouch *t in touches) {[self touchDownAtPoint:[t locationInNode:self]]; 
} 
} 
-(void)update:(CFTimeInterval)currentTime { 
    // Called before each frame is rendered 
} 
@end 

を使用します。だから、気をつけて私を助けてください。

+1

didMove(toView :)ではスピンノードの物理的ボディを作成し、それを設定します。 – Whirlwind

答えて

1

あなたは、彼らはあなたがこのような接触ビットマスクを設定する必要がありますタッチするとdidBeginContactへのコールバックを取得したい場合:

self.spinnyNode.physicsBody.contactTestBitMask = worldCategory; 

は現在、あなたは0x0にそれを設定しているに任意の連絡先を報告しないことを意味代議員。また、現場にデリゲートを設定する必要があります。

self.physicsWorld.contactDelegate = self; 

あなたはまた、ライン配置する必要がUPDATE

:後touchDownAtPoint方法

self.spinnyNode.physicsBody.contactTestBitMask = worldCategory; 
self.spinnyNode.physicsBody.categoryBitMask = birdCategory; 

を作成しました物理学の身体:

self.spinnyNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:40]; 

これ以外の場合は、physicsBodyの作成時にリセットされます。

+0

あなたの時間に感謝していますが、あなたが示唆したとおりに変更を加えましたが、まだ動作していません。つまり、衝突を検出しています。 –

+0

いいえ、ちょうど私が理解していることを確認するために、どのノード間で衝突を検出する必要がありますか? spinnyNodeがフレームに当たったら、私が与えたコードがうまくいくはずです。他のノードでは、それぞれにphysicsBodyがあり、それぞれがcategoryBitMaskを持ち、それぞれのcontactTestBitMaskがotherのcategoryBitMaskに設定されていることを確認する必要があります。 – simonWasHere

+0

実際には、別のものを見て、あなたは行を移動する必要があります:self.spinnyNode.physicsBody.categoryBitMask = birdCategory;およびself.spinnyNode.physicsBody.contactTestBitMask = worldCategory; physicsBodyを作成した後、私は自分の答えを更新します – simonWasHere

関連する問題