我以为我理解了位掩码、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/