0

我正在尝试编写游戏代码。我有一个可以跳跃和滑动的物体。我想在跳跃时保持“奔跑”的动画,但在滑动时,我想改变图像。我的问题:如果我只是将图像更改为“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");

}
4

1 回答 1

0

您正在替换对象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];
}
于 2014-06-06T08:46:44.153 回答