下面是生成一个“围住神经猫”游戏的初始场景:

1,界面下方会生成9*9共81个圆点,同时圆点内部添加文本标签显示索引
2,默认圆点为灰色,每行随机取两个点变为红色
3,奇数行和偶数行有一定的错位,错位的横向距离是圆的半径
效果图如下:
Swift - 在界面上生成81个随机红,灰色圆点(SpriteKit游戏开发)-LMLPHP
1,首先给资源库Images.xcassets添加资源图片:
Swift - 在界面上生成81个随机红,灰色圆点(SpriteKit游戏开发)-LMLPHP
2,圆点类 GaPoint.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import SpriteKit
 
//圆点类型枚举
enum PointType:Int{
    //灰0 红1
    case gray=0,red;
}
 
class GaPoint:SKSpriteNode{
 
    //自身索引
    var index = 0
    //圆点类型,默认为灰色点
    var type = PointType.gray
    //圆点内的文本节点
    var label:SKLabelNode?
     
    //设置文本节点的文字
    func onSetLabel(i:String){
        if label == nil {
            //文本节点的字体
            label = SKLabelNode(fontNamed:"Chalkduster")
            //文本节点的字体大小
            label!.fontSize = 22
            //文本节点的位置
            label!.position = CGPointMake(0, -10)
            //取消文本节点的交互性
            label!.userInteractionEnabled = false
            //将文本节点加入圆点类
            self.addChild(label!)
        }
        //设置文本节点的内容
        label!.text = i
    }
}

3,圆点容器类 PointContainer.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import SpriteKit
 
class PointContainer:SKNode,UIAlertViewDelegate{
    //灰点纹理
    let textPoint1 = SKTexture(imageNamed: "pot1")
    //红点纹理
    let textPoint2 = SKTexture(imageNamed: "pot2")
    //记录所有圆点的数组
    var arrPoint = [GaPoint]()
    
    func onInit(){
        for i in 0...80 {
            //默认为灰色点
            let point = GaPoint(texture: textPoint1)
            //对行数除以9取整,用来为下面判断奇偶行做准备
            let row = Int(i/9)
            //列
            let col = i%9
            //x轴位移
            var gap = 0
            //奇偶行判断
            if Int(row % 2) == 1 {
                gap = Int(textPoint1.size().width)/2
            }else {
                 
            }
            //取得纹理尺寸,用作计算位置
            let width = Int(textPoint1.size().width)
            //计算x坐标
            let x  = col * (width+5) - (9*width)/2 + gap
            //计算y坐标
            let y  = row * width - (9*width)/2
            //设置位置
            point.position = CGPointMake(CGFloat(x),CGFloat(y))
             
            //记录索引
            point.index = i
            point.onSetLabel("\(i)")
            //设置z轴排序
            point.zPosition = 10
            //将点加入PointContainer中
            self.addChild(point)
            //将点加入数组中
            arrPoint.append(point)
        }
 
        //随机生成红点
        onCreateRed()
    }
     
    //随机生成红色的点
    func onCreateRed(){
        for i in 0...8 {
            let r1 = Int(arc4random() % 9) + i * 9
            let r2 = Int(arc4random() % 9) + i * 9
            onSetRed(r1)
            onSetRed(r2)
        }
    }
     
    //改变圆点类型
    func onSetRed(index:Int){
        arrPoint[index].type = PointType.red
        arrPoint[index].texture = textPoint2
    }
}

4,主界面 GameScene.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import SpriteKit
 
class GameScene: SKScene {
     
    let pointContainer = PointContainer()
     
    //当切换到这个场景视图后
    override func didMoveToView(view: SKView) {
        //将Images.xcassets中的bg.jpg 来制作背景
        var bg = SKSpriteNode(imageNamed: "bg")
        //设置位置
        bg.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
        self.addChild(bg)
         
        //设置圆点容器
        pointContainer.position = CGPoint(x:CGRectGetMidX(self.frame)-10,
            y:CGRectGetMidY(self.frame)-150);
        self.addChild(pointContainer)
        pointContainer.onInit()
         
    }
    //响应屏幕点击的方法
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
         
    }
}
05-08 15:41