user3312949
user3312949

Reputation: 145

Sprite Kit turn node by Y

I have 2 nodes: SKShapeNode coin and SKSpriteNode hero with delegate and didBeginContact method. Contact works fine, but after i reverse my hero texture, nodes are don't interact. The logic is: hero is go on line, under line and above line positioned coins. When hero is go above line all good, but when i use changeHeroSide method, hero reversed and go under line, didBeginContact method don't response.

- (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;

}

Creating of nodes

 - (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];
    }

Upvotes: 2

Views: 91

Answers (1)

MutenFuzz
MutenFuzz

Reputation: 135

Mirroring the y scale that way is messing up the physics body properties. Looks like a bug in SpriteKit (at least on iOS 7). You can solve this by wrapping your sprite in a container node. This related question might be of some help.

Upvotes: 2

Related Questions