Sprite Kit - object colliding because of scale aft

2019-02-18 17:07发布

问题:

There was no problem but after iOS 7.1 update, object is colliding 2pipes' gap. Someone said before, it can be because of 'yScale' but I couldn't find the solution.

Some code;

  Pipe *pipe = [[Pipe alloc] initWithImageNamed:pipeImageName];
  [pipe setCenterRect:CGRectMake(26.0/kPipeWidth, 26.0/kPipeWidth, 4.0/kPipeWidth, 4.0/kPipeWidth)];
  [pipe setYScale:height/pipe.size.height];
  [pipe setPosition:CGPointMake(320+(pipe.size.width/2), abs(pipeYOffset + (pipe.size.height/2)))];

  pipe.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipe.size];
  [pipe.physicsBody setAffectedByGravity:NO];
  [pipe.physicsBody setDynamic:NO];

Any help will be appreciated. Thank you!

回答1:

  Pipe *pipe = [[Pipe alloc] initWithImageNamed:pipeImageName];
  [pipe setCenterRect:CGRectMake(26.0/kPipeWidth, 26.0/kPipeWidth, 4.0/kPipeWidth, 4.0/kPipeWidth)];

//set the physicsBody before X/Yscale
  pipe.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipe.size];

  [pipe setYScale:height/pipe.size.height];
  [pipe setPosition:CGPointMake(320+(pipe.size.width/2), abs(pipeYOffset + (pipe.size.height/2)))];

  [pipe.physicsBody setAffectedByGravity:NO];
  [pipe.physicsBody setDynamic:NO];

This may help you. I think this is a bug in ios7.1.



回答2:

I have the same problem, my game got released tonight and is now unplayable! That totally sucks. In my opinion the old behaviour of iOS 7.0 was buggy since PhysicsBodyies didn't scale with the sprite.

Since iOS7.1, PhysicsBodies apparently adapt their size to the sprite's scale. I'm not 100% sure yet if this is in fact so, but I'll find out soon enough. Just downloading the Xcode update, I'll let you know after I've done some testing.