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
與申報playerNode
:Ships *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
太棒了!感謝您的信息,並抓住路徑發佈的事情。在我的船舶實施中聲明一個方便的方法會是什麼樣子?我們是否在談論工廠方法?所以我會添加如下內容: '+(id)船舶{[[self alloc] init] autorelease]; }' – 2014-10-27 23:30:42
請參閱我編輯的答案。在ARC模式下不需要autorelease。 – 0x141E 2014-10-28 06:07:05
我不認爲我看到你的編輯,有什麼改變? – 2014-10-28 12:54:12