Reputation: 345
I had a general SKScene where I created a SKSpriteNode, added it via
[self addChild:_charecter];
And all was well..
I tried making a new class for the SKSpriteNode so that I could add some custom methods to it. Here is it's init method:
- (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;
}
The problem is that now when I add the _charecter to the view, it doesn't appear. I'm adding him in the SKScene as follows:
_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];
Any idea why my SKSpriteNode isn't visible? Thanks
Upvotes: 1
Views: 1374
Reputation: 1304
Trying using one of the SKSpriteNode initializers instead of 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;
}
From what I understand, when you use initWithImageNamed:
, the sprite node's size is set to the texture's size. Your sprite could of been on screen but had a size of (0,0). Let us know if this works!
On a side note, check out Ray's tutorial on texture atlases to replace _characterSprites
http://www.raywenderlich.com/45152/sprite-kit-tutorial-animations-and-texture-atlases
Upvotes: 1