使用精灵套件,我试图通过物理模拟使一系列球从空中掉落,我希望它们能够在不相互碰撞的情况下掉落并反弹。我只是想让他们直接走下去,互相穿过。我应该如何通过collisionBitMask
和categoryBitMask
来实现这一目标?
// Common.h
#ifndef Rainy_Poops_common_h
#define Rainy_Poops_common_h
static int poopSize_x = 20;
static int poopSize_y = 20;
static const uint32_t poopCategory = 0x1 << 1;
#endif
// MyScene.h
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
_timer++;
if (_timer % 3 == 0) {
SKSpriteNode * p = [[Poop alloc] init];
NSInteger random_x = arc4random_uniform([[UIScreen mainScreen]applicationFrame].size.width);
p.position = CGPointMake(random_x, [[UIScreen mainScreen]applicationFrame].size.height - 5);
[self addChild: p];
[self enumerateChildNodesWithName:@"poop" usingBlock:
^(SKNode *node, BOOL *stop) {
Poop *poop = (Poop *) node;
if (!poop.isDropping) {
poop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(poopSize_x, poopSize_y)];
poop.isDropping = YES;
}
if (poop.position.y < 0) {
[poop removeFromParent];
}
}];
}
}
#import "Poop.h"
@implementation Poop
- (id)init {
self = [super initWithImageNamed:@"poop2.png"];
self.isDropping = NO;
self.name = @"poop";
self.size = CGSizeMake(poopSize_x, poopSize_y);
SKPhysicsBody *physicsPoop = self.physicsBody;
physicsPoop.collisionBitMask = 0;
physicsPoop.categoryBitMask = poopCategory;
physicsPoop.affectedByGravity = YES;
physicsPoop.mass = 100;
physicsPoop.allowsRotation = NO;
physicsPoop.dynamic = YES;
return self;
}
@end
答案 0 :(得分:1)
我认为你的问题与这一行有关 -
poop.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(poopSize_x,poopSize_y)];
您在节点的初始化中将categoryBitMask
初始化为2并将collisionBitMask
初始化为0,但此行将重置您的大便的physicsBody并指定默认的collisionBitMask
和{{ 1}} categoryBitMask
答案 1 :(得分:0)
将collisionButMask设置为0表示没有碰撞,或者等于你希望球碰撞的精灵的categoryBitMask。