简体   繁体   中英

SpriteKit changing SKAction for one Node

I try to let the node moving faster after a specific System time. For example, after 30 seconds, the nodes move faster. In my Project it goes after the the BOOL Faster is true. With my Code, the nodes will move faster, but the ones, that already spawned, still using the old SKAction. So it looks weird, if there are 2 Nodes moving slower than the Node that is spawned. How can I fix that?

-(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];

}

Replace your if statement with the following code...

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

This method adds an action (with a key) to a node. The key allows you to find the slower nodes so they can be updated when the game speeds up.

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

Call this to update the slower nodes when the game speeds up...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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