SpriteKit添加孩子不起作用



我有一个一般的skscene,我在其中创建了一个skspritenode,通过

添加了它
[self addChild:_charecter];

一切都很好..

我尝试为SKSpriteNode制作新类,以便可以在其中添加一些自定义方法。这是Init方法:

- (instancetype)init
{
  if (self = [super init]) {
    SKTexture *run1 = [SKTexture textureWithImageNamed:@"run1"];
    SKTexture *run2 = [SKTexture textureWithImageNamed:@"run2"];
    SKTexture *run3 = [SKTexture textureWithImageNamed:@"run3"];
    _charecterSprites = [NSArray arrayWithObjects:run1, run2, run3, nil];
    self.texture = run1;
    self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1.0]; //CHANGE THIS LATER
  }
  return self;
}

问题是,现在当我将_Charecter添加到视图中时,它不会出现。我将他添加到Skscene中,如下所示:

_charecter = [[RUNPlayer alloc] init];
_charecter.position = CGPointMake(CGRectGetMidX(self.frame), CHARECTER_Y_AXIS);
self.physicsWorld.gravity = CGVectorMake(0, -10.0/150.0);
[self addChild:_charecter];

知道为什么我的SKSpritenode不可见吗?谢谢

尝试使用一个SKSpriteNode初始化器而不是init

- (instancetype)initWithImageNamed:(NSString *)name
{
  if (self = [super initWithImageNamed:name]) {
    SKTexture *run1 = [SKTexture textureWithImageNamed:@"run1"];
    SKTexture *run2 = [SKTexture textureWithImageNamed:@"run2"];
    SKTexture *run3 = [SKTexture textureWithImageNamed:@"run3"];
    _charecterSprites = [NSArray arrayWithObjects:run1, run2, run3, nil];
    self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1.0]; //CHANGE THIS LATER
  }
  return self;
}

据我了解,当您使用initWithImageNamed:时,Sprite节点的大小设置为纹理的大小。您的精灵可能在屏幕上,但大小为(0,0)。让我们知道这是否有效!

在旁注上,请查看Ray关于纹理地图集的教程,以替换_characterSprites http://www.rayrewenderlich.com/45152/sprite-kit-kit-kit-tutorial-animation-animation-and-animations-and-texture-atlases->

最新更新