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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 |
import SpriteKit class
GameScene: SKScene { var
bird = SKSpriteNode() var
skyColor = SKColor() var
verticalPipeGap = 150.0 var
pipeTextureUp = SKTexture() var
pipeTextureDown = SKTexture() var
movePipesAndRemove = SKAction() override
func didMoveToView(view: SKView) { // setup physics self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 ) // setup background color skyColor = SKColor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0) self.backgroundColor = skyColor // ground var
groundTexture = SKTexture(imageNamed: "land" ) groundTexture.filteringMode = SKTextureFilteringMode.Nearest var
moveGroundSprite = SKAction.moveByX(-groundTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.02 * groundTexture.size().width * 2.0)) var
resetGroundSprite = SKAction.moveByX(groundTexture.size().width * 2.0, y: 0, duration: 0.0) var
moveGroundSpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite,resetGroundSprite])) for
var i:CGFloat = 0; i < 2.0 + self.frame.size.width / ( groundTexture.size().width * 2.0 ); ++i { var
sprite = SKSpriteNode(texture: groundTexture) sprite.setScale(2.0) sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2.0) sprite.runAction(moveGroundSpritesForever) self.addChild(sprite) } // skyline var
skyTexture = SKTexture(imageNamed: "sky" ) skyTexture.filteringMode = SKTextureFilteringMode.Nearest var
moveSkySprite = SKAction.moveByX(-skyTexture.size().width * 2.0, y: 0, duration: NSTimeInterval(0.1 * skyTexture.size().width * 2.0)) var
resetSkySprite = SKAction.moveByX(skyTexture.size().width * 2.0, y: 0, duration: 0.0) var
moveSkySpritesForever = SKAction.repeatActionForever(SKAction.sequence([moveSkySprite,resetSkySprite])) for
var i:CGFloat = 0; i < 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ); ++i { var
sprite = SKSpriteNode(texture: skyTexture) sprite.setScale(2.0) sprite.zPosition = -20; sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2.0 + groundTexture.size().height * 2.0) sprite.runAction(moveSkySpritesForever) self.addChild(sprite) } // create the pipes textures pipeTextureUp = SKTexture(imageNamed: "PipeUp" ) pipeTextureUp.filteringMode = SKTextureFilteringMode.Nearest pipeTextureDown = SKTexture(imageNamed: "PipeDown" ) pipeTextureDown.filteringMode = SKTextureFilteringMode.Nearest // create the pipes movement actions var
distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeTextureUp.size().width); var
movePipes = SKAction.moveByX(-distanceToMove, y:0.0, duration:NSTimeInterval(0.01 * distanceToMove)); var
removePipes = SKAction.removeFromParent(); movePipesAndRemove = SKAction.sequence([movePipes, removePipes]); // spawn the pipes var
spawn = SKAction.runBlock({() in
self.spawnPipes()}) var
delay = SKAction.waitForDuration(NSTimeInterval(2.0)) var
spawnThenDelay = SKAction.sequence([spawn, delay]) var
spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay) self.runAction(spawnThenDelayForever) // setup our bird var
birdTexture1 = SKTexture(imageNamed: "bird-01" ) birdTexture1.filteringMode = SKTextureFilteringMode.Nearest var
birdTexture2 = SKTexture(imageNamed: "bird-02" ) birdTexture2.filteringMode = SKTextureFilteringMode.Nearest var
anim = SKAction.animateWithTextures([birdTexture1, birdTexture2], timePerFrame: 0.2) var
flap = SKAction.repeatActionForever(anim) bird = SKSpriteNode(texture: birdTexture1) bird.setScale(2.0) bird.position = CGPoint(x: self.frame.size.width * 0.35, y:self.frame.size.height * 0.6) bird.runAction(flap) bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0) bird.physicsBody.dynamic = true bird.physicsBody.allowsRotation = false self.addChild(bird) // create the ground var
ground = SKNode() ground.position = CGPointMake(0, groundTexture.size().height) ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0)) ground.physicsBody.dynamic = false self.addChild(ground) } func spawnPipes() { var
pipePair = SKNode() pipePair.position = CGPointMake( self.frame.size.width + pipeTextureUp.size().width * 2, 0 ); pipePair.zPosition = -10; var
height = UInt32( self.frame.size.height / 4 ) var
y = arc4random() % height + height; var
pipeDown = SKSpriteNode(texture: pipeTextureDown) pipeDown.setScale(2.0) pipeDown.position = CGPointMake(0.0, CGFloat(y) + pipeDown.size.height + CGFloat(verticalPipeGap)) pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size) pipeDown.physicsBody.dynamic = false pipePair.addChild(pipeDown) var
pipeUp = SKSpriteNode(texture: pipeTextureUp) pipeUp.setScale(2.0) pipeUp.position = CGPointMake(0.0, CGFloat(y)) pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size) pipeUp.physicsBody.dynamic = false pipePair.addChild(pipeUp) pipePair.runAction(movePipesAndRemove); self.addChild(pipePair) } override
func touchesBegan(touches: NSSet, withEvent event : UIEvent) { /* Called when a touch begins */ for
touch: AnyObject in
touches { let
location = touch.locationInNode(self) bird.physicsBody.velocity = CGVectorMake(0, 0) bird.physicsBody.applyImpulse(CGVectorMake(0, 30)) } } func clamp(min: CGFloat, max: CGFloat, value: CGFloat) -> CGFloat { if ( value > max ) { return
max; } else
if ( value < min ) { return
min; } else
{ return
value; } } override
func update(currentTime: CFTimeInterval) { /* Called before each frame is rendered */ bird.zRotation = self.clamp( -1, max: 0.5, value: bird.physicsBody.velocity.dy * ( bird.physicsBody.velocity.dy < 0 ? 0.003 : 0.001 ) ); } } |
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 |
import UIKit import SpriteKit extension SKNode { class
func unarchiveFromFile(file : NSString) -> SKNode? { let
path = NSBundle.mainBundle().pathForResource(file, ofType: "sks" ) var
sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil) var
archiver = NSKeyedUnarchiver(forReadingWithData: sceneData) archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene" ) let
scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as
GameScene archiver.finishDecoding() return
scene } } class
GameViewController: UIViewController { override
func viewDidLoad() { super.viewDidLoad() if
let scene = GameScene.unarchiveFromFile( "GameScene" ) as ? GameScene { // Configure the view. let
skView = self.view as
SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill skView.presentScene(scene) } } override
func shouldAutorotate() -> Bool { return
true } override
func supportedInterfaceOrientations() -> Int { if
UIDevice.currentDevice().userInterfaceIdiom == .Phone { return
Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw()) } else
{ return
Int(UIInterfaceOrientationMask.All.toRaw()) } } override
func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Release any cached data, images, etc that aren‘t in use. } } |
运行结果 :
swift交流技术群:107800925,共同学习,一起进步,好多牛人,有黑苹果装机高手!(意思就是说未必需要mac本)
FlapyBirds源码Swift版本,代码公布!,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/nil-lu/p/3770042.html