4个图像的背景相互添加

时间:2015-02-16 10:44:09

标签: swift sprite-kit

请帮忙。

我有4张照片。 我想用动画展示背景。

每张图片都应该落在之前的动画alpha上。结果,我得到了相互添加的4张图像的背景。

首先显示1.png fade alpha为1。 直到最后一个没有出现,开始第二个图像等等。

     var waitTime:NSTimeInterval    = 0
     var durTime:NSTimeInterval     = 4.4

     for var i = 0; i < 4; i++ {

            let frostImage = SKSpriteNode(imageNamed: "a\(i)")
            frostImage.position = CGPointMake(0, 0)
            frostImage.zPosition = -1
            frostImage.alpha = 0
            frostImage.anchorPoint = CGPointZero
            frostImage.size  = CGSizeMake(256, 384)
            self.addChild(frostImage)

            self.array.addObject(frostImage)

            let w = SKAction.waitForDuration(waitTime)
            let s = SKAction.fadeAlphaTo(0.6, duration: durTime)
            let a = SKAction.repeatAction(SKAction.sequence([w, s]), count: 1)


            durTime -= 0.4
            waitTime += 0.2

            self.arrayAnim.addObject(a)
        }


then 



            for node in self.array {
                node.runAction(self.arrayAnim[ind1] as SKAction, completion: nil)
                ind1++
            }

1 个答案:

答案 0 :(得分:0)

我是为你写的,但是在Objective-c中,我认为你可以把它转换为swift。

    SKSpriteNode *bg1 = [SKSpriteNode spriteNodeWithImageNamed:@"bg1"];
    bg1.zPosition = 1;
    SKSpriteNode *bg2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg2"];
    bg2.zPosition = 2;
    SKSpriteNode *bg3 = [SKSpriteNode spriteNodeWithImageNamed:@"bg3"];
    bg3.zPosition = 3;
    SKSpriteNode *bg4 = [SKSpriteNode spriteNodeWithImageNamed:@"bg4"];
    bg4.zPosition = 4;

    SKAction *action = [SKAction fadeAlphaTo:1.0 duration:1];
    SKAction *wait = [SKAction waitForDuration:1];
    SKAction *resetAlpha = [SKAction fadeAlphaBy:0.0 duration:0];

    [self runAction:[SKAction repeatActionForever:[SKAction sequence:@[[SKAction runBlock:^{
        [bg1 runAction:resetAlpha];
        [bg2 runAction:resetAlpha];
        [bg3 runAction:resetAlpha];
        [bg4 runAction:resetAlpha];
    }], [SKAction runBlock:^{
        [bg1 runAction:action];
    }], wait, [SKAction runBlock:^{
        [bg2 runAction:action];
    }], wait, [SKAction runBlock:^{
        [bg3 runAction:action];
    }], wait, [SKAction runBlock:^{
        [bg4 runAction:action];
    }], wait]]]];