2014-06-05 4 views
0

게임을 코딩하려고합니다. 나는 물건을 가지고있다. 그것은 뛰어 올라서 움직일 수있다. 점프하면서 '달리기'의 애니메이션을 잡고 싶지만 슬라이딩하면서 이미지를 변경하고 싶습니다. 내 문제 : 이미지가 '슬라이드 7'로 변경되는 경우 이미지가 표시되지 않습니다. 아무 반응이 없습니다. 슬라이드 애니메이션은 실행 애니메이션으로 다시 이동하는 것보다 약 4 초 동안 만 나타나야합니다. 어떤 제안?SpriteKit 변경 스 와이프 제스처 후 이미지

내 코드 :

-(void)Mensch{ 

SKTexture * MenschTexture1 = [SKTexture textureWithImageNamed:@"Mensch1"]; 
MenschTexture1.filteringMode = SKTextureFilteringNearest; 
SKTexture * MenschTexture2 = [SKTexture textureWithImageNamed:@"Mensch2"]; 
MenschTexture2.filteringMode = SKTextureFilteringNearest; 

SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[MenschTexture1, MenschTexture2] timePerFrame:0.4]]; 

Mensch = [SKSpriteNode spriteNodeWithTexture:MenschTexture1]; 
Mensch.size = CGSizeMake(45, 45); 
Mensch.position = CGPointMake(self.frame.size.width/5, Boden.position.y + 73); 
Mensch.zPosition = 2; 

Mensch.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Mensch.size]; 
Mensch.physicsBody.dynamic = YES; 
Mensch.physicsBody.allowsRotation = NO; 
Mensch.physicsBody.usesPreciseCollisionDetection = YES; 
Mensch.physicsBody.restitution = 0; 
Mensch.physicsBody.velocity = CGVectorMake(0, 0); 

[Mensch runAction:Run]; 
[self addChild:Mensch]; 

} 


- (void)didMoveToView:(SKView *)view 
{ 
UISwipeGestureRecognizer *recognizerDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeDown:)]; 
recognizerDown.direction = UISwipeGestureRecognizerDirectionDown; 
[[self view] addGestureRecognizer:recognizerDown]; 
} 


-(void)handleSwipeDown:(UISwipeGestureRecognizer *)sender 
{ 

[Mensch removeAllActions]; 

Mensch = [SKSpriteNode spriteNodeWithImageNamed:@"slide7.png"]; 

NSLog(@"Slide"); 

} 

답변

0

당신은 Mensch 객체를 대체하고있다. 더 구체적으로 말하면 포인터을 개체로 바꾸는 것이지만 장면에 표시되는 것을 중지하지는 않습니다.

다음 중 하나를 수행 할 수 있습니다

  • 즉시 표시해야 스프라이트, 내부 SKTexture를 교체합니다. 즉, Mensch.texture = [SKTexture textureWithImageNamed:@"slide7.png"];
  • 스프라이트를 교체하십시오. 현재 스프라이트 ([sprite removeFromParent])를 제거하고 새 스프라이트 ([self addChild:newSprite])를 추가해야합니다.

Mensch 개체를 다시 만들 필요가 없기 때문에 첫 번째 것이 필요하다고 생각합니다.

이 개체에서 많은 Mensch 관련 논리를 수행하고 있다고 덧붙이시겠습니까? 이 생성 코드를 MFC의 SKSpriteNode 서브 클래스로 옮기는 것이 Object Oriented 관점에서 더 나을 것입니다.

@interface Mensch : SKSpriteNode 

- (void) displayRunAnimation; 
- (void) displaySlideAnimation; 

@end 

@implementation : Mensch 

- (instancetype) init { 
    self = [super init]; 
    if (self) { 
    // your initialization code here, including physics body setup 

    [self displayRunAnimation]; 
    } 
    return self; 
} 

- (void) displayRunAnimation { 
    SKTexture * MenschTexture1 = [SKTexture textureWithImageNamed:@"Mensch1"]; 
    MenschTexture1.filteringMode = SKTextureFilteringNearest; 
    SKTexture * MenschTexture2 = [SKTexture textureWithImageNamed:@"Mensch2"]; 
    MenschTexture2.filteringMode = SKTextureFilteringNearest; 

    SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[MenschTexture1, MenschTexture2] timePerFrame:0.4]]; 
    // if you plan to call this often, you want to cache this SKAction, since creating it over and over is a waste of resources 

    [self runAction:Run]; 
} 

- (void) displaySlideAnimation { 
    [self removeAllActions]; 
    self.texture = [SKTexture textureWithImageNamed:@"slide7.png"]; 
    NSLog(@"Slide"); 

    // re-start the runAnimation after a time interval 
    [self performSelector:@selector(displayRunAnimation) withObject:nil afterDelay:4.0]; 
} 
+0

감사합니다. 괜찮습니다.하지만 실행 애니메이션으로 다시 들어갈 때까지 약 4 초 동안 새 슬라이드 텍스처를 표시하고 싶습니다. 어떻게 코딩 할 수 있습니까? –

+0

자, displaySlideAnimation 메소드 ('performSelector : @selector (displayRunAnimation)')에 호출을 추가했습니다. – CloakedEddy

+0

정말 고마워요! –

관련 문제