在SpriteKit中设置碰撞检测应该不是一件容易的事,而我之前已经做过。尽管在其他项目中成功使用了它,并且有关该主题的文档很多,但我无法在当前项目中使用它,我也不知道为什么。

上下文非常简单。我将球体(SKSpriteNode)添加到背景Sprite,然后用手指移动它们,希望它们在彼此接触时能够检测到碰撞。 (这不会发生)。

预期的结果是检测到碰撞。

场景设置:

@interface GameScene () <SKPhysicsContactDelegate>
...
@implementation GameScene

// Collision BitMask
static const uint32_t planetCategory =  0x1 << 0;


场景也将自己设置为PhysicsWorld的代表

[self.physicsWorld setContactDelegate:self];


我给场景只设置了一个碰撞位掩码,因为所有对象都属于同一类型,并且应该相互碰撞。我还设置了场景以符合SKPhysicsContactDelegate协议。

精灵配置:

#pragma mark Protocol Methods (For Adding Planets)
-(void)addSKPlanetDescriptorObject:(SKPlanetDescriptor *)object
{
    if (self.allowedNodes > 0){

        SKObject *node = [[SKObject alloc]initWithImageNamed:object.imageName];
        [node setDescriptor:object];

        // Set Physics
        [node.physicsBody setCategoryBitMask:planetCategory];
        [node.physicsBody setContactTestBitMask:planetCategory];
        [node.physicsBody setCollisionBitMask:planetCategory];

        [self.background addChild:node];
        [self.sceneObjects addObject:node];
        [node setPosition:self.pendingSpawnPoint];
        [self decreaseObjectCount];
    } else {
        [self indicateMinObjectCount];
    }
}


最后是用于SKSpriteNode的子类SKObject的初始化程序

@implementation SKObject


-(instancetype)initWithImageNamed:(NSString *)name
{
    self = [super initWithImageNamed:name];
    if (self)
    {
        [self setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:self.size.width/2]];
        [self.physicsBody setAffectedByGravity:NO];
        [self.physicsBody setUsesPreciseCollisionDetection:YES];
        [self.physicsBody setDynamic:YES];
        [self.physicsBody setMass:1000];
    }
    return self;
}


我试过在init语句之外删除setPhysicsBody方法,试过更改位掩码,试过省略setCollisionBitmask等,但是我根本无法弄清为什么没有碰撞。

我还添加了

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


如果发现任何问题,请用换行符通知我。但是这种方法永远不会被解雇。

最佳答案

使用与您的代码非常相似的简单代码(只需尝试拖动精灵),我就可以使一切正常(使用iPhone 6模拟器)...请尝试以下代码,看看是否对您有用:

#import "GameScene.h"
static const uint32_t planetCategory =  0x1 << 0;

@interface GameScene () <SKPhysicsContactDelegate>

@property (nonatomic,strong) SKSpriteNode *ball1;
@property (nonatomic,strong) SKSpriteNode *ball2;

@end

@implementation GameScene

-(void)didMoveToView:(SKView *)view {

    [self.physicsWorld setContactDelegate:self];

    SKSpriteNode *ball1 = [self createBall];

    // Set Physics
    [ball1.physicsBody setCategoryBitMask:planetCategory];
    [ball1.physicsBody setContactTestBitMask:planetCategory];
    [ball1.physicsBody setCollisionBitMask:planetCategory];
    ball1.position = CGPointMake(100,499);
    ball1.name = @"ball1";


    [self addChild:ball1];

    SKSpriteNode *ball2 = [self createBall];



    // Set Physics
    [ball2.physicsBody setCategoryBitMask:planetCategory];
    [ball2.physicsBody setContactTestBitMask:planetCategory];
    [ball2.physicsBody setCollisionBitMask:planetCategory];
    ball2.position = CGPointMake(100,123);
    ball2.name = @"ball2";


    [self addChild:ball2];


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];


        if([[self nodeAtPoint:location].name isEqualToString:@"ball1"]){

            [self childNodeWithName:@"ball1"].position = location;

        }
        if([[self nodeAtPoint:location].name isEqualToString:@"ball2"]){

            [self childNodeWithName:@"ball2"].position = location;


        }
    }
}

-(SKSpriteNode*) createBall{

    SKSpriteNode *ball = [[SKSpriteNode alloc] initWithImageNamed:@"balloon_large"];

    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];

    [ball.physicsBody setAffectedByGravity:NO];
    [ball.physicsBody setUsesPreciseCollisionDetection:YES];
    [ball.physicsBody setDynamic:YES];
    [ball.physicsBody setMass:1000];

    return ball;
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

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

@end


如果您无法使用此代码段重现良好的结果,则可能需要检查代码的其他部分以查找导致这些问题的原因。但是让我们先尝试一下,现在让我来谈谈结果...

关于sprite-kit - SpriteKit:Sprite之间没有碰撞检测。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29190197/

10-14 19:56