首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpriteKit Obj C游戏在iOS 9中崩溃

SpriteKit Obj C游戏在iOS 9中崩溃
EN

Stack Overflow用户
提问于 2016-02-09 19:30:10
回答 1查看 128关注 0票数 0

我有一个使用雪碧工具包和objective的游戏,最初使用Xcode 6和iOS 7开发,但现在它在iOS 9设备上崩溃了。

当用户从一个级别升级到另一个级别,并且添加一个新的水平滚动背景(SKNode)到场景时,就会发生崩溃。

例外:

线程1 EXC_BAD_ACCESS (code=1,address=0x0)

更改级别时,将水平滚动背景添加到场景中:

代码语言:javascript
复制
// 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是初始化为:FMMParallaxNodeSKSNode实例:

代码语言:javascript
复制
- (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中构建和分析时,没有与此对象相关的错误,而且我还在本地创建了一个新指针(原始指针是一个实例变量),但仍然会得到相同的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-11 00:10:26

做一个像这样的弱引用:

代码语言:javascript
复制
- (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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35300624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档