【发布时间】:2015-09-09 15:16:21
【问题描述】:
我想通过子类生成精灵节点以进行屏幕显示,但它没有显示在屏幕上。有人知道我做错了什么吗?
子类
@implementation Seagull
-(id)init
{
self = [super init];
if (self) {
_atlas = [SKTextureAtlas atlasNamed:@"Seagull"];
_seagull = [SKSpriteNode spriteNodeWithTexture:[_atlas textureNamed:@"Seagull1"]];
_seagull.size = CGSizeMake(156.8, 115.4);
NSArray *flyFrames = @[[_atlas textureNamed:@"Seagull1"],
[_atlas textureNamed:@"Seagull2"]];
_flyAnimation = [SKAction repeatActionForever:[SKAction animateWithTextures:flyFrames timePerFrame:0.15 resize:NO restore:NO]];
[_seagull runAction:_flyAnimation];
}
return self;
}
@end
已创建子类对象
-(Seagull *)spawnSeagull
{
Seagull *seaGull = [[Seagull alloc] init];
seaGull.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5);
NSLog(@"seagull postion.x = %f && position.y = %f", seaGull.position.x, seaGull.position.y);
[self addChild:seaGull];
return seaGull;
}
在 viewDidLoad 中添加到场景中
[self spawnSeagull];
【问题讨论】:
-
您在
spawnSeagull中返回了一只海鸥,但您没有表明它被分配给[self spawnSeagull];中的任何对象。你的意思是写_seagull = [self spawnSeagull];? -
您的代码是否出错?因为我猜在您的 GameScene 中,没有声明名为
spawnSeagull的方法。 -
@timgcarlson 嗯。没有分配给任何对象是什么意思?我是否需要在 GameScene 中创建一个 SKSpriteNode 对象并将其分配给 spawnSeagull 或 Didmovetoview 中的 Seagull 类对象?
-
@WangYudong spawnSeagull 在游戏场景中被声明是的
标签: ios objective-c sprite-kit init skspritenode