快速组合多个精灵

时间:2016-01-31 20:22:58

标签: ios swift sprite-kit

我正在尝试向视图中添加4个精灵,在假想方块的另一个角落中添加每个精灵,然后围绕中心旋转所有精灵。完成此任务的最佳方法是什么?

我知道如何旋转每一个精灵,但也许有一种方法可以将多个精灵分组,比如将它们放入容器中?

How the result should look like

1 个答案:

答案 0 :(得分:1)

这是我的工作代码。

import SpriteKit

let player = SKNode()
let playerTopLeft = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(20, 20))
let playerTopRight = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(20, 20))
let playerBottomRight = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(20, 20))
let playerBottomLeft = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(20, 20))

class GameScene: SKScene {

    override func didMoveToView(view: SKView) {
        backgroundColor = SKColor.blackColor()

        player.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
        addChild(player)

        playerTopLeft.position = CGPoint(x:-40, y:-40)
        player.addChild(playerTopLeft)

        playerTopRight.position = CGPoint(x:40, y:-40)
        player.addChild(playerTopRight)

        playerBottomRight.position = CGPoint(x:40, y:40)
        player.addChild(playerBottomRight)

        playerBottomLeft.position = CGPoint(x:-40, y:40)
        player.addChild(playerBottomLeft)

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch in touches {

            let rotate = SKAction.rotateByAngle(CGFloat(-M_PI_2), duration:1)
            player.runAction(rotate)

        }
    }
}