我有一个使用雪碧工具包和objective的游戏,最初使用Xcode 6和iOS 7开发,但现在它在iOS 9设备上崩溃了。
当用户从一个级别升级到另一个级别,并且添加一个新的水平滚动背景(SKNode)到场景时,就会发生崩溃。
例外:
线程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中构建和分析时,没有与此对象相关的错误,而且我还在本地创建了一个新指针(原始指针是一个实例变量),但仍然会得到相同的错误。
发布于 2016-02-11 00:10:26
做一个像这样的弱引用:
- (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;
}https://stackoverflow.com/questions/35300624
复制相似问题