如何在碰撞时制作一个具有physicsBody不可移动/静态的精灵?

时间:2014-11-07 14:07:36

标签: ipad ios8 sprite-kit collision-detection

我有一个带气球的游戏。我从底部添加气球,它们飞起来,同时能够在屏幕外飞行。我给了气球physicsBodys,所以他们互相碰撞,但我想让屏幕左右边缘的气球布满。我找到了一个解决方案,其中精灵对象反射光线网站上的所有屏幕。但我需要一个不同的解决方案。 这是我的气球代码:

MBDBallon *randomBallon = [[MBDBallon alloc] initWithImageName:balloonImageName
                                               andBallonSize:ballonSize
                                               withBallonImageName:[self.balloonColorImageNamesArray objectAtIndex:balloonWithColorIndex]
                                               withStartingPosition:CGPointMake(self.viewSize.width/2, - self.viewSize.height )];

                    //randomBallon.name = [self returnRandomImageNameFromArray:self.ballonSpriteImageNamesArray];
                randomBallon.physicsBody.usesPreciseCollisionDetection = YES;
                randomBallon.physicsBody.categoryBitMask = balloonCategory;
                randomBallon.physicsBody.collisionBitMask = balloonCategory;
                randomBallon.physicsBody.contactTestBitMask = balloonCategory;

这是我的左墙代码:

  SKSpriteNode *leftWall = [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:CGSizeMake( self.viewSize.width / 15, self.viewSize.height * 3 )];
    [leftWall setAnchorPoint:CGPointMake(0.0f, 0.0f)];

    leftWall.position = CGPointMake( self.viewSize.width / 15 , 0.0f );
    leftWall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake( self.viewSize.width / 15, self.viewSize.height * 3 )];
    leftWall.physicsBody.categoryBitMask = balloonCategory;
    leftWall.physicsBody.collisionBitMask = balloonCategory;
    leftWall.physicsBody.contactTestBitMask = balloonCategory;
    leftWall.physicsBody.usesPreciseCollisionDetection = YES;
    leftWall.physicsBody.allowsRotation     = NO;
    leftWall.physicsBody.dynamic            = NO;
    leftWall.physicsBody.friction = 0.0f;
    [self addChild:leftWall];

然而,在几次气球碰撞后,墙壁正在移动。我真的不知道要阻止这种行为的捕获物或财产。 :

leftWall.physicsBody.dynamic            = NO;

应该停止一切运动,但事实并非如此。请帮忙,谢谢:)。

1 个答案:

答案 0 :(得分:3)

您尚未正确设置categoryBitMasks。

randomBallon.physicsBody.categoryBitMask = balloonCategory;
randomBallon.physicsBody.collisionBitMask = wallCategory|balloonCategory;

leftWall.physicsBody.collisionBitMask = wallCategory;
leftWall.physicsBody.contactTestBitMask = balloonCategory;

CategoryBitMask是您要分配给精灵的类别。 CollisionBitMask是您希望精灵与之碰撞的类别。在这种情况下,气球应该与墙壁和其他气球碰撞。墙应设置为仅与气球碰撞。