하위 클래스별로 스크리닝 할 스프라이트 노드를 스폰하고 싶지만 화면에 표시되지 않습니다. 누군가 내가 뭘 잘못했는지 알아?
아강
@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];
클래스 (Seagull) 에서 속성 SKSpriteNode
(_seagull)을 만들 때 실수를했습니다 . SKSpriteNode
당신에 init
방법, 당신은 초기화 _seagull
int로서 SKSpriteNode
,하지만 산란 갈매기에, 당신이 할 모든 방금 생성되며, 클래스의 인스턴스를 추가 할 때 Seagull
장면에, 아무 상관 없습니다 _seagull
실제로 갈매기의 질감을 포함한다. 이 문제를 해결하려면, 당신은 반환해야 seaGull.seagull
에 spawnSeagull
난 두려워 가장 좋은 방법이 아닙니다한다.
그러나 코드에서 수정해야 할 부분이 여전히 몇 가지 있습니다.
에서 spawnSeagull
:
CGPointMake(self.size.width * 0.5, self.size.height * 0.5)
이런 식으로 장면의 절반 크기를 얻지 못하기 때문에 잘못되었습니다.[self addChild:seaGull]
당신의 GameScene 이 아닌의 서브 클래스로, 장면에 추가 할 수 있기 때문에 SKSpriteNode
.에서 viewDidLoad
( didMoveToView
권장) :
spawnSeagull
.아래에 전체 코드를 추가합니다.
제거 init
하고 하위 클래스에 클래스 메서드를 추가합니다.
+ (Seagull *)spawnSeagull
{
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"Seagull"];
Seagull *seagull = [Seagull spriteNodeWithTexture:[atlas textureNamed:@"Seagull1"]];
// seagull.size = CGSizeMake(156.8, 115.4);
// May be set scale of seagull is better? like:
seagull.scale = 2.0;
NSArray *flyFrames = @[[atlas textureNamed:@"Seagull1"],
[atlas textureNamed:@"Seagull2"]];
SKAction *flyAnimation = [SKAction repeatActionForever:[SKAction animateWithTextures:flyFrames timePerFrame:0.15 resize:NO restore:NO]];
[seagull runAction:flyAnimation];
return seagull;
}
GameScene 에서 클래스 메서드를 호출합니다 .
- (void)didMoveToView:(SKView *)view
{
Seagull *seagull = [Seagull spawnSeagull];
seagull.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:seagull];
}
이 Apple 문서 에서 더 많은 샘플 코드를 찾아보십시오 shipSprite
. 생성 방법 이 도움 이 될 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다