




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
GameViewController.swift/ Step 1 Add the GameSceneimport UIKitimport SpriteKit class GameViewController: UIViewController override func viewDidLoad() super.viewDidLoad() let scene = GameScene(size: view.bounds.size) let skView = view as! SKView skView.showsFPS = true skView.showsNodeCount = true skView.ignoresSiblingOrder = true scene.scaleMode = .ResizeFill skView.presentScene(scene) override func prefersStatusBarHidden() - Bool return true /Step 2: Create a game scene with a playerclass GameScene: SKScene / 1 let player = SKSpriteNode(imageNamed: player) override func didMoveToView(view: SKView) / 2 backgroundColor = SKColor.whiteColor() / 3 player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.3) / 4 addChild(player) /Step 3: Add some monsters off the screen func random() - CGFloat return CGFloat(Float(arc4random() / 0xFFFFFFFF) func random(min min: CGFloat, max: CGFloat) - CGFloat return random() * (max - min) + min func addMonster() / Create sprite let monster = SKSpriteNode(imageNamed: monster) / Determine where to spawn the monster along the Y axis let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2) / Position the monster slightly off-screen along the right edge, / and along a random position along the Y axis as calculated above monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY) / Add the monster to the scene addChild(monster) / Determine speed of the monster let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0) / Create the actions let actionMove = SKAction.moveTo(CGPoint(x: -monster.size.width/2, y: actualY), duration: NSTimeInterval(actualDuration) let actionMoveDone = SKAction.removeFromParent() monster.runAction(SKAction.sequence(actionMove, actionMoveDone) /Step 4: Create monsters within the didMoveView function runAction(SKAction.repeatActionForever( SKAction.sequence( SKAction.runBlock(addMonster), SKAction.waitForDuration(1.0) ) ) / Step 5: Adding Vector Math, for calculating projectiles/ use operator overloading of the Swiftfunc + (left: CGPoint, right: CGPoint) - CGPoint return CGPoint(x: left.x + right.x, y: left.y + right.y)func - (left: CGPoint, right: CGPoint) - CGPoint return CGPoint(x: left.x - right.x, y: left.y - right.y)func * (point: CGPoint, scalar: CGFloat) - CGPoint return CGPoint(x: point.x * scalar, y: point.y * scalar)func / (point: CGPoint, scalar: CGFloat) - CGPoint return CGPoint(x: point.x / scalar, y: point.y / scalar)#if !(arch(x86_64) | arch(arm64) func sqrt(a: CGFloat) - CGFloat return CGFloat(sqrtf(Float(a) #endifextension CGPoint func length() - CGFloat return sqrt(x*x + y*y) func normalized() - CGPoint return self / length() =/ Step 6: Add touchesoverride func touchesEnded(touches: Set, withEvent event: UIEvent?) / 1 - Choose one of the touches to work with guard let touch = touches.first else return let touchLocation = touch.locationInNode(self) / 2 - Set up initial location of projectile let projectile = SKSpriteNode(imageNamed: projectile) projectile.position = player.position / 3 - Determine offset of location to projectile let offset = touchLocation - projectile.position / 4 - Bail out if you are shooting down or backwards if (offset.x 0) return / 5 - OK to add now - youve double checked position addChild(projectile) / 6 - Get the direction of where to shoot let direction = offset.normalized() / 7 - Make it shoot far enough to be guaranteed off screen let shootAmount = direction * 1000 / 8 - Add the shoot amount to the current position let realDest = shootAmount + projectile.position / 9 - Create the actions let actionMove = SKAction.moveTo(realDest, duration: 2.0) let actionMoveDone = SKAction.removeFromParent() projectile.runAction(SKAction.sequence(actionMove, actionMoveDone) Collison Detection:/ Step 7: Global Structstruct PhysicsCategory static let None : UInt32 = 0 static let All : UInt32 = UInt32.max static let Monster : UInt32 = 0b1 / 1 static let Projectile: UInt32 = 0b10 / 2/ Step 8: Mark GameScene to follow PhysicsContactDelegateclass GameScene: SKScene, SKPhysicsContactDelegate / Step 9: Inside didMoveToView(_:)physicsWorld.gravity = CGVectorMake(0, 0)physicsWorld.contactDelegate = self/ Step 10: Add to addMonster()/ Add physics to AddMonstermonster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.size) / 1monster.physicsBody?.dynamic = true / 2monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster / 3monster.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile / 4monster.physicsBody?.collisionBitMask = PhysicsCategory.None / 5/ Step 11: touchesEnded(_:withEvent:), right after the line setting the projectiles position:/ Adding Physics to the projectileprojectile.physicsBody = SKPhysicsBody(circleOfRadius: projectile.size.width/2)projectile.physicsBody?.dynamic = trueprojectile.physicsBody?.categoryBitMask = PhysicsCategory.Projectileprojectile.physicsBody?.contactTestBitMask = PhysicsCategory.Monsterprojectile.physicsBody?.collisionBitMask = PhysicsCategory.Noneprojectile.physicsBody?.usesPreciseCollisionDetection = true/ Step 12: New Method: Projectile & Collisionfunc projectileDidCollideWithMonster(projectile:SKSpriteNode, monster:SKSpriteNode) print(Hit) projectile.removeFromParent() monster.removeFromParent()/ Step 13: DelegateContact within GameScene classfunc didBeginContact(contact: SKPhysicsContact) / 1 var firstBody: SKPhysicsBody var secondBody: SKPhysicsBody if contact.bo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 营销咨询方案(3篇)
- 木质素复合材料生态友好性评估
- 药抗生素使用课件
- 市场对绿色包装接受度分析报告
- 建筑方案设计与总图审批
- 2025版司法局《财产保全反担保申请书》(空白模板)
- 高徽浆灌浆施工方案
- 药品经营监督检查课件
- 中式建筑方案设计图
- 惠农区网络推广营销方案
- 2024年司法协理员招聘考试题库及答案
- DB61-T 5061-2023 民用建筑有线电视系统工程技术规程
- 质量保障方案文案(3篇)
- 产科分娩风险管理制度
- DB61T-建设项目使用草地现状调查技术规范
- 安徽省房屋建筑和市政基础设施工程施工应用BIM技术招标投标评标办法实施导则(2025版)
- 急诊仪器设备管理制度
- T/CCOA 62-2023大豆油生产技术规范
- (高清版)DG∕TJ 08-207-2008 房屋修缮工程技术规程
- 江苏省普通高中生物课程标准教学要求(修订稿)
- 国家智慧中小学教育平台应用培训
评论
0/150
提交评论