我有一堆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;
无论碰撞是在顶部,左侧还是右侧,都会发生这种情况。我已经走了,并确保我的图像尽可能多地被丢弃。我不确定它还能是什么。看起来身体的命中箱似乎比它应该更大。
答案 0 :(得分:2)
英雄精灵的维度是什么?如果是方形图像,则更改以下
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width];
到
hero.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:hero.frame.size.width/2.0f];