ios - 未检测 SpriteKit 中边界框和 SKNode 之间的碰撞

标签 ios objective-c sprite-kit

这是我在 GameScene 中的代码。我有一个简单的 Sprite ,它在重力作用下掉落并在屏幕边缘弹跳,还有一个粒子发射器。我所需要的只是我应该能够检测到 SKNode 和物理体(即框架)之间的碰撞。

使用的代码

#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

这是我第一次使用 SpriteKit。所以请耐心等待并帮助我。

最佳答案

如果你想在他们接触时得到 didBeginContact 的回调,你需要像这样设置接触位掩码:

self.spinnyNode.physicsBody.contactTestBitMask = worldCategory;

当前您将其设置为 0x0,这意味着不向委托(delegate)人报告任何联系人。还需要在场景中设置delegate:

self.physicsWorld.contactDelegate = self;

更新

您还需要添加以下行:

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

进入 touchDownAtPoint 方法之后你创建了物理体:

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

否则它们将在您创建 physicsBody 时被重置。

关于ios - 未检测 SpriteKit 中边界框和 SKNode 之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104758/

相关文章:

html - 在 Swift 4 中将属性文本转换为 HTML

ios - 具有最大长度的 UITextField 自动完成

ios - 暂停 segue 直到 IAP 交易完成

ios - 调配一个没有实例的类

swift - 如何在 Swift 3 中使用 NSTimeInterval

ios - SkSpriteNode没有成员(member)中心

iphone - 基于页面的应用加载 epub

iphone - 向 CGMutableRefPath 添加边框颜色和圆角

swift - 生成球在屏幕外的随机位置

ios - 为什么我的应用程序在与计算机 iPhone 断开连接的情况下加载 MapKit View 时崩溃?