Sprite Kit 按 Y 转动节点
Sprite Kit turn node by Y
我有 2 个节点:SKShapeNode coin 和 SKSpriteNode hero with delegate 和 didBeginContact 方法。接触工作正常,但在我反转我的英雄纹理后,节点不交互。
逻辑是:英雄是上线、下线和上线定位的硬币。当英雄上线时一切正常,但是当我使用 changeHeroSide 方法时,英雄反转并下线,didBeginContact 方法不响应。
- (void)changeHeroSide
{
self.yScale = -fabs(self.yScale); // this part don't interact under line
/** BUT THIS PART WORKS WELL AND INTERACTS UNDER LINE
self.zRotation = M_PI;
self.xScale = fabs(self.xScale);
**/
self.position = CGPointMake(self.position.x, self.position.y - self.frame.size.height);
self.physicsBody.affectedByGravity = NO;
}
创建节点
- (void)create
{
self.hero = [[VZHero alloc] initAtPosition:CGPointZero withPlayer:nil];
self.hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.hero.frame.size];
self.hero.xScale = -fabs(self.hero.xScale);
SKShapeNode *platform = [self getCurrentPlatform];
self.hero.position = CGPointMake([self getHeroEdgeXCoordinateForPlatform:platform], platform.frame.size.height + _hero.frame.size.height/2);
self.hero.physicsBody.dynamic = YES;
self.hero.physicsBody.categoryBitMask = monsterCategory;
self.hero.physicsBody.contactTestBitMask = projectileCategory;
self.hero.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:self.hero atWorldLayer:VZWorldLayerCharacter];
SKShapeNode *coin = [SKShapeNode shapeNodeWithCircleOfRadius:radius];
coin.name = @"coin";
coin.strokeColor = [SKColor blackColor];
coin.fillColor = [SKColor yellowColor];
coin.lineWidth = 1;
coin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
coin.physicsBody.dynamic = NO;
coin.position = pos;
coin.physicsBody.categoryBitMask = projectileCategory;
coin.physicsBody.contactTestBitMask = monsterCategory;
// coin.physicsBody.collisionBitMask = 0;
coin.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:coin atWorldLayer:VZWorldLayerCharacter];
}
以这种方式镜像 y 比例会扰乱物理体属性。看起来像是 SpriteKit 中的错误(至少在 iOS 7 上)。您可以通过将精灵包裹在容器节点中来解决这个问题。 This 相关问题可能会有一些帮助。
我有 2 个节点:SKShapeNode coin 和 SKSpriteNode hero with delegate 和 didBeginContact 方法。接触工作正常,但在我反转我的英雄纹理后,节点不交互。 逻辑是:英雄是上线、下线和上线定位的硬币。当英雄上线时一切正常,但是当我使用 changeHeroSide 方法时,英雄反转并下线,didBeginContact 方法不响应。
- (void)changeHeroSide
{
self.yScale = -fabs(self.yScale); // this part don't interact under line
/** BUT THIS PART WORKS WELL AND INTERACTS UNDER LINE
self.zRotation = M_PI;
self.xScale = fabs(self.xScale);
**/
self.position = CGPointMake(self.position.x, self.position.y - self.frame.size.height);
self.physicsBody.affectedByGravity = NO;
}
创建节点
- (void)create
{
self.hero = [[VZHero alloc] initAtPosition:CGPointZero withPlayer:nil];
self.hero.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.hero.frame.size];
self.hero.xScale = -fabs(self.hero.xScale);
SKShapeNode *platform = [self getCurrentPlatform];
self.hero.position = CGPointMake([self getHeroEdgeXCoordinateForPlatform:platform], platform.frame.size.height + _hero.frame.size.height/2);
self.hero.physicsBody.dynamic = YES;
self.hero.physicsBody.categoryBitMask = monsterCategory;
self.hero.physicsBody.contactTestBitMask = projectileCategory;
self.hero.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:self.hero atWorldLayer:VZWorldLayerCharacter];
SKShapeNode *coin = [SKShapeNode shapeNodeWithCircleOfRadius:radius];
coin.name = @"coin";
coin.strokeColor = [SKColor blackColor];
coin.fillColor = [SKColor yellowColor];
coin.lineWidth = 1;
coin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
coin.physicsBody.dynamic = NO;
coin.position = pos;
coin.physicsBody.categoryBitMask = projectileCategory;
coin.physicsBody.contactTestBitMask = monsterCategory;
// coin.physicsBody.collisionBitMask = 0;
coin.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:coin atWorldLayer:VZWorldLayerCharacter];
}
以这种方式镜像 y 比例会扰乱物理体属性。看起来像是 SpriteKit 中的错误(至少在 iOS 7 上)。您可以通过将精灵包裹在容器节点中来解决这个问题。 This 相关问题可能会有一些帮助。