0

In my spritekit game I am working on applying a wind like force to my game. I have somehow implemented this a number of ways.

The first way I tried is this : Attempt one In this code here the sprite is first pushed as it should, however I cant get it too stop.

-(void)update:(NSTimeInterval)currentTime {

   NSLog(@" %d",_time);
  if (_time == 200 /* | _time==400 */ ) {

  [self startPush];


  }
    -(void)startPush
    {

      SKAction *startPush = [SKAction runBlock:^{
        [_player.physicsBody applyForce:CGVectorMake(50, 0)];
      }];


      SKAction *wait = [SKAction waitForDuration:1];
      SKAction *stopPush = [SKAction runBlock:^{

     //   [_player.physicsBody applyForce:CGVectorMake(-50, 0)];
        [_player.physicsBody applyForce:CGVectorMake(0, 0)];


      }];
      SKAction *sequence = [SKAction sequence:@[startPush, wait,stopPush]];
      [self runAction:sequence];



    }

Attempt Two I attempted to change my code since i saw this post by LearnCocos2d where he said that AKActions are bad for movements.

Constant movement in SpriteKit

So i tried this

Here also the pushing doesnt stop even if pushOn is set to No. Would love some help in doing the final tweaks in this.

Thank you

-(void)update:(NSTimeInterval)currentTime {

   NSLog(@" %d",_time);
  if (_time == 200 /* | _time==400 */ ) {


    windOn=NO;
  }else  if (_time==300)
{
pushOn=NO;
}

  if (pushOn)
  {

      {

        [_player.physicsBody applyForce:CGVectorMake(0.1, 0)];
      }
Community
  • 1
  • 1
Sleep Paralysis
  • 449
  • 7
  • 22

1 Answers1

2

Applying a zero force is equivalent to applying ... nothing. You're basically leaving the body as is with this code:

[_player.physicsBody applyForce:CGVectorMake(0, 0)];

Instead you probably wanted to zero the velocity vector:

_player.physicsBody.velocity = CGVectorMake(0, 0);

The difference is that applyForce acts as an "add vector" operation, whereas the line above is a "set vector" operation.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • ah its you! I was also of the mind that if I applied the exact same force the opposite way it would negate the force being applied on it right now. Also, Is it ok using actions to do such a task? – Sleep Paralysis May 21 '14 at 14:40