您可以使用以下代碼設置四個彩色Square。
class FourColorSquare : SKNode {
private var colors : [UIColor] = []
private var size : CGSize!
init(colors : [UIColor], size: CGSize) {
super.init()
self.colors = colors
self.size = size
self.setupNodes()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setupNodes() {
let node1 = SKSpriteNode(color: colors[0], size: self.size)
node1.position = CGPointMake(0, 0)
node1.anchorPoint = CGPointMake(1, 0)
addChild(node1)
let node2 = SKSpriteNode(color: colors[1], size: self.size)
node2.position = CGPointMake(0, 0)
node2.anchorPoint = CGPointMake(0, 0)
addChild(node2)
let node3 = SKSpriteNode(color: colors[2], size: self.size)
node3.position = CGPointMake(0, 0)
node3.anchorPoint = CGPointMake(0, 1)
addChild(node3)
let node4 = SKSpriteNode(color: colors[3], size: self.size)
node4.position = CGPointMake(0, 0)
node4.anchorPoint = CGPointMake(1, 1)
addChild(node4)
}
func rotate(angle : CGFloat, animated : Bool) {
var rotateAction : SKAction!
if animated {
rotateAction = SKAction.rotateByAngle(angle, duration: 0.6)
}
else {
rotateAction = SKAction.rotateByAngle(angle, duration: 0)
}
for node in self.children as [SKSpriteNode] {
node.runAction(rotateAction)
}
}
}
您可以使用它像這樣
let colors = FourColorSquare(colors: [.redColor(),.greenColor(),.blueColor(),.yellowColor()], size: CGSizeMake(50, 50))
colors.position = CGPointMake(200, 100)
addChild(colors)
colors.rotate(-3.14/2, animated: true)
您可以設置單獨的物理機構在FourColorSquare
每個節點進行碰撞檢測每種顏色。每種顏色應該有一個單獨的categoryBitMask
來測試與每個彩色球碰撞。
謝謝,這對我有用。 – 2015-04-04 14:58:40