Jake Brown
Jake Brown

Reputation: 13

Spritekit node jitters while moving.... why?

Im trying to make a node move using SKAction but whenever I add the node and move it, it begins to jitter up and down along the y axis like crazy!

Any ideas?

I've tried using MoveToY, MoveTo, etc.. and no luck not sure what is causing the jitter..

Thanks..

-(void)addEnemy{
    enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy"];
    enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(40, 40)];
    enemy.physicsBody.dynamic = YES;
    enemy.physicsBody.usesPreciseCollisionDetection = YES;
    enemy.physicsBody.categoryBitMask = enemyHitCatagory;
    enemy.physicsBody.contactTestBitMask = bulletHitCatagory;
    enemy.physicsBody.collisionBitMask = bulletHitCatagory;

    enemy.position = CGPointMake(random() % 300, CGRectGetMaxY(self.frame));
    enemy.size = CGSizeMake(40, 40);
    enemy.zPosition = 5;
    SKAction *attack = [SKAction moveTo:(CGPointMake(20, 300)) duration:(3)];
    [enemy runAction:[SKAction repeatAction:attack count:1]];


    [self addChild:enemy];

}

Upvotes: 0

Views: 330

Answers (1)

Ben Kane
Ben Kane

Reputation: 10040

This is happening because the physics bodies are colliding. By default, an SKPhysicsBody has a restitution of 0.2, making it a little bouncy. Setting enemy.physicsBody.restitution = 0 and doing the same for the physics body your enemy is colliding with should resolve the jitter you're seeing.

Upvotes: 1

Related Questions