SKSpriteNode pools in iOS 8 seem to be allocated t

2019-02-18 02:53发布

问题:

I might be missing something. But my current app on the appstore works in iOS 7, but in iOS 8 completely fails because it won't create a preallocated pool of sprites. They appear to be written to the same address unless the sprites have specifically different properties.

In iOS 7 the following code produces a set with 4 unique objects. In iOS 8, the same code produces a set with only 1 object:

  NSMutableSet *aSet = [NSMutableSet set];
  SKColor *sameColor = [SKColor redColor];
  CGSize sameSize = CGSizeMake(10, 10);

  for (int i = 0; i < 4; i++) {

      //allocate a brand new sprite
      SKSpriteNode *thisSprite1 = [[SKSpriteNode alloc] initWithColor:sameColor size:sameSize];

      [aSet addObject:thisSprite1];

  }

NSLog(@"aSet Count: %i", aSet.count);

iOS8 Result:

2014-09-09 15:06:43.065 MSM[383:27490] aSet Count: 1

Am I going crazy? Amazingly, pretty much my entire app is based on this code concept repeated over and over again. If I do the same thing, but use something like NSObject, then the problem goes away, so it appears to be a new change to SKSprite. I know I can work around it with some crazy stuff, but is a huge pain, since I shouldn't have to do that, and I was hoping to avoid another version submission.

回答1:

Thanks to Josh for the direction on how to solve this new bump in the road.

I subclassed SKSpriteNode, overriding -isEqual and -hash, to both be what my best guess at the NSObject implementation is. Then just did a Find/Replace All in Project for "SKSpriteNode" for my subclass name, and all is back to it was in the iOS 7 build:

-(BOOL)isEqual:(id)object{

    return self == object;
}

- (NSUInteger)hash
{
    return (NSUInteger)self;
}