SpriteKit添加Child不工作

时间:2014-03-09 01:14:13

标签: ios objective-c sprite-kit

我有一个普通的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不可见? 感谢

1 个答案:

答案 0 :(得分:1)

尝试使用其中一个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:时,精灵节点的大小设置为纹理的大小。你的精灵可能在屏幕上,但大小为(0,0)。如果有效,请告诉我们!

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