【问题标题】:SpriteKit change Image after swipe gesture滑动手势后 SpriteKit 更改图像
【发布时间】:2014-06-05 13:23:00
【问题描述】:

我正在尝试编写游戏代码。我有一个可以跳跃和滑动的物体。 我想在跳跃时保持“奔跑”的动画,但在滑动时,我想改变图像。我的问题:如果我只是将图像更改为“slide7”,图像不会炫耀。 没发生什么事。 幻灯片动画应该只出现大约 4 秒,然后再次进入运行动画。有什么建议吗?

我的代码:

-(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");

}

【问题讨论】:

    标签: ios sprite-kit skaction


    【解决方案1】:

    您正在替换 Mensch 对象。更具体地说,您正在替换对象的 指针,但这并不能阻止它在场景中显示。

    您可以:

    • 替换精灵内部的SKTexture,它应该立即显示。这意味着Mensch.texture = [SKTexture textureWithImageNamed:@"slide7.png"];
    • 更换精灵。这需要删除当前精灵 ([sprite removeFromParent]) 并添加新精灵 ([self addChild:newSprite])。

    我认为您需要第一个,因为您不必重新创建 Mensch 对象。

    我可以补充一下,你在这个对象中执行了很多 Mensch 特定的逻辑吗?将这个创建代码移动到 SKSpriteNode 子类 Mensch 会更好(从面向对象的角度来看)。

    @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];
    }
    

    【讨论】:

    • 好的,谢谢,但我想显示新的幻灯片纹理大约 4 秒,直到它再次进入运行动画。我该如何编码?
    • 好的,我在 displaySlideAnimation 方法中添加了一个调用 (performSelector:@selector(displayRunAnimation).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多