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 に答える