SpriteKit改变一个节点的SKAction

时间:2014-07-31 16:43:55

标签: ios objective-c sprite-kit skaction

我尝试让节点在特定的系统时间后移动得更快。例如,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];

}

1 个答案:

答案 0 :(得分:2)

用以下代码替换你的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"];
        }
    }
}