2014-10-27 70 views
0

Heyo夥計們,在嘗試在SpriteKit中創建子類節點時出現了一個奇怪的問題。我在編譯時沒有遇到任何錯誤,並且應用程序一直運行到節點需要添加爲場景的子節點,然後失敗並說reason: 'Attemped to add nil node to parent:我不確定我錯過了什麼,感謝任何幫助!下面是我的節點標題:Spritekit Subclassed節點顯示爲零

#import <SpriteKit/SpriteKit.h> 

@interface Ships : SKSpriteNode 

-(Ships *)initWithImageNamed: (NSString *)imageName; 

@end 

,這裏是實現:

#import "Ships.h" 

@implementation Ships 

-(Ships *)initWithImageNamed: (NSString *)imageName 
{ 
self = [Ships spriteNodeWithImageNamed:imageName]; 

// Adds more accurate physics body for ship collisions 
CGFloat offsetX = (self.frame.size.width * 1.2) * self.anchorPoint.x; 
CGFloat offsetY = (self.frame.size.height * 1.2) * self.anchorPoint.y; 

CGMutablePathRef path = CGPathCreateMutable(); 

CGPathMoveToPoint(path, NULL, 8 - offsetX, 30 - offsetY); 
CGPathAddLineToPoint(path, NULL, 7 - offsetX, 22 - offsetY); 
CGPathAddLineToPoint(path, NULL, 50 - offsetX, 2 - offsetY); 
CGPathAddLineToPoint(path, NULL, 65 - offsetX, 7 - offsetY); 
CGPathAddLineToPoint(path, NULL, 70 - offsetX, 8 - offsetY); 
CGPathAddLineToPoint(path, NULL, 27 - offsetX, 31 - offsetY); 

CGPathCloseSubpath(path); 

self.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path]; 
self.physicsBody.dynamic = NO; 
self.physicsBody.restitution = 0.0f; 
self.physicsBody.friction = 0.0f; 
self.physicsBody.linearDamping = 1.0f; 
self.physicsBody.allowsRotation = NO; 
self.physicsBody.usesPreciseCollisionDetection = YES; 

// Keeps player ship on top of all other objects(unless other objects are assigned greater z position 
self.zPosition = 100.0f; 

return self; 
} 

@end 
在我的場景

然後,我在interface與申報playerNodeShips *playerNode;,這裏是試圖實現節點的方法:

-(void) createPlayerNode 
{ 
playerNode = [playerNode initWithImageNamed:@"Nova-L1"]; 
playerNode.position = CGPointMake(self.frame.size.width/5, self.frame.size.height/2); 
playerNode.physicsBody.categoryBitMask = CollisionCategoryPlayer; 
playerNode.physicsBody.collisionBitMask = 0; 
playerNode.physicsBody.contactTestBitMask = CollisionCategoryBottom | CollisionCategoryObject | CollisionCategoryScore | CollisionCategoryPup; 

[self addChild:playerNode]; 
} 

我在應用程序的其他地方打電話[self createPlayerNode];,一旦我崩潰了。任何幫助是極大的讚賞!另外,我在我的iPhone 5

回答

2

船舶的初始化運行是一個實例方法不是類的方法,所以你分配/初始化語句應該

playerNode = [[Ships alloc] initWithImageNamed:@"Nova-L1"]]; 

和Ships.m,更換

self = [Ships spriteNodeWithImageNamed:imageName]; 

與此

self = [super initWithImageNamed:imageName]; 

另外,你應該創建to後釋放路徑他物理體...

ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path]; 

    CGPathRelease(path); 

或者,你可以在Ships.m中聲明一個方便的方法來在一個步驟中分配/ init。

編輯:在Ships.m

聲明類方法

+ (Ships *)shipNodeWithImageNamed:(NSString *)imageName 
{ 
    return [[Ships alloc] initWithImageNamed:imageName]; 
} 

Add方法原型Ships.h

+ (Ships *)shipNodeWithImageNamed:(NSString *)imageName; 

創建一個新的節點船
playerNode = [Ships shipNodeWithImageNamed:@"Nova-L1"]; 
+0

太棒了!感謝您的信息,並抓住路徑發佈的事情。在我的船舶實施中聲明一個方便的方法會是什麼樣子?我們是否在談論工廠方法?所以我會添加如下內容: '+(id)船舶{[[self alloc] init] autorelease]; }' – 2014-10-27 23:30:42

+0

請參閱我編輯的答案。在ARC模式下不需要autorelease。 – 0x141E 2014-10-28 06:07:05

+0

我不認爲我看到你的編輯,有什麼改變? – 2014-10-28 12:54:12