Question

I've defined the gravity as 0 in the initWithSize method and then in the touchesBegan i've set it to -5 so when you touch the screen the app will start the problem is in the touchesBegan method i've also implemented an applyImpulse, but it does not trigger. It is only the gravity that is being triggered. This means the mover just falls to the button.

How come the applyImpulse not being triggered?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 );
    mover.physicsBody.restitution = 0.0;
    mover.physicsBody.velocity = CGVectorMake(0, 0);
    [mover.physicsBody applyImpulse:CGVectorMake(0, 15)];

}

and the mover method:

-(void)addMover:(CGSize) size {


    SKTexture* lolTexture1 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol1"]];
    lolTexture1.filteringMode = SKTextureFilteringNearest;

    SKTexture* lolTexture2 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol2"]];
    lolTexture2.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture3 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol3"]];
    lolTexture3.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture4 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol4"]];
    lolTexture4.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture5 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol5"]];
    lolTexture5.filteringMode = SKTextureFilteringNearest;
    SKTexture* lolTexture6 = [SKTexture textureWithImage:[UIImage imageNamed:@"lol6"]];
    lolTexture6.filteringMode = SKTextureFilteringNearest;
    SKAction *flap = [SKAction repeatActionForever:[SKAction animateWithTextures:@[lolTexture1, lolTexture2, lolTexture3, lolTexture4, lolTexture5, lolTexture6] timePerFrame:0.15]];

    SKSpriteNode *mover = [SKSpriteNode spriteNodeWithTexture:lolTexture1];
    [mover setScale:1.0];

    mover.position = CGPointMake(self.frame.size.width / 4, CGRectGetMidY(self.frame));

    [mover runAction:flap withKey:@"birdGo"];

    [self addChild:mover];

    self.physicsWorld.gravity = CGVectorMake( 0.0, 0 );

    mover.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:mover.frame.size.width/2-7];

}

Was it helpful?

Solution

Declare mover outside of addMover so that you can use it in touchesBegan method.

SKSpriteNode *mover;

Then in addMover

mover = [SKSpriteNode spriteNodeWithTexture:lolTexture1];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top