2015-04-03 122 views
0

非常感谢提前。 我试图在StackOverflow上搜索或只是谷歌它,也许我用错了关键词或别的东西,我无法找到我的问题的答案。 所以我是iOS新手,我是这么做的,我在屏幕中间用四种不同颜色(这是一张图片)设置了一个正方形,每当我点击屏幕时,它都会旋转90度。还有更小的球会从屏幕外部以颜色出现,比如红色的球,绿色的球,蓝色的球等。当球以相同的颜色接触正方形时,得分为1分。就像游戏一样。颜色搭配精灵套件和swift

那么我应该如何设置不同颜色的方形来实现这一点呢? 我认为它只能将一种颜色设置为一个精灵。或者我应该把4个三角形放在一起做广场?

回答

2

您可以使用以下代码设置四个彩色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来测试与每个彩色球碰撞。

+0

谢谢,这对我有用。 – 2015-04-04 14:58:40

1

您可以使用与图形CGContext相关联的路径,描边和填充功能,并修改下面的代码以使其在图像中绘制所需图案并用颜色填充不同部分。由此产生的UIImage将成为新雪碧的基础。请参阅CGContext Documentation

import CoreImage 
    import CoreGraphics 
    int width = 100 
    int height = 100 
    var colorSpace = CGColorSpaceCreateDeviceRGB() 
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue) 
    var ctx = CGBitmapContextCreate(nil, width, height, 8, 0, colorspace, bitmapInfo)! 
    . 
    . draw into your ctx (context) here 
    . 
    var image = UIImage(CGImage: CGBitmapContextCreateImage(ctx))!