![](/img/trans.png)
[英]SpriteKit/Obj-C -Restarting game in touches began with nodesatPoint
[英]SpriteKit Obj C game crashes in iOS 9
我有一個使用Sprite Kit和Objective-C的游戲,該游戲最初是使用Xcode 6和iOS 7開發的,但現在在iOS 9設備上崩潰。
當用戶從一個級別前進到另一個級別並將新的水平滾動背景( SKNode
)添加到場景時,發生崩潰。
例外:
線程1 EXC_BAD_ACCESS(代碼= 1,地址= 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
是SKSNode
實例, FMMParallaxNode
初始化為:
- (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中進行構建和分析時,沒有與該對象相關的錯誤,並且我還在本地創建了一個新的指針(原始指針是一個實例var),但仍然遇到相同的錯誤。
像這樣做一個弱引用:
- (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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.