Reputation: 2281
I have a game using sprite kit and objective-C originally developed using Xcode 6 and iOS 7, but it now crashes on iOS 9 devices.
The crash occurs when the user is progressing from one level to another and a new horizontal scrolling background (SKNode
) is added to the scene.
Exception:
Thread 1 EXC_BAD_ACCESS (code=1, address=0x0)
Adding the horizontal scrolling background to the scene when changing level:
// Array of planets to scroll
NSArray *parallaxBackgroundNames = @[@"bg_galaxy.png", @"bg_planetsunrise.png",
@"bg_spacialanomaly.png", @"bg_spacialanomaly2.png"];
CGSize planetSizes = CGSizeMake(200.0, 200.0);
// Initialize new back round scrolling node and ad to scene
_parallaxNodeBackgrounds = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackgroundNames
size:planetSizes
pointsPerSecondSpeed:10.0];
// Position in center of screen
_parallaxNodeBackgrounds.position = CGPointMake(self.size.width/2.0, self.size.height/2.0);
// Method to randomly place planets as offsets to center of screen
[_parallaxNodeBackgrounds randomizeNodesPositions];
// EXCEPTION THROWN HERE when adding it to the layer
[self addChild:_parallaxNodeBackgrounds];
The _parallaxNodeBackgounds
is an SKSNode
instance of FMMParallaxNode
initialized as:
- (instancetype)initWithBackgrounds:(NSArray *)files size:(CGSize)size pointsPerSecondSpeed:(float)pointsPerSecondSpeed
{
if (self = [super init])
{
_pointsPerSecondSpeed = pointsPerSecondSpeed;
_numberOfImagesForBackground = [files count];
_backgrounds = [NSMutableArray arrayWithCapacity:_numberOfImagesForBackground];
_randomizeDuringRollover = NO;
[files enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
SKSpriteNode *node = [SKSpriteNode spriteNodeWithImageNamed:obj];
node.size = size;
node.anchorPoint = CGPointZero;
node.position = CGPointMake(size.width * idx, 0.0);
node.name = @"background";
//NSLog(@"node.position = x=%f,y=%f",node.position.x,node.position.y);
[_backgrounds addObject:node];
[self addChild:node];
}];
}
return self;
}
Any input as to why this crash happens is appreciated.
EDIT: My understanding of this exception is that it is thrown if a pointer is pointing to memory which has been deallocated or the pointer is corrupt.
There are no errors associated with this object when I build and analyse in Xcode and I have also created a new pointer locally (the original pointer is a instance var) but still get the same error.
Upvotes: 0
Views: 128
Reputation: 16827
Do a weak reference like this:
- (instancetype)initWithBackgrounds:(NSArray *)files size:(CGSize)size pointsPerSecondSpeed:(float)pointsPerSecondSpeed
{
if (self = [super init])
{
__weak FMMParallaxNode *weakSelf = self;
_pointsPerSecondSpeed = pointsPerSecondSpeed;
_numberOfImagesForBackground = [files count];
_backgrounds = [NSMutableArray arrayWithCapacity:_numberOfImagesForBackground];
_randomizeDuringRollover = NO;
[files enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
SKSpriteNode *node = [SKSpriteNode spriteNodeWithImageNamed:obj];
node.size = size;
node.anchorPoint = CGPointZero;
node.position = CGPointMake(size.width * idx, 0.0);
node.name = @"background";
//NSLog(@"node.position = x=%f,y=%f",node.position.x,node.position.y);
[_backgrounds addObject:node];
[weakSelf addChild:node];
}];
}
return self;
}
Upvotes: 1