欢迎来到天天文库
浏览记录
ID:42605527
大小:14.80 KB
页数:8页
时间:2019-09-18
《iOS开发 Swift 实战代码之 FlappyBird》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、GameViewController.swift//Step1AddtheGameSceneimportUIKitimportSpriteKitclassGameViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()letscene=GameScene(size:view.bounds.size)letskView=viewas!SKViewskView.showsFPS=trueskView.showsNodeCount
2、=trueskView.ignoresSiblingOrder=truescene.scaleMode=.ResizeFillskView.presentScene(scene)}overridefuncprefersStatusBarHidden()->Bool{returntrue}}//Step2:CreateagamescenewithaplayerclassGameScene:SKScene{//1letplayer=SKSpriteNode(imageNamed:"player")overridefuncdi
3、dMoveToView(view:SKView){//2backgroundColor=SKColor.whiteColor()//3player.position=CGPoint(x:size.width*0.1,y:size.height*0.3)//4addChild(player)}}//Step3:Addsomemonstersoffthescreenfuncrandom()->CGFloat{returnCGFloat(Float(arc4random())/0xFFFFFFFF)}funcrandom(mi
4、nmin:CGFloat,max:CGFloat)->CGFloat{returnrandom()*(max-min)+min}funcaddMonster(){//Createspriteletmonster=SKSpriteNode(imageNamed:"monster")//DeterminewheretospawnthemonsteralongtheYaxisletactualY=random(min:monster.size.height/2,max:size.height-monster.size.heig
5、ht/2)//Positionthemonsterslightlyoff-screenalongtherightedge,//andalongarandompositionalongtheYaxisascalculatedabovemonster.position=CGPoint(x:size.width+monster.size.width/2,y:actualY)//AddthemonstertothesceneaddChild(monster)//Determinespeedofthemonsterletactua
6、lDuration=random(min:CGFloat(2.0),max:CGFloat(4.0))//CreatetheactionsletactionMove=SKAction.moveTo(CGPoint(x:-monster.size.width/2,y:actualY),duration:NSTimeInterval(actualDuration))letactionMoveDone=SKAction.removeFromParent()monster.runAction(SKAction.sequence(
7、[actionMove,actionMoveDone]))}//Step4:CreatemonsterswithinthedidMoveViewfunctionrunAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(addMonster),SKAction.waitForDuration(1.0)])))//Step5:AddingVectorMath,forcalculatingprojectiles//useoperato
8、roverloadingoftheSwiftfunc+(left:CGPoint,right:CGPoint)->CGPoint{returnCGPoint(x:left.x+right.x,y:left.y+right.y)}func-(left:CGPoint,right:CGPoint)->CGPoint{re
此文档下载收益归作者所有