Ios Spritekit子类节点显示为nil
大家好,在SpriteKit中创建子类节点时遇到了一个奇怪的问题。编译时我没有收到任何错误,应用程序一直运行,直到我到达需要将节点作为场景的子节点添加的位置,然后失败并说Ios Spritekit子类节点显示为nil,ios,objective-c,sprite-kit,subclassing,skspritenode,Ios,Objective C,Sprite Kit,Subclassing,Skspritenode,大家好,在SpriteKit中创建子类节点时遇到了一个奇怪的问题。编译时我没有收到任何错误,应用程序一直运行,直到我到达需要将节点作为场景的子节点添加的位置,然后失败并说原因:'尝试将零节点添加到父节点:我不确定我缺少了什么,非常感谢您的帮助!以下是节点的标题: #import <SpriteKit/SpriteKit.h> @interface Ships : SKSpriteNode -(Ships *)initWithImageNamed: (NSString *)imag
原因:'尝试将零节点添加到父节点:
我不确定我缺少了什么,非常感谢您的帮助!以下是节点的标题:
#import <SpriteKit/SpriteKit.h>
@interface Ships : SKSpriteNode
-(Ships *)initWithImageNamed: (NSString *)imageName;
@end
-(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];
}
然后在我的场景中,我在界面中声明playerNode
,使用:Ships*playerNode代码>下面是尝试实现节点的方法:
#import <SpriteKit/SpriteKit.h>
@interface Ships : SKSpriteNode
-(Ships *)initWithImageNamed: (NSString *)imageName;
@end
-(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]代码>应用程序中的其他位置,一旦我这样做…崩溃。非常感谢您的帮助!另外,我在iphone5上运行Ships的初始值设定项是一个实例方法而不是类方法,因此您的alloc/init语句应该是
playerNode = [[Ships alloc] initWithImageNamed:@"Nova-L1"]];
在船上。m,替换
self = [Ships spriteNodeWithImageNamed:imageName];
用这个
self = [super initWithImageNamed:imageName];
此外,您应该在创建物理实体后释放路径
ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
CGPathRelease(path);
或者,您可以在Ships.m中通过一个步骤将一个方便的方法声明为alloc/init
编辑:
在Ships.m中声明类方法
+ (Ships *)shipNodeWithImageNamed:(NSString *)imageName
{
return [[Ships alloc] initWithImageNamed:imageName];
}
将方法原型添加到Ships.h
+ (Ships *)shipNodeWithImageNamed:(NSString *)imageName;
并创建一个新的ship节点
playerNode = [Ships shipNodeWithImageNamed:@"Nova-L1"];
令人惊叹的!感谢您提供的信息,并获取路径发布内容。在我的ships实现中声明一个方便的方法会是什么样子?我们的谈话是否像工厂方法?所以我会添加一些类似于+(id)ships{return[[self alloc]init]autorelease];}
非常酷。我倾向于在我的头脑中把事情过于复杂化。非常感谢你的帮助!