我正在尝试编写游戏代码。我有一个可以跳跃和滑动的物体。 我想要保持' run'虽然跳跃,但在滑动时,我想改变图像。我的问题:如果我只是将图像更改为' 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");
}
答案 0 :(得分:0)
您正在替换 Mensch
个对象。更具体地说,您正在替换对象的指针,但这并不能阻止它在场景中显示。
你可以:
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];
}