HelloimDarius
HelloimDarius

Reputation: 695

Stop node rotation in Sprite Kit

I can't figure out how to stop node from rotating. Why allowsRotation isn't disabling it?
Here's how I describe my node:

SKSpriteNode *badguy = [SKSpriteNode spriteNodeWithTexture:[self.spriteAtlas textureNamed:@"test"]];
badguy.texture.filteringMode = SKTextureFilteringNearest;
badguy.physicsBody.angularVelocity = 0;
badguy.physicsBody.allowsRotation = NO;
badguy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(32, 32)];    
badguy.physicsBody.velocity = CGVectorMake(0, 0);
badguy.physicsBody.categoryBitMask = CollisionTypeEnemy;
badguy.physicsBody.contactTestBitMask = CollisionTypePlayer | CollisionTypeWall | CollisionTypeEnemy;
badguy.physicsBody.collisionBitMask = CollisionTypeWall | CollisionTypePlayer | CollisionTypeEnemy;
badguy.physicsBody.mass = 10000;
badguy.physicsBody.restitution = 0;
badguy.physicsBody.dynamic = YES;
badguy.zPosition = 10;

On Update I call this method:

-(void)enemiesFollow
{

    [self.world enumerateChildNodesWithName:@"badGuy" usingBlock:^(SKNode * _Nonnull badGuyNode, BOOL * _Nonnull stop) {
        if((SDistanceBetweenPoints(self.player.position, badGuyNode.position) < 100)&&
           (SDistanceBetweenPoints(self.player.position, badGuyNode.position) > 32))
        { 
            SKAction * actionMove = [SKAction moveTo:self.player.position duration:2.0];
            [badGuyNode runAction:actionMove];  
        }else{
            [badGuyNode removeAllActions];
        }

    }];

}

Upvotes: 4

Views: 1308

Answers (1)

Whirlwind
Whirlwind

Reputation: 13665

Thanks to Skyler Lauren's suggestion, I am pretty much sure that the problem is in these lines:

SKSpriteNode *badguy = [SKSpriteNode spriteNodeWithTexture:[self.spriteAtlas textureNamed:@"test"]];
badguy.texture.filteringMode = SKTextureFilteringNearest;
badguy.physicsBody.angularVelocity = 0;
badguy.physicsBody.allowsRotation = NO;
badguy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(32, 32)];  

The point is that you can't set properties of a physics body before you create an actual body. So, just move those lines after you create a physics body and you will be good, like this:

SKSpriteNode *badguy = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(32,32)];
badguy.texture.filteringMode = SKTextureFilteringNearest;
badguy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(32, 32)];
badguy.physicsBody.angularVelocity = 0;
badguy.physicsBody.allowsRotation = YES;

Upvotes: 6

Related Questions