繁体   English   中英

SpriteKit为一个节点更改SKAction

[英]SpriteKit changing SKAction for one Node

我尝试让节点在特定的系统时间后更快地移动。 例如,在30秒后,节点移动得更快。 在我的项目中,BOOL Faster是正确的。 使用我的代码,节点将移动得更快,但是已经产生的节点仍使用旧的SKAction。 因此,如果有2个节点的移动速度比所产生的节点的移动速度慢,则这看起来很奇怪。 我该如何解决?

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {

..... 

SKAction * Spawn = [SKAction performSelector:@selector(Enemy) onTarget:self];
SKAction * Delay = [SKAction waitForDuration:1.5];
SKAction * SpawnThenDelay = [SKAction sequence:@[Spawn, Delay]];
SKAction * SpawnThenDelayForever = [SKAction repeatActionForever:SpawnThenDelay];
[self runAction:SpawnThenDelayForever];

.....
}

-(void)Enemy {

switch (arc4random_uniform(4)){

    case 0:
        if(Day){

            SKTexture * MenschSTexture1 = [SKTexture textureWithImageNamed:@"MenschSchwert1"];
            MenschSTexture1.filteringMode = SKTextureFilteringNearest;
            SKTexture * MenschSTexture2 = [SKTexture textureWithImageNamed:@"MenschSchwert2"];
            MenschSTexture2.filteringMode = SKTextureFilteringNearest;

            SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[MenschSTexture1, MenschSTexture2] timePerFrame:0.25]];

            MenschSchwert = [SKSpriteNode spriteNodeWithTexture:MenschSTexture1];
            MenschSchwert.size = CGSizeMake(50, 50);
            MenschSchwert.zPosition = 2;

            MenschSchwert.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(15, 40)];
            MenschSchwert.physicsBody.dynamic = NO;
            MenschSchwert.physicsBody.allowsRotation = NO;
            MenschSchwert.physicsBody.usesPreciseCollisionDetection = YES;
            MenschSchwert.physicsBody.restitution = 0;
            MenschSchwert.physicsBody.velocity = CGVectorMake(0, 0);

            [MenschSchwert runAction:Run];

            MenschSchwert.physicsBody.categoryBitMask = HindernissCategory;
            MenschSchwert.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
            MenschSchwert.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;

            Feind = MenschSchwert;

            Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 75);
        }

        if(Night){

            Zombie = [SKSpriteNode spriteNodeWithImageNamed:@"Zombie.png"];
            Zombie.size = CGSizeMake(50, 50);
            Zombie.zPosition = 2;

            Zombie.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 40)];
            Zombie.physicsBody.dynamic = NO;
            Zombie.physicsBody.allowsRotation = NO;
            Zombie.physicsBody.usesPreciseCollisionDetection = YES;
            Zombie.physicsBody.restitution = 0;
            Zombie.physicsBody.velocity = CGVectorMake(0, 0);

            Zombie.physicsBody.categoryBitMask = HindernissCategory;
            Zombie.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
            Zombie.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;

            Feind = Zombie;

            Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 75);
        }
        break;
    case 1:

        Steine = [SKSpriteNode spriteNodeWithImageNamed:@"Stein.png"];
        Steine.size = CGSizeMake(50, 55);
        Steine.zPosition = 2;

        Steine.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
        Steine.physicsBody.dynamic = NO;
        Steine.physicsBody.allowsRotation = NO;
        Steine.physicsBody.usesPreciseCollisionDetection = YES;
        Steine.physicsBody.restitution = 0;

        Steine.physicsBody.categoryBitMask = HindernissCategory;
        Steine.physicsBody.collisionBitMask = BodenCategory | MenschCategory;
        Steine.physicsBody.contactTestBitMask = BodenCategory | MenschCategory;

        Feind = Steine;

        Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 69);
        break;
    case 2:

        Block = [SKSpriteNode spriteNodeWithImageNamed:@"Block.png"];
        Block.size = CGSizeMake(40, 35);
        Block.zPosition = 2;

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

        Block.physicsBody.categoryBitMask = LuftHindernissCategory;
        Block.physicsBody.collisionBitMask = MenschCategory;
        Block.physicsBody.contactTestBitMask = MenschCategory;

        Feind = Block;

        Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 110);
        break;
    case 3:

        Stachel = [SKSpriteNode spriteNodeWithImageNamed:@"Stachel.png"];
        Stachel.size = CGSizeMake(60, 60);
        Stachel.zPosition = 2;

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

        Stachel.physicsBody.categoryBitMask = LuftHindernissCategory;
        Stachel.physicsBody.collisionBitMask = MenschCategory;
        Stachel.physicsBody.contactTestBitMask = MenschCategory;

        Feind = Stachel;

        Feind.position = CGPointMake(self.frame.size.width * 1.25, Boden.position.y + 100);
        break;
    default:
        break;

}

if(Faster){

actionMove2 = [SKAction moveToX:-100 duration:4];
actionMoveDone2 = [SKAction removeFromParent];

[Feind runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove2,actionMoveDone2]]]];

}

else{
actionMove = [SKAction moveToX:-100 duration:5];
actionMoveDone = [SKAction removeFromParent];

[Feind runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove,actionMoveDone]]]];
}

[self addChild:Feind];

}

将您的if语句替换为以下代码...

if (Faster) {
    [self addActionToNode:Feind withDuration:4 andKey:@"faster"];
}
else {
    [self addActionToNode:Feind withDuration:5 andKey:@"slower"];
}

此方法将操作(带有键)添加到节点。 该键使您可以找到速度较慢的节点,以便在游戏加速时可以对其进行更新。

- (void) addActionToNode:(SKSpriteNode *)node withDuration:(NSTimeInterval)duration andKey:(NSString *)key
{
    SKAction *move = [SKAction moveToX:-100 duration:duration];
    SKAction *remove = [SKAction removeFromParent];
    SKAction *action = [SKAction repeatActionForever:[SKAction sequence:@[move,remove]]];

    [node runAction:action withKey:key];

    if (!node.parent) {
        [self addChild:node];
    }
}

当游戏加速时,调用此命令更新较慢的节点...

- (void) updateActions
{
    for (SKSpriteNode *node in self.children) {
        if ([node actionForKey:@"slower"]) {
            [node removeAllActions];
            [self addActionToNode:node withDuration:4 andKey:@"faster"];
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM