1

我设置了一堆 SKSpriteNode。我的英雄节点不应该撞墙,如果撞墙,它会触发碰撞。这工作正常,但碰撞点并不完全在精灵的图像上。

这是英雄的代码:

hero = [SKSpriteNode spriteNodeWithImageNamed:@"hero"];
hero.name = heroCategoryName;
hero.position = CGPointMake(100, CGRectGetMidY(self.frame));
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width];
hero.physicsBody.friction = 1.0f;
hero.physicsBody.restitution = 0.0f;
hero.physicsBody.linearDamping = 0.1f;
hero.physicsBody.allowsRotation = NO;
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.contactTestBitMask = wallCategory;
hero.physicsBody.usesPreciseCollisionDetection = YES;
hero.physicsBody.mass = 0.2f;
[self addChild:hero];

这是墙壁:

SKSpriteNode *wall  = [SKSpriteNode spriteNodeWithImageNamed:@"wall"];
        wall.name = wallCategoryName;
        wall.hidden = YES;

        wall.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:wall.size];
        wall.physicsBody.dynamic = NO;
        wall.physicsBody.collisionBitMask = 0;
        wall.physicsBody.usesPreciseCollisionDetection = YES;
        wall.physicsBody.categoryBitMask = wallCategory;
        wall.physicsBody.contactTestBitMask = heroCategory;

无论碰撞是在顶部、左侧还是右侧,都会发生这种情况。我已经走了,并确保我的图像都尽可能地被废话。我不确定它还能是什么。看起来身体的碰撞箱比它应该的要大。

4

1 回答 1

2

英雄精灵的尺寸是多少?如果是正方形图像,则更改以下内容

hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width];

进入

hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width/2.0f];
于 2014-02-10T07:16:04.347 回答