更改collisionBitMask和categoryBitMask有时会出错

时间:2014-08-31 17:12:31

标签: objective-c sprite-kit

我用物理体设置2个物体,我只想在触摸和拖动1个物体时,我可以将它移动到另一个物体,当触摸到时,2个形状将移动,因为不覆盖另一个形状。 问题是SOMETIMES,当TouchesEnded时,2个对象覆盖另一个,有时不会:(

这是我的代码:

static const uint32_t objectCategory = 1 << 0;
static const uint32_t noCategory = 1 << 1;


-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
[self addChild:object1];
[self addChild:object2];
}

-(void)object1z
{

    object1 = [SKSpriteNode spriteNodeWithImageNamed:@"object1"];
    object1.position = CGPointMake(width*6.5/8, height/2);
    CGFloat offsetX = object1.frame.size.width * object1.anchorPoint.x;
    CGFloat offsetY = object1.frame.size.height * object1.anchorPoint.y;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL,  - offsetX, object1.frame.size.height - offsetY);
    CGPathAddLineToPoint(path, NULL,  - offsetX,  - offsetY);
    CGPathAddLineToPoint(path, NULL, object1.frame.size.height - offsetX, object1.frame.size.height - offsetY);
    CGPathCloseSubpath(path);
    object1.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    object1.physicsBody.allowsRotation = NO;
    object1.physicsBody.categoryBitMask = objectCategory;
    object1.physicsBody.collisionBitMask = objectCategory;
    object1.physicsBody.dynamic = YES;
    object1.physicsBody.usesPreciseCollisionDetection = YES;
    object1.physicsBody.restitution = 0;
object1.name = @"object1";
}

-(void)object2z
{

    object2 = [SKSpriteNode spriteNodeWithImageNamed:@"object2"];
    object2.position = CGPointMake(width*/8, height/2);
    CGFloat offsetX = object2.frame.size.width * object2.anchorPoint.x;
    CGFloat offsetY = object2.frame.size.height * object2.anchorPoint.y;
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL,  - offsetX, object2.frame.size.height - offsetY);
    CGPathAddLineToPoint(path, NULL,  - offsetX,  - offsetY);
    CGPathAddLineToPoint(path, NULL, object1.frame.size.height - offsetX, object1.frame.size.height - offsetY);
    CGPathCloseSubpath(path);
    object2.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    object2.physicsBody.allowsRotation = NO;
    object2.physicsBody.categoryBitMask = objectCategory;
    object2.physicsBody.collisionBitMask = objectCategory;
    object2.physicsBody.dynamic = YES;
    object2.physicsBody.usesPreciseCollisionDetection = YES;
    object2.physicsBody.restitution = 0;
object2.name = @"object2";
}

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

   for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
SKNode *node;
node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"object1"]) {
        o1 = 1;
        o2 = 0;
    } else if ([node.name isEqualToString:@"object2"])
    {
        o2 = 1;
        o1 = 0;

    }
}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPoint previousPosition = [touch previousLocationInNode:self];

    .CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
if (o2 ==1) {
    object2.physicsBody.categoryBitMask = noCategory;
    object2.physicsBody.collisionBitMask = noCategory;
    [object2 setPosition:CGPointMake(object2.position.x + translation.x, object2.position.y +translation.y)];
}
if (o1 ==1) {
    object1.physicsBody.categoryBitMask = noCategory;
    object1.physicsBody.collisionBitMask = noCategory;
    [object1 setPosition:CGPointMake(object1.position.x + translation.x, object1.position.y +translation.y)];
}
    }

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
        object1.physicsBody.categoryBitMask = objectCategory;
        object1.physicsBody.collisionBitMask = objectCategory;
        object2.physicsBody.categoryBitMask = objectCategory;
        object2.physicsBody.collisionBitMask = objectCategory;
}

0 个答案:

没有答案