Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SpriteKit
- class GameScene: SKScene {
- // Make the Bird Sprite
- var bird = SKSpriteNode()
- // Make the Sky Sprite for Background Color
- var skyColor = SKColor()
- override func didMoveToView(view: SKView) {
- /* Setup your scene here */
- // Set the Sky Sprite to a color
- skyColor = SKColor(red: 113.0/255.0, green: 197.0/255.0, blue: 207.0/255.0, alpha: 1.0)
- self.backgroundColor = skyColor
- // 2 textures for animating the birds wings
- var birdTexture1 = SKTexture(imageNamed:"Bird1")
- birdTexture1.filteringMode = SKTextureFilteringMode.Nearest
- var birdTexture2 = SKTexture(imageNamed:"Bird2")
- birdTexture2.filteringMode = SKTextureFilteringMode.Nearest
- // Putting the animation together and running it forever
- var animation = SKAction.animateWithTextures([birdTexture1, birdTexture2], timePerFrame: 0.2)
- var flap = SKAction.repeatActionForever(animation)
- // Setting the texture and animation to the bird and the starting position
- bird = SKSpriteNode(texture: birdTexture1)
- bird.position = CGPoint(x: self.frame.size.width / 2.8, y: CGRectGetMidY(self.frame))
- bird.runAction(flap)
- // Add the bird to the scene
- self.addChild(bird)
- // Set the Ground Texture
- var groundTexture = SKTexture(imageNamed: "Ground")
- groundTexture.filteringMode = SKTextureFilteringMode.Nearest
- // Move the Ground
- var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width, y: 0, duration: NSTimeInterval(0.1 * groundTexture.size().width))
- // Reset the Ground
- var resetGroundSprite = SKAction.moveByX(groundTexture.size().width, y: 0, duration: 0.0)
- // Set it to be on loop and last forever (put it in the for loop below
- var moveGroundSpriteForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite, resetGroundSprite]))
- // Set a for loop so that the ground keeps going as far as the bird/player goes
- for var i:CGFloat = 0; i<2 + self.frame.size.width / (groundTexture.size().width); ++i {
- var sprite = SKSpriteNode(texture: groundTexture)
- sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2)
- sprite.runAction(moveGroundSpriteForever)
- self.addChild(sprite)
- }
- // Set the Skyline Texture
- var skylineTexture = SKTexture(imageNamed: "Skyline")
- skylineTexture.filteringMode = SKTextureFilteringMode.Nearest
- var moveSkylineSprite = SKAction.moveByX(-skylineTexture.size().width, y: 0, duration: NSTimeInterval(0.1 * skylineTexture.size().width))
- var resetSkylineSprite = SKAction.moveByX(skylineTexture.size().width, y: 0, duration: 0.0)
- var moveSkylineSpriteForever = SKAction.repeatActionForever(SKAction.sequence([moveSkylineSprite, resetSkylineSprite]))
- // Make it continue like the Ground Texture
- for var i:CGFloat = 0; i<2.0 + self.frame.size.width / (skylineTexture.size().width); ++i {
- var sprite = SKSpriteNode(texture: skylineTexture)
- sprite.zPosition = -20;
- sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2 + groundTexture.size().height)
- sprite.runAction(moveSkylineSpriteForever)
- self.addChild(sprite)
- }
- }
- override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
- /* Called when a touch begins */
- }
- override func update(currentTime: CFTimeInterval) {
- /* Called before each frame is rendered */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment