SpriteKit Obj C 游戏在 iOS 9 时崩溃
SpriteKit Obj C game crashes in iOS 9
我有一个使用 sprite kit 和 objective-C 最初使用 Xcode 6 和 iOS 7 开发的游戏,但它现在在 iOS 9 设备上崩溃。
当用户从一个级别前进到另一个级别并向场景添加新的水平滚动背景 (SKNode
) 时,会发生崩溃。
异常:
Thread 1 EXC_BAD_ACCESS (code=1, address=0x0)
在改变级别时将水平滚动背景添加到场景中:
// 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];
_parallaxNodeBackgounds
是 FMMParallaxNode
的 SKSNode
实例,初始化为:
- (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;
}
感谢任何关于为什么会发生此崩溃的意见。
编辑:
我对这个异常的理解是,如果指针指向已被释放的内存或指针已损坏,则会抛出该异常。
当我在 Xcode 中构建和分析时,没有与此对象相关的错误,并且我还在本地创建了一个新指针(原始指针是一个实例变量),但仍然出现相同的错误。
像这样做一个弱引用:
- (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;
}
我有一个使用 sprite kit 和 objective-C 最初使用 Xcode 6 和 iOS 7 开发的游戏,但它现在在 iOS 9 设备上崩溃。
当用户从一个级别前进到另一个级别并向场景添加新的水平滚动背景 (SKNode
) 时,会发生崩溃。
异常:
Thread 1 EXC_BAD_ACCESS (code=1, address=0x0)
在改变级别时将水平滚动背景添加到场景中:
// 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];
_parallaxNodeBackgounds
是 FMMParallaxNode
的 SKSNode
实例,初始化为:
- (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;
}
感谢任何关于为什么会发生此崩溃的意见。
编辑: 我对这个异常的理解是,如果指针指向已被释放的内存或指针已损坏,则会抛出该异常。
当我在 Xcode 中构建和分析时,没有与此对象相关的错误,并且我还在本地创建了一个新指针(原始指针是一个实例变量),但仍然出现相同的错误。
像这样做一个弱引用:
- (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;
}