首页 技术 正文
技术 2022年11月14日
0 收藏 689 点赞 3,884 浏览 5153 个字

下面通过一个样例演示如何实现飞行道具的生成,以及道具碰撞拾取。

样例说明:1,屏幕从右到左不断地生成苹果飞过来(苹果高度随机)2,点击屏幕可以让熊猫跳跃3,熊猫碰到苹果,苹果消失运行效果:Swift – 跳跃吃苹果游戏开发(SpriteKit游戏开发)样例代码:苹果工厂类 AppleFactory.swift

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 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

123456789101112131415161718192021222324252627282930313233 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

1234567891011 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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 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                }        }    }}

源码下载:Swift – 跳跃吃苹果游戏开发(SpriteKit游戏开发)EatApple.zip

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,858