下面通过一个样例演示如何实现飞行道具的生成,以及道具碰撞拾取。
样例说明:
1,屏幕从右到左不断地生成苹果飞过来(苹果高度随机)
2,点击屏幕可以让熊猫跳跃
3,熊猫碰到苹果,苹果消失
运行效果:
样例代码:
苹果工厂类 AppleFactory.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 67 68 69 70 71 | import SpriteKit class AppleFactory : SKNode { //定义苹果纹理 let appleTexture = SKTexture (imageNamed: "apple" ) //游戏场景的狂赌 var sceneWidth : CGFloat = 0.0 //定义苹果数组 var arrApple = [ SKSpriteNode ]() //定时器 var timer = NSTimer () func onInit(width: CGFloat ) { self .sceneWidth = width //启动的定时器 timer = NSTimer .scheduledTimerWithTimeInterval( 0.2, target: self , selector: "createApple" , userInfo: nil , repeats: true ) } //创建苹果类 func createApple(){ //通过随机数来随机生成苹果 //算法是,随机生成0-9的数,当随机数大于8的时候声称苹果 //也就是说,有1/10的几率生成苹果 //这样游戏场景中的苹果就不会整整齐齐以相同间隔出现了 let random = arc4random() % 10 if random > 8 { //生成苹果 let apple = SKSpriteNode (texture: appleTexture) //设置物理体 apple.physicsBody = SKPhysicsBody (rectangleOfSize: apple.size) //弹性设为0 apple.physicsBody?.restitution = 0 //物理体标识 apple.physicsBody?.categoryBitMask = BitMaskType .apple //不受物理效果影响 apple.physicsBody?. dynamic = false //设置中心点 apple.anchorPoint = CGPointMake (0, 0) //z轴深度 apple.zPosition = 40 //设定位置 let theY = CGFloat (arc4random()%200 + 200) apple.position = CGPointMake (sceneWidth+apple.frame.width , theY) //加入数组 arrApple.append(apple) //加入场景 self .addChild(apple) } } //苹果移动方法 func move(speed: CGFloat ){ for apple in arrApple { apple.position.x -= speed } //移出屏幕外时移除苹果 if arrApple.count > 0 && arrApple[0].position.x < -20{ arrApple[0].removeFromParent() arrApple.removeAtIndex(0) } } //重置方法 func reSet(){ //移除所有子对象 self .removeAllChildren() //清空苹果数组 arrApple.removeAll(keepCapacity: false ) } } |
熊猫类 Panda.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 | import SpriteKit class Panda : SKSpriteNode { //定义纹理 let pandaTexture = SKTexture (imageNamed: "panda" ) init () { //执行父类的构造方法 super . init (texture:pandaTexture,color: SKColor .whiteColor(),size:pandaTexture.size()) //设置中心点 self .anchorPoint = CGPointMake (0, 0) self .physicsBody = SKPhysicsBody (rectangleOfSize:pandaTexture.size()) self .physicsBody?. dynamic = true self .physicsBody?.allowsRotation = false //弹性 self .physicsBody?.restitution = 0 self .physicsBody?.categoryBitMask = BitMaskType .panda self .physicsBody?.contactTestBitMask = BitMaskType .scene| BitMaskType .apple self .physicsBody?.collisionBitMask = BitMaskType .scene } required init (coder aDecoder: NSCoder ) { fatalError( "init(coder:) has not been implemented" ) } //跳 func jump (){ //施加一个向上的力,让小人跳起来 self .physicsBody?.velocity = CGVectorMake (0, 700) } } |
碰撞标识类 - BitMaskType.swift
1 2 3 4 5 6 7 8 9 10 11 | class BitMaskType { class var panda: UInt32 { return 1<<0 } class var apple: UInt32 { return 1<<1 } class var scene: UInt32 { return 1<<2 } } |
主场景 - 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 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 | import SpriteKit class GameScene : SKScene , SKPhysicsContactDelegate { lazy var appleFactory = AppleFactory () lazy var panda = Panda () //移动速度 var moveSpeed: CGFloat = 15 //吃到的苹果数 var appleNum = 0 override func didMoveToView(view: SKView ) { //物理世界代理 self .physicsWorld.contactDelegate = self //重力设置 self .physicsWorld.gravity = CGVectorMake (0, -5) //设置物理体 self .physicsBody = SKPhysicsBody (edgeLoopFromRect: self .frame) //设置种类标示 self .physicsBody?.categoryBitMask = BitMaskType .scene //是否响应物理效果 self .physicsBody?. dynamic = false //场景的背景颜色 let skyColor = SKColor (red:113/255,green:197/255,blue:207/255,alpha:1) self .backgroundColor = skyColor //给小人定一个初始位置 panda.position = CGPointMake (200, 400) //将小人显示在场景中 self .addChild(panda) //苹果工厂 appleFactory.onInit( self .frame.width) self .addChild( appleFactory ) } override func touchesBegan(touches: Set < UITouch >, withEvent event: UIEvent ?) { panda.jump() } override func update(currentTime: CFTimeInterval ) { appleFactory.move(moveSpeed) } //碰撞检测方法 func didBeginContact(contact: SKPhysicsContact ) { //熊猫和苹果碰撞 if (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask) == ( BitMaskType .apple | BitMaskType .panda){ //苹果计数加1 self .appleNum++ //如果碰撞体A是苹果,隐藏碰撞体A,反之隐藏碰撞体B //(因为苹果出了屏幕都会被移除,所以这里隐藏就可以了) if contact.bodyA.categoryBitMask == BitMaskType .apple { contact.bodyA.node?.hidden = true } else { contact.bodyB.node?.hidden = true } } } } |
源码下载: