ios7 - SpriteKit碰撞检测不起作用

标签 ios7 collision-detection sprite-kit

我以为我理解了位掩码、CategoryBitMasks 和 CollisionBitMasks 的概念,但事实证明我不理解;-( 但我不明白为什么。 我设置了用于碰撞检测的位掩码,将类别位掩码添加到帧,并将categoryBitMask 添加到我的对象(在本例中为出租车)。但出租车只是从屏幕上掉下来:-/

有什么想法为什么会这样吗?

#import "MyScene.h"
#import "SKSpriteNode+DebugDraw.h"

// Define Bit Masks for Collision Detection
typedef NS_OPTIONS(uint32_t, CNPhysicsCategory) {
    CNPhysicsCategoryEdge = 1 <<0,
    CNPhysicsCategoryTaxi = 1 <<1,

};

@interface MyScene() <SKPhysicsContactDelegate>
@end

@implementation MyScene{
    SKNode *_gameNode;
    SKSpriteNode *_taxiNode;
}

-(instancetype)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        [self initializeScene];
    }
    return self;
}

-(void)initializeScene{

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    self.physicsWorld.contactDelegate = self;
    self.physicsBody.categoryBitMask = CNPhysicsCategoryEdge;

    SKSpriteNode* bg = [SKSpriteNode spriteNodeWithImageNamed:@"background.png"];
    bg.position = CGPointMake(self.size.width/2, self.size.height/2);
    [self addChild: bg];

    [self addTaxi];

    _gameNode = [SKNode node];
    [self addChild:_gameNode];
}

-(void)addTaxi{
    _taxiNode = [SKSpriteNode spriteNodeWithImageNamed:@"taxi.png"];
    _taxiNode.position = CGPointMake(self.size.width/2, self.size.height/2);

    [self addChild:_taxiNode];

    CGSize contactSize = CGSizeMake(_taxiNode.size.width, _taxiNode.size.height);
    _taxiNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: contactSize];
    [_taxiNode attachDebugRectWithSize:contactSize];

    _taxiNode.physicsBody.categoryBitMask = CNPhysicsCategoryTaxi;

}

最佳答案

我终于成功了 - 问题不是碰撞检测,而是手机的布局,因为我没有用 viewWillLayoutSubviews 方法替换 ViewController 中的 viewDidLoad 方法。 现在一切正常。

重要帖子如下:bodyWithEdgeLoopFromRect not working in landscape

感谢您的支持!

关于ios7 - SpriteKit碰撞检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641280/

相关文章:

javascript - 如何让 JavaScript 中的碰撞函数正常工作?

swift - 球在 Pong Clone 中不动

ios - 为什么 SKEmitterNode 会破坏累积的帧?

ios - 我不断收到错误 'Instance member isPaused cannot be used on type GameScene'

iOS 7 UIBarButtonItem 荒谬的间距问题

ios7 - 尽管 ios 6.1 作为基础 sdk xcode 5 为 ios 7 构建 ipa

ios - 快速检测两个 UIView 的碰撞

ios - 无法使用initWithUUIDString创建NSUUID

ios - 删除 UISegmentedControl 的框架

python - Pygame 碰撞检测不适用于旋转图像